aboutsummaryrefslogtreecommitdiff
path: root/packages/idb-bridge
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2019-08-01 23:21:05 +0200
committerFlorian Dold <florian.dold@gmail.com>2019-08-01 23:21:05 +0200
commit92b04858a3dcc98b8d252e69a06c8ee2f1745394 (patch)
tree4747f8bff80bf2f346e7016e1675d43caf60fb50 /packages/idb-bridge
parentcc4e8ddc85d36f29a7385a7f4eb08c77f46b3af6 (diff)
idb: make put/add return the effective store key
Diffstat (limited to 'packages/idb-bridge')
-rw-r--r--packages/idb-bridge/src/BridgeIDBObjectStore.ts33
-rw-r--r--packages/idb-bridge/src/MemoryBackend.ts5
-rw-r--r--packages/idb-bridge/src/backend-interface.ts9
-rw-r--r--packages/idb-bridge/tsconfig.json1
4 files changed, 27 insertions, 21 deletions
diff --git a/packages/idb-bridge/src/BridgeIDBObjectStore.ts b/packages/idb-bridge/src/BridgeIDBObjectStore.ts
index af5f80511..b951463ad 100644
--- a/packages/idb-bridge/src/BridgeIDBObjectStore.ts
+++ b/packages/idb-bridge/src/BridgeIDBObjectStore.ts
@@ -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,8 +184,8 @@ class BridgeIDBObjectStore {
const operation = async () => {
const { btx } = this._confirmActiveTransaction();
return this._backend.deleteRecord(btx, this._name, keyRange);
- }
-
+ };
+
return this.transaction._execRequestAsync({
operation,
source: this,
@@ -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;
};
diff --git a/packages/idb-bridge/src/MemoryBackend.ts b/packages/idb-bridge/src/MemoryBackend.ts
index a31adb826..5e1587494 100644
--- a/packages/idb-bridge/src/MemoryBackend.ts
+++ b/packages/idb-bridge/src/MemoryBackend.ts
@@ -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(
diff --git a/packages/idb-bridge/src/backend-interface.ts b/packages/idb-bridge/src/backend-interface.ts
index 7329ed96a..f4feac957 100644
--- a/packages/idb-bridge/src/backend-interface.ts
+++ b/packages/idb-bridge/src/backend-interface.ts
@@ -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>;
}
diff --git a/packages/idb-bridge/tsconfig.json b/packages/idb-bridge/tsconfig.json
index 9f076388e..d3a746e1d 100644
--- a/packages/idb-bridge/tsconfig.json
+++ b/packages/idb-bridge/tsconfig.json
@@ -4,7 +4,6 @@
"module": "commonjs",
"target": "es5",
"noImplicitAny": true,
- "sourceMap": false,
"outDir": "build",
"declaration": true,
"noEmitOnError": true,