wallet-core/packages/idb-bridge/src/backend-interface.ts

225 lines
5.1 KiB
TypeScript
Raw Normal View History

2019-08-01 23:27:42 +02:00
/*
Copyright 2019 Florian Dold
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing
permissions and limitations under the License.
*/
2022-02-10 19:52:45 +01:00
import { BridgeIDBDatabaseInfo, BridgeIDBKeyRange } from "./bridge-idb.js";
2019-06-15 22:44:54 +02:00
import {
IDBCursorDirection,
IDBTransactionMode,
IDBValidKey,
2022-02-10 19:52:45 +01:00
} from "./idbtypes.js";
/** @public */
2019-06-15 22:44:54 +02:00
export interface ObjectStoreProperties {
keyPath: string[] | null;
2019-06-15 22:44:54 +02:00
autoIncrement: boolean;
indexes: { [nameame: string]: IndexProperties };
2019-06-15 22:44:54 +02:00
}
/** @public */
2019-06-15 22:44:54 +02:00
export interface IndexProperties {
keyPath: string[];
2019-06-15 22:44:54 +02:00
multiEntry: boolean;
unique: boolean;
}
/** @public */
2019-06-15 22:44:54 +02:00
export interface Schema {
databaseName: string;
databaseVersion: number;
objectStores: { [name: string]: ObjectStoreProperties };
}
/** @public */
2019-06-15 22:44:54 +02:00
export interface DatabaseConnection {
connectionCookie: string;
}
/** @public */
2019-06-15 22:44:54 +02:00
export interface DatabaseTransaction {
transactionCookie: string;
}
/** @public */
2019-06-15 22:44:54 +02:00
export enum ResultLevel {
OnlyCount,
2019-06-23 22:16:03 +02:00
OnlyKeys,
Full,
2019-06-15 22:44:54 +02:00
}
/** @public */
export enum StoreLevel {
NoOverwrite,
AllowOverwrite,
UpdateExisting,
}
/** @public */
2019-06-15 22:44:54 +02:00
export interface RecordGetRequest {
direction: IDBCursorDirection;
2019-06-15 22:44:54 +02:00
objectStoreName: string;
indexName: string | undefined;
2019-06-21 19:18:36 +02:00
/**
* The range of keys to return.
* If indexName is defined, the range refers to the index keys.
* Otherwise it refers to the object store keys.
*/
range: BridgeIDBKeyRange | undefined | null;
2019-06-21 19:18:36 +02:00
/**
* Last cursor position in terms of the index key.
* Can only be specified if indexName is defined and
* lastObjectStorePosition is defined.
*
2019-06-21 19:18:36 +02:00
* Must either be undefined or within range.
*/
lastIndexPosition?: IDBValidKey;
2019-06-21 19:18:36 +02:00
/**
* Last position in terms of the object store key.
*/
lastObjectStorePosition?: IDBValidKey;
2019-06-21 19:18:36 +02:00
/**
* If specified, the index key of the results must be
* greater or equal to advanceIndexKey.
*
2019-06-21 19:18:36 +02:00
* Only applicable if indexName is specified.
*/
advanceIndexKey?: IDBValidKey;
2019-06-21 19:18:36 +02:00
/**
* If specified, the primary key of the results must be greater
* or equal to advancePrimaryKey.
*/
advancePrimaryKey?: IDBValidKey;
2019-06-21 19:18:36 +02:00
/**
* Maximum number of results to return.
* If 0, return all available results
2019-06-21 19:18:36 +02:00
*/
2019-06-15 22:44:54 +02:00
limit: number;
resultLevel: ResultLevel;
}
/** @public */
2019-06-15 22:44:54 +02:00
export interface RecordGetResponse {
values: any[] | undefined;
indexKeys: IDBValidKey[] | undefined;
primaryKeys: IDBValidKey[] | undefined;
2019-06-15 22:44:54 +02:00
count: number;
}
/** @public */
2019-06-15 22:44:54 +02:00
export interface RecordStoreRequest {
objectStoreName: string;
value: any;
key: IDBValidKey | undefined;
storeLevel: StoreLevel;
2019-06-15 22:44:54 +02:00
}
/** @public */
export interface RecordStoreResponse {
/**
* Key that the record was stored under in the object store.
*/
key: IDBValidKey;
}
/** @public */
2019-06-15 22:44:54 +02:00
export interface Backend {
getDatabases(): Promise<BridgeIDBDatabaseInfo[]>;
connectDatabase(name: string): Promise<DatabaseConnection>;
beginTransaction(
conn: DatabaseConnection,
objectStores: string[],
mode: IDBTransactionMode,
2019-06-15 22:44:54 +02:00
): Promise<DatabaseTransaction>;
enterVersionChange(
conn: DatabaseConnection,
newVersion: number,
): Promise<DatabaseTransaction>;
deleteDatabase(name: string): Promise<void>;
2019-06-15 22:44:54 +02:00
close(db: DatabaseConnection): Promise<void>;
getSchema(db: DatabaseConnection): Schema;
2021-02-16 14:49:38 +01:00
getCurrentTransactionSchema(btx: DatabaseTransaction): Schema;
getInitialTransactionSchema(btx: DatabaseTransaction): Schema;
renameIndex(
btx: DatabaseTransaction,
objectStoreName: string,
oldName: string,
newName: string,
): void;
2019-06-15 22:44:54 +02:00
deleteIndex(
btx: DatabaseTransaction,
objectStoreName: string,
indexName: string,
): void;
2019-06-15 22:44:54 +02:00
rollback(btx: DatabaseTransaction): Promise<void>;
commit(btx: DatabaseTransaction): Promise<void>;
deleteObjectStore(btx: DatabaseTransaction, name: string): void;
createObjectStore(
btx: DatabaseTransaction,
name: string,
keyPath: string | string[] | null,
autoIncrement: boolean,
): void;
renameObjectStore(
btx: DatabaseTransaction,
oldName: string,
newName: string,
): void;
createIndex(
btx: DatabaseTransaction,
indexName: string,
objectStoreName: string,
keyPath: string | string[],
2019-06-15 22:44:54 +02:00
multiEntry: boolean,
unique: boolean,
): void;
deleteRecord(
btx: DatabaseTransaction,
objectStoreName: string,
range: BridgeIDBKeyRange,
): Promise<void>;
getRecords(
btx: DatabaseTransaction,
req: RecordGetRequest,
): Promise<RecordGetResponse>;
storeRecord(
btx: DatabaseTransaction,
storeReq: RecordStoreRequest,
): Promise<RecordStoreResponse>;
2021-02-23 20:16:10 +01:00
clearObjectStore(
btx: DatabaseTransaction,
objectStoreName: string,
2021-06-09 15:26:18 +02:00
): Promise<void>;
2019-06-15 22:44:54 +02:00
}