@ztimson/utils 0.25.2 → 0.25.3

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.d.ts CHANGED
@@ -4,7 +4,7 @@ export * from './aset';
4
4
  export * from './cache';
5
5
  export * from './color';
6
6
  export * from './csv';
7
- export * from './collection';
7
+ export * from './database';
8
8
  export * from './files';
9
9
  export * from './emitter';
10
10
  export * from './errors';
package/dist/index.mjs CHANGED
@@ -392,28 +392,52 @@ function makeUnique(arr) {
392
392
  function makeArray(value) {
393
393
  return Array.isArray(value) ? value : [value];
394
394
  }
395
- class Collection {
396
- constructor(db, collection, version, setup) {
397
- __publicField(this, "database");
398
- this.db = db;
399
- this.collection = collection;
395
+ class Database {
396
+ constructor(database, tables, version) {
397
+ __publicField(this, "connection");
398
+ this.database = database;
399
+ this.tables = tables;
400
400
  this.version = version;
401
- this.setup = setup;
402
- this.database = new Promise((resolve, reject) => {
403
- const req = indexedDB.open(this.db, this.version);
401
+ this.connection = new Promise((resolve, reject) => {
402
+ const req = indexedDB.open(this.database, this.version);
404
403
  req.onerror = () => reject(req.error);
405
- req.onsuccess = () => resolve(req.result);
406
- req.onupgradeneeded = (event) => {
407
- const db2 = req.result;
408
- if (!db2.objectStoreNames.contains(collection)) db2.createObjectStore(collection, { keyPath: void 0 });
409
- if (this.setup) this.setup(db2, event);
404
+ req.onsuccess = () => {
405
+ const db = req.result;
406
+ if (tables.find((s) => !db.objectStoreNames.contains(typeof s === "string" ? s : s.name))) {
407
+ db.close();
408
+ Object.assign(this, new Database(this.database, this.tables, db.version + 1));
409
+ } else {
410
+ this.version = db.version;
411
+ resolve(db);
412
+ }
413
+ };
414
+ req.onupgradeneeded = () => {
415
+ 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
+ });
410
422
  };
411
423
  });
412
424
  }
413
- async tx(collection, fn2, readonly) {
414
- const db = await this.database;
415
- const tx = db.transaction(collection, readonly ? "readonly" : "readwrite");
416
- const store = tx.objectStore(collection);
425
+ includes(name) {
426
+ return this.tables.some((t) => typeof t === "string" ? name === t : name === t.name);
427
+ }
428
+ table(name) {
429
+ return new Table(this, name);
430
+ }
431
+ }
432
+ class Table {
433
+ constructor(database, name) {
434
+ this.database = database;
435
+ this.name = name;
436
+ }
437
+ async tx(schema, fn2, readonly = false) {
438
+ const db = await this.database.connection;
439
+ const tx = db.transaction(schema, readonly ? "readonly" : "readwrite");
440
+ const store = tx.objectStore(schema);
417
441
  return new Promise((resolve, reject) => {
418
442
  const request = fn2(store);
419
443
  request.onsuccess = () => resolve(request.result);
@@ -421,28 +445,28 @@ class Collection {
421
445
  });
422
446
  }
423
447
  add(value, key) {
424
- return this.tx(this.collection, (store) => store.add(value, key));
448
+ return this.tx(this.name, (store) => store.add(value, key));
425
449
  }
426
450
  count() {
427
- return this.tx(this.collection, (store) => store.count(), true);
451
+ return this.tx(this.name, (store) => store.count(), true);
428
452
  }
429
453
  put(key, value) {
430
- return this.tx(this.collection, (store) => store.put(value, key));
454
+ return this.tx(this.name, (store) => store.put(value, key));
431
455
  }
432
456
  getAll() {
433
- return this.tx(this.collection, (store) => store.getAll(), true);
457
+ return this.tx(this.name, (store) => store.getAll(), true);
434
458
  }
435
459
  getAllKeys() {
436
- return this.tx(this.collection, (store) => store.getAllKeys(), true);
460
+ return this.tx(this.name, (store) => store.getAllKeys(), true);
437
461
  }
438
462
  get(key) {
439
- return this.tx(this.collection, (store) => store.get(key), true);
463
+ return this.tx(this.name, (store) => store.get(key), true);
440
464
  }
441
465
  delete(key) {
442
- return this.tx(this.collection, (store) => store.delete(key));
466
+ return this.tx(this.name, (store) => store.delete(key));
443
467
  }
444
468
  clear() {
445
- return this.tx(this.collection, (store) => store.clear());
469
+ return this.tx(this.name, (store) => store.clear());
446
470
  }
447
471
  }
448
472
  class Cache {
@@ -465,10 +489,13 @@ class Cache {
465
489
  this.options = options;
466
490
  if (options.storageKey && !options.storage && typeof Storage !== "undefined") options.storage = localStorage;
467
491
  if (options.storage) {
468
- if (options.storage instanceof Collection) {
492
+ if (options.storage instanceof Table) {
469
493
  (async () => {
470
494
  var _a2;
471
- (await ((_a2 = options.storage) == null ? void 0 : _a2.getAll())).forEach((v) => this.add(v));
495
+ return (await ((_a2 = options.storage) == null ? void 0 : _a2.getAll())).forEach((v) => {
496
+ console.log(v);
497
+ this.add(v);
498
+ });
472
499
  })();
473
500
  } else if (options.storageKey) {
474
501
  const stored = (_a = options.storage) == null ? void 0 : _a.getItem(options.storageKey);
@@ -496,7 +523,7 @@ class Cache {
496
523
  }
497
524
  save(key) {
498
525
  if (this.options.storage) {
499
- if (this.options.storage instanceof Collection) {
526
+ if (this.options.storage instanceof Table) {
500
527
  this.options.storage.put(key, this.store[key]);
501
528
  } else if (this.options.storageKey) {
502
529
  this.options.storage.setItem(this.options.storageKey, JSONSanitize(this.store));
@@ -563,13 +590,16 @@ class Cache {
563
590
  */
564
591
  expire(key) {
565
592
  this.complete = false;
566
- if (this.options.expiryPolicy == "keep") this.store[key]._expired = true;
567
- else this.delete(key);
593
+ if (this.options.expiryPolicy == "keep") {
594
+ this.store[key]._expired = true;
595
+ this.save(key);
596
+ } else this.delete(key);
568
597
  return this;
569
598
  }
570
599
  /**
571
600
  * Get item from the cache
572
601
  * @param {K} key Key to lookup
602
+ * @param expired Include expired items
573
603
  * @return {T} Cached item
574
604
  */
575
605
  get(key, expired) {
@@ -1998,8 +2028,8 @@ export {
1998
2028
  CliBackground,
1999
2029
  CliEffects,
2000
2030
  CliForeground,
2001
- Collection,
2002
2031
  CustomError,
2032
+ Database,
2003
2033
  ForbiddenError,
2004
2034
  GatewayTimeoutError,
2005
2035
  Http,
@@ -2024,6 +2054,7 @@ export {
2024
2054
  PromiseProgress,
2025
2055
  SYMBOL_LIST,
2026
2056
  ServiceUnavailableError,
2057
+ Table,
2027
2058
  TypedEmitter,
2028
2059
  UnauthorizedError,
2029
2060
  addUnique,