@ztimson/utils 0.25.3 → 0.25.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/database.d.ts +2 -2
- package/dist/index.cjs +11 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +11 -11
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/database.d.ts
CHANGED
|
@@ -9,13 +9,13 @@ export declare class Database {
|
|
|
9
9
|
connection: Promise<IDBDatabase>;
|
|
10
10
|
constructor(database: string, tables: (string | TableOptions)[], version?: number | undefined);
|
|
11
11
|
includes(name: string): boolean;
|
|
12
|
-
table<K extends IDBValidKey = any, T = any>(name:
|
|
12
|
+
table<K extends IDBValidKey = any, T = any>(name: any): Table<K, T>;
|
|
13
13
|
}
|
|
14
14
|
export declare class Table<K extends IDBValidKey = any, T = any> {
|
|
15
15
|
private readonly database;
|
|
16
16
|
readonly name: string;
|
|
17
17
|
constructor(database: Database, name: string);
|
|
18
|
-
tx<R>(
|
|
18
|
+
tx<R>(table: string, fn: (store: IDBObjectStore) => IDBRequest, readonly?: boolean): Promise<R>;
|
|
19
19
|
add(value: T, key?: K): Promise<void>;
|
|
20
20
|
count(): Promise<number>;
|
|
21
21
|
put(key: K, value: T): Promise<void>;
|
package/dist/index.cjs
CHANGED
|
@@ -404,10 +404,11 @@ ${opts.message || this.desc}`;
|
|
|
404
404
|
this.version = version;
|
|
405
405
|
this.connection = new Promise((resolve, reject) => {
|
|
406
406
|
const req = indexedDB.open(this.database, this.version);
|
|
407
|
+
const tableNames = new ASet(tables.map((t) => (typeof t == "object" ? t.name : t).toString()));
|
|
407
408
|
req.onerror = () => reject(req.error);
|
|
408
409
|
req.onsuccess = () => {
|
|
409
410
|
const db = req.result;
|
|
410
|
-
if (
|
|
411
|
+
if (tableNames.symmetricDifference(new ASet(Array.from(db.objectStoreNames))).length) {
|
|
411
412
|
db.close();
|
|
412
413
|
Object.assign(this, new Database(this.database, this.tables, db.version + 1));
|
|
413
414
|
} else {
|
|
@@ -417,12 +418,11 @@ ${opts.message || this.desc}`;
|
|
|
417
418
|
};
|
|
418
419
|
req.onupgradeneeded = () => {
|
|
419
420
|
const db = req.result;
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
});
|
|
421
|
+
const existingTables = new ASet(Array.from(db.objectStoreNames));
|
|
422
|
+
console.log("delete", existingTables.difference(tableNames));
|
|
423
|
+
existingTables.difference(tableNames).forEach((name) => db.deleteObjectStore(name));
|
|
424
|
+
console.log("create", tableNames.difference(existingTables));
|
|
425
|
+
tableNames.difference(existingTables).forEach((name) => db.createObjectStore(name));
|
|
426
426
|
};
|
|
427
427
|
});
|
|
428
428
|
}
|
|
@@ -430,7 +430,7 @@ ${opts.message || this.desc}`;
|
|
|
430
430
|
return this.tables.some((t) => typeof t === "string" ? name === t : name === t.name);
|
|
431
431
|
}
|
|
432
432
|
table(name) {
|
|
433
|
-
return new Table(this, name);
|
|
433
|
+
return new Table(this, name.toString());
|
|
434
434
|
}
|
|
435
435
|
}
|
|
436
436
|
class Table {
|
|
@@ -438,10 +438,10 @@ ${opts.message || this.desc}`;
|
|
|
438
438
|
this.database = database;
|
|
439
439
|
this.name = name;
|
|
440
440
|
}
|
|
441
|
-
async tx(
|
|
441
|
+
async tx(table, fn2, readonly = false) {
|
|
442
442
|
const db = await this.database.connection;
|
|
443
|
-
const tx = db.transaction(
|
|
444
|
-
const store = tx.objectStore(
|
|
443
|
+
const tx = db.transaction(table, readonly ? "readonly" : "readwrite");
|
|
444
|
+
const store = tx.objectStore(table);
|
|
445
445
|
return new Promise((resolve, reject) => {
|
|
446
446
|
const request = fn2(store);
|
|
447
447
|
request.onsuccess = () => resolve(request.result);
|