idb: make put/add return the effective store key
This commit is contained in:
parent
cc4e8ddc85
commit
92b04858a3
@ -50,7 +50,6 @@ import {
|
|||||||
} from "./backend-interface";
|
} from "./backend-interface";
|
||||||
import BridgeIDBFactory from "./BridgeIDBFactory";
|
import BridgeIDBFactory from "./BridgeIDBFactory";
|
||||||
|
|
||||||
|
|
||||||
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#object-store
|
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#object-store
|
||||||
class BridgeIDBObjectStore {
|
class BridgeIDBObjectStore {
|
||||||
_indexesCache: Map<string, BridgeIDBIndex> = new Map();
|
_indexesCache: Map<string, BridgeIDBIndex> = new Map();
|
||||||
@ -62,7 +61,9 @@ class BridgeIDBObjectStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get indexNames(): FakeDOMStringList {
|
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 {
|
get keyPath(): KeyPath | null {
|
||||||
@ -112,7 +113,6 @@ class BridgeIDBObjectStore {
|
|||||||
|
|
||||||
let { btx } = this._confirmActiveTransaction();
|
let { btx } = this._confirmActiveTransaction();
|
||||||
|
|
||||||
|
|
||||||
newName = String(newName);
|
newName = String(newName);
|
||||||
|
|
||||||
const oldName = this._name;
|
const oldName = this._name;
|
||||||
@ -122,7 +122,9 @@ class BridgeIDBObjectStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this._backend.renameObjectStore(btx, oldName, newName);
|
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) {
|
public _store(value: Value, key: Key | undefined, overwrite: boolean) {
|
||||||
@ -134,12 +136,15 @@ class BridgeIDBObjectStore {
|
|||||||
}
|
}
|
||||||
const operation = async () => {
|
const operation = async () => {
|
||||||
const { btx } = this._confirmActiveTransaction();
|
const { btx } = this._confirmActiveTransaction();
|
||||||
return this._backend.storeRecord(btx, {
|
const result = await this._backend.storeRecord(btx, {
|
||||||
objectStoreName: this._name,
|
objectStoreName: this._name,
|
||||||
key: key,
|
key: key,
|
||||||
value: value,
|
value: value,
|
||||||
storeLevel: overwrite ? StoreLevel.AllowOverwrite : StoreLevel.NoOverwrite,
|
storeLevel: overwrite
|
||||||
|
? StoreLevel.AllowOverwrite
|
||||||
|
: StoreLevel.NoOverwrite,
|
||||||
});
|
});
|
||||||
|
return result.key;
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.transaction._execRequestAsync({ operation, source: this });
|
return this.transaction._execRequestAsync({ operation, source: this });
|
||||||
@ -179,8 +184,8 @@ class BridgeIDBObjectStore {
|
|||||||
const operation = async () => {
|
const operation = async () => {
|
||||||
const { btx } = this._confirmActiveTransaction();
|
const { btx } = this._confirmActiveTransaction();
|
||||||
return this._backend.deleteRecord(btx, this._name, keyRange);
|
return this._backend.deleteRecord(btx, this._name, keyRange);
|
||||||
}
|
};
|
||||||
|
|
||||||
return this.transaction._execRequestAsync({
|
return this.transaction._execRequestAsync({
|
||||||
operation,
|
operation,
|
||||||
source: this,
|
source: this,
|
||||||
@ -220,10 +225,7 @@ class BridgeIDBObjectStore {
|
|||||||
console.log("running get operation:", recordRequest);
|
console.log("running get operation:", recordRequest);
|
||||||
}
|
}
|
||||||
const { btx } = this._confirmActiveTransaction();
|
const { btx } = this._confirmActiveTransaction();
|
||||||
const result = await this._backend.getRecords(
|
const result = await this._backend.getRecords(btx, recordRequest);
|
||||||
btx,
|
|
||||||
recordRequest,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (BridgeIDBFactory.enableTracing) {
|
if (BridgeIDBFactory.enableTracing) {
|
||||||
console.log("get operation result count:", result.count);
|
console.log("get operation result count:", result.count);
|
||||||
@ -268,7 +270,6 @@ class BridgeIDBObjectStore {
|
|||||||
range?: BridgeIDBKeyRange | Key,
|
range?: BridgeIDBKeyRange | Key,
|
||||||
direction: BridgeIDBCursorDirection = "next",
|
direction: BridgeIDBCursorDirection = "next",
|
||||||
) {
|
) {
|
||||||
|
|
||||||
if (range === null) {
|
if (range === null) {
|
||||||
range = undefined;
|
range = undefined;
|
||||||
}
|
}
|
||||||
@ -427,7 +428,6 @@ class BridgeIDBObjectStore {
|
|||||||
|
|
||||||
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBObjectStore-count-IDBRequest-any-key
|
// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#widl-IDBObjectStore-count-IDBRequest-any-key
|
||||||
public count(key?: Key | BridgeIDBKeyRange) {
|
public count(key?: Key | BridgeIDBKeyRange) {
|
||||||
|
|
||||||
if (key === null) {
|
if (key === null) {
|
||||||
key = undefined;
|
key = undefined;
|
||||||
}
|
}
|
||||||
@ -448,10 +448,7 @@ class BridgeIDBObjectStore {
|
|||||||
|
|
||||||
const operation = async () => {
|
const operation = async () => {
|
||||||
const { btx } = this._confirmActiveTransaction();
|
const { btx } = this._confirmActiveTransaction();
|
||||||
const result = await this._backend.getRecords(
|
const result = await this._backend.getRecords(btx, recordGetRequest);
|
||||||
btx,
|
|
||||||
recordGetRequest,
|
|
||||||
);
|
|
||||||
return result.count;
|
return result.count;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ import {
|
|||||||
RecordGetResponse,
|
RecordGetResponse,
|
||||||
ResultLevel,
|
ResultLevel,
|
||||||
StoreLevel,
|
StoreLevel,
|
||||||
|
RecordStoreResponse,
|
||||||
} from "./backend-interface";
|
} from "./backend-interface";
|
||||||
import structuredClone from "./util/structuredClone";
|
import structuredClone from "./util/structuredClone";
|
||||||
import {
|
import {
|
||||||
@ -1094,7 +1095,7 @@ export class MemoryBackend implements Backend {
|
|||||||
async storeRecord(
|
async storeRecord(
|
||||||
btx: DatabaseTransaction,
|
btx: DatabaseTransaction,
|
||||||
storeReq: RecordStoreRequest,
|
storeReq: RecordStoreRequest,
|
||||||
): Promise<void> {
|
): Promise<RecordStoreResponse> {
|
||||||
if (this.enableTracing) {
|
if (this.enableTracing) {
|
||||||
console.log(`TRACING: storeRecord`);
|
console.log(`TRACING: storeRecord`);
|
||||||
}
|
}
|
||||||
@ -1166,6 +1167,8 @@ export class MemoryBackend implements Backend {
|
|||||||
const indexProperties = schema.indexes[indexName];
|
const indexProperties = schema.indexes[indexName];
|
||||||
this.insertIntoIndex(index, key, value, indexProperties);
|
this.insertIntoIndex(index, key, value, indexProperties);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return { key };
|
||||||
}
|
}
|
||||||
|
|
||||||
private insertIntoIndex(
|
private insertIntoIndex(
|
||||||
|
@ -103,6 +103,13 @@ export interface RecordStoreRequest {
|
|||||||
storeLevel: StoreLevel;
|
storeLevel: StoreLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface RecordStoreResponse {
|
||||||
|
/**
|
||||||
|
* Key that the record was stored under in the object store.
|
||||||
|
*/
|
||||||
|
key: Key;
|
||||||
|
}
|
||||||
|
|
||||||
export interface Backend {
|
export interface Backend {
|
||||||
getDatabases(): Promise<BridgeIDBDatabaseInfo[]>;
|
getDatabases(): Promise<BridgeIDBDatabaseInfo[]>;
|
||||||
|
|
||||||
@ -176,5 +183,5 @@ export interface Backend {
|
|||||||
storeRecord(
|
storeRecord(
|
||||||
btx: DatabaseTransaction,
|
btx: DatabaseTransaction,
|
||||||
storeReq: RecordStoreRequest,
|
storeReq: RecordStoreRequest,
|
||||||
): Promise<void>;
|
): Promise<RecordStoreResponse>;
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"target": "es5",
|
"target": "es5",
|
||||||
"noImplicitAny": true,
|
"noImplicitAny": true,
|
||||||
"sourceMap": false,
|
|
||||||
"outDir": "build",
|
"outDir": "build",
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
"noEmitOnError": true,
|
"noEmitOnError": true,
|
||||||
|
Loading…
Reference in New Issue
Block a user