@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.
@@ -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: string): Table<K, T>;
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>(schema: string, fn: (store: IDBObjectStore) => IDBRequest, readonly?: boolean): Promise<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 (tables.find((s) => !db.objectStoreNames.contains(typeof s === "string" ? s : s.name))) {
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
- Array.from(db.objectStoreNames).filter((s) => !this.tables.find((t) => typeof t === "string" ? t : t.name == s)).forEach((name) => db.deleteObjectStore(name));
421
- tables.filter((t) => !db.objectStoreNames.contains(typeof t === "string" ? t : t.name)).forEach((t) => {
422
- db.createObjectStore(typeof t === "string" ? t : t.name, {
423
- keyPath: typeof t === "string" ? void 0 : t.key
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(schema, fn2, readonly = false) {
441
+ async tx(table, fn2, readonly = false) {
442
442
  const db = await this.database.connection;
443
- const tx = db.transaction(schema, readonly ? "readonly" : "readwrite");
444
- const store = tx.objectStore(schema);
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);