@ztimson/utils 0.25.8 → 0.25.9

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
@@ -395,46 +395,88 @@ function makeArray(value) {
395
395
  class Database {
396
396
  constructor(database, tables, version) {
397
397
  __publicField(this, "connection");
398
+ __publicField(this, "ready", false);
398
399
  __publicField(this, "tables");
399
400
  this.database = database;
400
401
  this.version = version;
401
402
  this.connection = new Promise((resolve, reject) => {
402
403
  const req = indexedDB.open(this.database, this.version);
403
- this.tables = tables.map((t) => {
404
+ this.tables = !tables ? [] : tables.map((t) => {
404
405
  t = typeof t == "object" ? t : { name: t };
405
406
  return { ...t, name: t.name.toString() };
406
407
  });
407
- const tableNames = new ASet(this.tables.map((t) => t.name));
408
408
  req.onerror = () => reject(req.error);
409
409
  req.onsuccess = () => {
410
410
  const db = req.result;
411
- if (tableNames.symmetricDifference(new ASet(Array.from(db.objectStoreNames))).length) {
411
+ const existing = Array.from(db.objectStoreNames);
412
+ if (!tables) this.tables = existing.map((t) => {
413
+ const tx = db.transaction(t, "readonly");
414
+ const store = tx.objectStore(t);
415
+ return { name: t, key: store.keyPath };
416
+ });
417
+ const desired = new ASet((tables || []).map((t) => typeof t == "string" ? t : t.name));
418
+ if (tables && desired.symmetricDifference(new ASet(existing)).length) {
412
419
  db.close();
413
420
  Object.assign(this, new Database(this.database, this.tables, db.version + 1));
421
+ this.connection.then(resolve);
414
422
  } else {
415
423
  this.version = db.version;
416
424
  resolve(db);
417
425
  }
426
+ this.ready = true;
418
427
  };
419
428
  req.onupgradeneeded = () => {
420
429
  const db = req.result;
421
430
  const existingTables = new ASet(Array.from(db.objectStoreNames));
422
- existingTables.difference(tableNames).forEach((name) => db.deleteObjectStore(name));
423
- tableNames.difference(existingTables).forEach((name) => db.createObjectStore(name));
431
+ if (tables) {
432
+ const desired = new ASet((tables || []).map((t) => typeof t == "string" ? t : t.name));
433
+ existingTables.difference(desired).forEach((name) => db.deleteObjectStore(name));
434
+ desired.difference(existingTables).forEach((name) => {
435
+ const t = this.tables.find(findByProp("name", name));
436
+ db.createObjectStore(name, {
437
+ keyPath: t == null ? void 0 : t.key,
438
+ autoIncrement: (t == null ? void 0 : t.autoIncrement) || !(t == null ? void 0 : t.key)
439
+ });
440
+ });
441
+ }
424
442
  };
425
443
  });
426
444
  }
445
+ async createTable(table) {
446
+ if (typeof table == "string") table = { name: table };
447
+ const conn = await this.connection;
448
+ if (!this.includes(table.name)) {
449
+ conn.close();
450
+ Object.assign(this, new Database(this.database, [...this.tables, table], (this.version ?? 0) + 1));
451
+ }
452
+ return this.table(table.name);
453
+ }
454
+ async deleteTable(table) {
455
+ if (typeof table == "string") table = { name: table };
456
+ if (!this.includes(table.name)) return;
457
+ const conn = await this.connection;
458
+ conn.close();
459
+ Object.assign(this, new Database(this.database, this.tables.filter((t) => t.name != table.name), (this.version ?? 0) + 1));
460
+ }
427
461
  includes(name) {
428
- return !!this.tables.find((t) => t.name == name.toString());
462
+ return !!this.tables.find((t) => t.name == (typeof name == "object" ? name.name : name.toString()));
429
463
  }
430
464
  table(name) {
431
465
  return new Table(this, name.toString());
432
466
  }
433
467
  }
434
468
  class Table {
435
- constructor(database, name) {
469
+ constructor(database, name, key = "id") {
470
+ __publicField(this, "all", this.getAll);
471
+ __publicField(this, "create", this.add);
472
+ __publicField(this, "update", this.set);
436
473
  this.database = database;
437
474
  this.name = name;
475
+ this.key = key;
476
+ this.database.connection.then(() => {
477
+ const exists = !!this.database.tables.find(findByProp("name", this.name));
478
+ if (!exists) this.database.createTable(this.name);
479
+ });
438
480
  }
439
481
  async tx(table, fn2, readonly = false) {
440
482
  const db = await this.database.connection;
@@ -449,11 +491,17 @@ class Table {
449
491
  add(value, key) {
450
492
  return this.tx(this.name, (store) => store.add(value, key));
451
493
  }
494
+ clear() {
495
+ return this.tx(this.name, (store) => store.clear());
496
+ }
452
497
  count() {
453
498
  return this.tx(this.name, (store) => store.count(), true);
454
499
  }
455
- put(key, value) {
456
- return this.tx(this.name, (store) => store.put(value, key));
500
+ delete(key) {
501
+ return this.tx(this.name, (store) => store.delete(key));
502
+ }
503
+ get(key) {
504
+ return this.tx(this.name, (store) => store.get(key), true);
457
505
  }
458
506
  getAll() {
459
507
  return this.tx(this.name, (store) => store.getAll(), true);
@@ -461,14 +509,15 @@ class Table {
461
509
  getAllKeys() {
462
510
  return this.tx(this.name, (store) => store.getAllKeys(), true);
463
511
  }
464
- get(key) {
465
- return this.tx(this.name, (store) => store.get(key), true);
512
+ put(key, value) {
513
+ return this.tx(this.name, (store) => store.put(value, key));
466
514
  }
467
- delete(key) {
468
- return this.tx(this.name, (store) => store.delete(key));
515
+ read(key) {
516
+ return key ? this.get(key) : this.getAll();
469
517
  }
470
- clear() {
471
- return this.tx(this.name, (store) => store.clear());
518
+ set(value, key) {
519
+ if (!key && !value[this.key]) return this.add(value);
520
+ return this.put(key || value[this.key], value);
472
521
  }
473
522
  }
474
523
  class Cache {