@ztimson/utils 0.25.3 → 0.25.5

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/index.mjs CHANGED
@@ -400,10 +400,11 @@ class Database {
400
400
  this.version = version;
401
401
  this.connection = new Promise((resolve, reject) => {
402
402
  const req = indexedDB.open(this.database, this.version);
403
+ const tableNames = new ASet(tables.map((t) => (typeof t == "object" ? t.name : t).toString()));
403
404
  req.onerror = () => reject(req.error);
404
405
  req.onsuccess = () => {
405
406
  const db = req.result;
406
- if (tables.find((s) => !db.objectStoreNames.contains(typeof s === "string" ? s : s.name))) {
407
+ if (tableNames.symmetricDifference(new ASet(Array.from(db.objectStoreNames))).length) {
407
408
  db.close();
408
409
  Object.assign(this, new Database(this.database, this.tables, db.version + 1));
409
410
  } else {
@@ -413,12 +414,9 @@ class Database {
413
414
  };
414
415
  req.onupgradeneeded = () => {
415
416
  const db = req.result;
416
- Array.from(db.objectStoreNames).filter((s) => !this.tables.find((t) => typeof t === "string" ? t : t.name == s)).forEach((name) => db.deleteObjectStore(name));
417
- tables.filter((t) => !db.objectStoreNames.contains(typeof t === "string" ? t : t.name)).forEach((t) => {
418
- db.createObjectStore(typeof t === "string" ? t : t.name, {
419
- keyPath: typeof t === "string" ? void 0 : t.key
420
- });
421
- });
417
+ const existingTables = new ASet(Array.from(db.objectStoreNames));
418
+ existingTables.difference(tableNames).forEach((name) => db.deleteObjectStore(name));
419
+ tableNames.difference(existingTables).forEach((name) => db.createObjectStore(name));
422
420
  };
423
421
  });
424
422
  }
@@ -426,7 +424,7 @@ class Database {
426
424
  return this.tables.some((t) => typeof t === "string" ? name === t : name === t.name);
427
425
  }
428
426
  table(name) {
429
- return new Table(this, name);
427
+ return new Table(this, name.toString());
430
428
  }
431
429
  }
432
430
  class Table {
@@ -434,10 +432,10 @@ class Table {
434
432
  this.database = database;
435
433
  this.name = name;
436
434
  }
437
- async tx(schema, fn2, readonly = false) {
435
+ async tx(table, fn2, readonly = false) {
438
436
  const db = await this.database.connection;
439
- const tx = db.transaction(schema, readonly ? "readonly" : "readwrite");
440
- const store = tx.objectStore(schema);
437
+ const tx = db.transaction(table, readonly ? "readonly" : "readwrite");
438
+ const store = tx.objectStore(table);
441
439
  return new Promise((resolve, reject) => {
442
440
  const request = fn2(store);
443
441
  request.onsuccess = () => resolve(request.result);