diff options
Diffstat (limited to 'packages/idb-bridge/src/backend-interface.ts')
-rw-r--r-- | packages/idb-bridge/src/backend-interface.ts | 148 |
1 files changed, 97 insertions, 51 deletions
diff --git a/packages/idb-bridge/src/backend-interface.ts b/packages/idb-bridge/src/backend-interface.ts index ae43c9df7..690f92f54 100644 --- a/packages/idb-bridge/src/backend-interface.ts +++ b/packages/idb-bridge/src/backend-interface.ts @@ -14,73 +14,52 @@ permissions and limitations under the License. */ -import { BridgeIDBDatabaseInfo, BridgeIDBKeyRange } from "./bridge-idb"; +import { BridgeIDBDatabaseInfo, BridgeIDBKeyRange } from "./bridge-idb.js"; import { IDBCursorDirection, IDBTransactionMode, IDBValidKey, -} from "./idbtypes"; +} 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; /** @@ -103,13 +80,37 @@ export interface RecordGetRequest { advancePrimaryKey?: IDBValidKey; /** * Maximum number of results to return. - * If -1, return all available results + * If 0, return all available results + */ + limit: number; + resultLevel: ResultLevel; +} + +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; } -/** @public */ 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 referring 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( |