@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/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,11 @@ 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
+ console.log("delete", existingTables.difference(tableNames));
419
+ existingTables.difference(tableNames).forEach((name) => db.deleteObjectStore(name));
420
+ console.log("create", tableNames.difference(existingTables));
421
+ tableNames.difference(existingTables).forEach((name) => db.createObjectStore(name));
422
422
  };
423
423
  });
424
424
  }
@@ -426,7 +426,7 @@ class Database {
426
426
  return this.tables.some((t) => typeof t === "string" ? name === t : name === t.name);
427
427
  }
428
428
  table(name) {
429
- return new Table(this, name);
429
+ return new Table(this, name.toString());
430
430
  }
431
431
  }
432
432
  class Table {
@@ -434,10 +434,10 @@ class Table {
434
434
  this.database = database;
435
435
  this.name = name;
436
436
  }
437
- async tx(schema, fn2, readonly = false) {
437
+ async tx(table, fn2, readonly = false) {
438
438
  const db = await this.database.connection;
439
- const tx = db.transaction(schema, readonly ? "readonly" : "readwrite");
440
- const store = tx.objectStore(schema);
439
+ const tx = db.transaction(table, readonly ? "readonly" : "readwrite");
440
+ const store = tx.objectStore(table);
441
441
  return new Promise((resolve, reject) => {
442
442
  const request = fn2(store);
443
443
  request.onsuccess = () => resolve(request.result);