diff options
author | Florian Dold <florian@dold.me> | 2023-07-11 15:41:48 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2023-08-22 08:01:13 +0200 |
commit | b2d0ad57ddf251a109d536cdc49fb6505dbdc50c (patch) | |
tree | 7eaeca3ad8ec97a9c1970c1004feda2d61c3441b /packages/idb-bridge/src/backend-interface.ts | |
parent | 58fdf9dc091b076787a9746c405fe6a9366f5da6 (diff) | |
download | wallet-core-b2d0ad57ddf251a109d536cdc49fb6505dbdc50c.tar.xz |
sqlite3 backend for idb-bridge / wallet-core
Diffstat (limited to 'packages/idb-bridge/src/backend-interface.ts')
-rw-r--r-- | packages/idb-bridge/src/backend-interface.ts | 142 |
1 files changed, 94 insertions, 48 deletions
diff --git a/packages/idb-bridge/src/backend-interface.ts b/packages/idb-bridge/src/backend-interface.ts index a21515544..3255261e2 100644 --- a/packages/idb-bridge/src/backend-interface.ts +++ b/packages/idb-bridge/src/backend-interface.ts @@ -21,66 +21,45 @@ import { IDBValidKey, } from "./idbtypes.js"; -/** @public */ -export interface ObjectStoreProperties { - keyPath: string[] | null; - autoIncrement: boolean; - indexes: { [nameame: string]: IndexProperties }; -} - -/** @public */ -export interface IndexProperties { - keyPath: string[]; - multiEntry: boolean; - unique: boolean; -} - -/** @public */ -export interface Schema { - databaseName: string; - databaseVersion: number; - objectStores: { [name: string]: ObjectStoreProperties }; +export interface ConnectResult { + conn: DatabaseConnection; + version: number; + objectStores: string[]; } -/** @public */ export interface DatabaseConnection { connectionCookie: string; } -/** @public */ export interface DatabaseTransaction { transactionCookie: string; } -/** @public */ export enum ResultLevel { OnlyCount, OnlyKeys, Full, } -/** @public */ export enum StoreLevel { NoOverwrite, AllowOverwrite, UpdateExisting, } -/** @public */ -export interface RecordGetRequest { + +export interface IndexGetQuery { direction: IDBCursorDirection; objectStoreName: string; - indexName: string | undefined; + indexName: string; /** * 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. + * The range refers to the index keys. */ range: BridgeIDBKeyRange | undefined | null; /** * Last cursor position in terms of the index key. - * Can only be specified if indexName is defined and - * lastObjectStorePosition is defined. + * Can only be specified if lastObjectStorePosition is defined. * * Must either be undefined or within range. */ @@ -92,8 +71,6 @@ export interface RecordGetRequest { /** * If specified, the index key of the results must be * greater or equal to advanceIndexKey. - * - * Only applicable if indexName is specified. */ advanceIndexKey?: IDBValidKey; /** @@ -109,7 +86,31 @@ export interface RecordGetRequest { resultLevel: ResultLevel; } -/** @public */ +export interface ObjectStoreGetQuery { + direction: IDBCursorDirection; + objectStoreName: string; + /** + * The range of keys to return. + * Refers to the object store keys. + */ + range: BridgeIDBKeyRange | undefined | null; + /** + * Last position in terms of the object store key. + */ + lastObjectStorePosition?: IDBValidKey; + /** + * If specified, the primary key of the results must be greater + * or equal to advancePrimaryKey. + */ + advancePrimaryKey?: IDBValidKey; + /** + * Maximum number of results to return. + * If 0, return all available results + */ + limit: number; + resultLevel: ResultLevel; +} + export interface RecordGetResponse { values: any[] | undefined; indexKeys: IDBValidKey[] | undefined; @@ -117,7 +118,6 @@ export interface RecordGetResponse { count: number; } -/** @public */ export interface RecordStoreRequest { objectStoreName: string; value: any; @@ -125,7 +125,6 @@ export interface RecordStoreRequest { storeLevel: StoreLevel; } -/** @public */ export interface RecordStoreResponse { /** * Key that the record was stored under in the object store. @@ -133,38 +132,79 @@ export interface RecordStoreResponse { key: IDBValidKey; } -/** @public */ +export interface ObjectStoreMeta { + indexSet: string[]; + keyPath: string | string[] | null; + autoIncrement: boolean; +} + +export interface IndexMeta { + keyPath: string | string[]; + multiEntry: boolean; + unique: boolean; +} + +// FIXME: Instead of refering to an object store by name, +// maybe refer to it via some internal, numeric ID? +// This would simplify renaming. export interface Backend { getDatabases(): Promise<BridgeIDBDatabaseInfo[]>; - connectDatabase(name: string): Promise<DatabaseConnection>; + connectDatabase(name: string): Promise<ConnectResult>; beginTransaction( - conn: DatabaseConnection, + dbConn: DatabaseConnection, objectStores: string[], mode: IDBTransactionMode, ): Promise<DatabaseTransaction>; enterVersionChange( - conn: DatabaseConnection, + dbConn: DatabaseConnection, newVersion: number, ): Promise<DatabaseTransaction>; deleteDatabase(name: string): Promise<void>; - close(db: DatabaseConnection): Promise<void>; + close(dbConn: DatabaseConnection): Promise<void>; - getSchema(db: DatabaseConnection): Schema; + // FIXME: Use this for connection + // prepareConnect() - acquires a lock, maybe enters a version change transaction? + // finishConnect() - after possible versionchange is done, allow others to connect - getCurrentTransactionSchema(btx: DatabaseTransaction): Schema; + /** + * Get metadata for an object store. + * + * When dbConn is running a version change transaction, + * the current schema (and not the initial schema) is returned. + * + * Caller may mutate the result, a new object + * is returned on each call. + */ + getObjectStoreMeta( + dbConn: DatabaseConnection, + objectStoreName: string, + ): ObjectStoreMeta | undefined; - getInitialTransactionSchema(btx: DatabaseTransaction): Schema; + /** + * Get metadata for an index. + * + * When dbConn is running a version change transaction, + * the current schema (and not the initial schema) is returned. + * + * Caller may mutate the result, a new object + * is returned on each call. + */ + getIndexMeta( + dbConn: DatabaseConnection, + objectStoreName: string, + indexName: string, + ): IndexMeta | undefined; renameIndex( btx: DatabaseTransaction, objectStoreName: string, - oldName: string, - newName: string, + oldIndexName: string, + newIndexName: string, ): void; deleteIndex( @@ -173,8 +213,9 @@ export interface Backend { indexName: string, ): void; - rollback(btx: DatabaseTransaction): Promise<void>; + rollback(btx: DatabaseTransaction): void; + // FIXME: Should probably not be async commit(btx: DatabaseTransaction): Promise<void>; deleteObjectStore(btx: DatabaseTransaction, name: string): void; @@ -207,9 +248,14 @@ export interface Backend { range: BridgeIDBKeyRange, ): Promise<void>; - getRecords( + getObjectStoreRecords( + btx: DatabaseTransaction, + req: ObjectStoreGetQuery, + ): Promise<RecordGetResponse>; + + getIndexRecords( btx: DatabaseTransaction, - req: RecordGetRequest, + req: IndexGetQuery, ): Promise<RecordGetResponse>; storeRecord( |