idb: make put/add return the effective store key

This commit is contained in:
Florian Dold 2019-08-01 23:21:05 +02:00
parent cc4e8ddc85
commit 92b04858a3
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
4 changed files with 27 additions and 21 deletions

View File

@ -50,7 +50,6 @@ import {
} from "./backend-interface";
import BridgeIDBFactory from "./BridgeIDBFactory";
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#object-store
class BridgeIDBObjectStore {
_indexesCache: Map<string, BridgeIDBIndex> = new Map();
@ -62,7 +61,9 @@ class BridgeIDBObjectStore {
}
get indexNames(): FakeDOMStringList {
return fakeDOMStringList(this._schema.objectStores[this._name].indexes).sort();
return fakeDOMStringList(
this._schema.objectStores[this._name].indexes,
).sort();
}
get keyPath(): KeyPath | null {
@ -112,7 +113,6 @@ class BridgeIDBObjectStore {
let { btx } = this._confirmActiveTransaction();
newName = String(newName);
const oldName = this._name;
@ -122,7 +122,9 @@ class BridgeIDBObjectStore {
}
this._backend.renameObjectStore(btx, oldName, newName);
this.transaction.db._schema = this._backend.getSchema(this._backendConnection);
this.transaction.db._schema = this._backend.getSchema(
this._backendConnection,
);
}
public _store(value: Value, key: Key | undefined, overwrite: boolean) {
@ -134,12 +136,15 @@ class BridgeIDBObjectStore {
}
const operation = async () => {
const { btx } = this._confirmActiveTransaction();
return this._backend.storeRecord(btx, {
const result = await this._backend.storeRecord(btx, {
objectStoreName: this._name,
key: key,
value: value,
storeLevel: overwrite ? StoreLevel.AllowOverwrite : StoreLevel.NoOverwrite,
storeLevel: overwrite
? StoreLevel.AllowOverwrite
: StoreLevel.NoOverwrite,
});
return result.key;
};
return this.transaction._execRequestAsync({ operation, source: this });
@ -179,7 +184,7 @@ class BridgeIDBObjectStore {
const operation = async () => {
const { btx } = this._confirmActiveTransaction();
return this._backend.deleteRecord(btx, this._name, keyRange);
}
};
return this.transaction._execRequestAsync({
operation,
@ -220,10 +225,7 @@ class BridgeIDBObjectStore {
console.log("running get operation:", recordRequest);
}
const { btx } = this._confirmActiveTransaction();
const result = await this._backend.getRecords(
btx,
recordRequest,
);
const result = await this._backend.getRecords(btx, recordRequest);
if (BridgeIDBFactory.enableTracing) {
console.log("get operation result count:", result.count);
@ -268,7 +270,6 @@ class BridgeIDBObjectStore {
range?: BridgeIDBKeyRange | Key,
direction: BridgeIDBCursorDirection = "next",
) {
if (range === null) {
range = undefined;
}
@ -427,7 +428,6 @@ class BridgeIDBObjectStore {
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBObjectStore-count-IDBRequest-any-key
public count(key?: Key | BridgeIDBKeyRange) {
if (key === null) {
key = undefined;
}
@ -448,10 +448,7 @@ class BridgeIDBObjectStore {
const operation = async () => {
const { btx } = this._confirmActiveTransaction();
const result = await this._backend.getRecords(
btx,
recordGetRequest,
);
const result = await this._backend.getRecords(btx, recordGetRequest);
return result.count;
};

View File

@ -9,6 +9,7 @@ import {
RecordGetResponse,
ResultLevel,
StoreLevel,
RecordStoreResponse,
} from "./backend-interface";
import structuredClone from "./util/structuredClone";
import {
@ -1094,7 +1095,7 @@ export class MemoryBackend implements Backend {
async storeRecord(
btx: DatabaseTransaction,
storeReq: RecordStoreRequest,
): Promise<void> {
): Promise<RecordStoreResponse> {
if (this.enableTracing) {
console.log(`TRACING: storeRecord`);
}
@ -1166,6 +1167,8 @@ export class MemoryBackend implements Backend {
const indexProperties = schema.indexes[indexName];
this.insertIntoIndex(index, key, value, indexProperties);
}
return { key };
}
private insertIntoIndex(

View File

@ -103,6 +103,13 @@ export interface RecordStoreRequest {
storeLevel: StoreLevel;
}
export interface RecordStoreResponse {
/**
* Key that the record was stored under in the object store.
*/
key: Key;
}
export interface Backend {
getDatabases(): Promise<BridgeIDBDatabaseInfo[]>;
@ -176,5 +183,5 @@ export interface Backend {
storeRecord(
btx: DatabaseTransaction,
storeReq: RecordStoreRequest,
): Promise<void>;
): Promise<RecordStoreResponse>;
}

View File

@ -4,7 +4,6 @@
"module": "commonjs",
"target": "es5",
"noImplicitAny": true,
"sourceMap": false,
"outDir": "build",
"declaration": true,
"noEmitOnError": true,