@xylabs/indexed-db 5.0.83 → 5.0.86

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.
Files changed (80) hide show
  1. package/README.md +246 -313
  2. package/dist/browser/IndexDescription.d.ts +1 -0
  3. package/dist/browser/IndexDescription.d.ts.map +1 -1
  4. package/dist/browser/IndexedDbKeyValueStore.d.ts +23 -0
  5. package/dist/browser/IndexedDbKeyValueStore.d.ts.map +1 -1
  6. package/dist/browser/ObjectStore.d.ts +1 -0
  7. package/dist/browser/ObjectStore.d.ts.map +1 -1
  8. package/dist/browser/checkDbNeedsUpgrade.d.ts +7 -0
  9. package/dist/browser/checkDbNeedsUpgrade.d.ts.map +1 -1
  10. package/dist/browser/checkStoreNeedsUpgrade.d.ts +8 -0
  11. package/dist/browser/checkStoreNeedsUpgrade.d.ts.map +1 -1
  12. package/dist/browser/createStoreDuringUpgrade.d.ts +7 -0
  13. package/dist/browser/createStoreDuringUpgrade.d.ts.map +1 -1
  14. package/dist/browser/getExistingIndexes.d.ts +7 -0
  15. package/dist/browser/getExistingIndexes.d.ts.map +1 -1
  16. package/dist/browser/index.mjs +23 -0
  17. package/dist/browser/index.mjs.map +1 -1
  18. package/dist/browser/withDb.d.ts +10 -0
  19. package/dist/browser/withDb.d.ts.map +1 -1
  20. package/dist/browser/withDbByVersion.d.ts +11 -0
  21. package/dist/browser/withDbByVersion.d.ts.map +1 -1
  22. package/dist/browser/withReadOnlyStore.d.ts +8 -0
  23. package/dist/browser/withReadOnlyStore.d.ts.map +1 -1
  24. package/dist/browser/withReadWriteStore.d.ts +8 -0
  25. package/dist/browser/withReadWriteStore.d.ts.map +1 -1
  26. package/dist/browser/withStore.d.ts +10 -0
  27. package/dist/browser/withStore.d.ts.map +1 -1
  28. package/dist/neutral/IndexDescription.d.ts +1 -0
  29. package/dist/neutral/IndexDescription.d.ts.map +1 -1
  30. package/dist/neutral/IndexedDbKeyValueStore.d.ts +23 -0
  31. package/dist/neutral/IndexedDbKeyValueStore.d.ts.map +1 -1
  32. package/dist/neutral/ObjectStore.d.ts +1 -0
  33. package/dist/neutral/ObjectStore.d.ts.map +1 -1
  34. package/dist/neutral/checkDbNeedsUpgrade.d.ts +7 -0
  35. package/dist/neutral/checkDbNeedsUpgrade.d.ts.map +1 -1
  36. package/dist/neutral/checkStoreNeedsUpgrade.d.ts +8 -0
  37. package/dist/neutral/checkStoreNeedsUpgrade.d.ts.map +1 -1
  38. package/dist/neutral/createStoreDuringUpgrade.d.ts +7 -0
  39. package/dist/neutral/createStoreDuringUpgrade.d.ts.map +1 -1
  40. package/dist/neutral/getExistingIndexes.d.ts +7 -0
  41. package/dist/neutral/getExistingIndexes.d.ts.map +1 -1
  42. package/dist/neutral/index.mjs +23 -0
  43. package/dist/neutral/index.mjs.map +1 -1
  44. package/dist/neutral/withDb.d.ts +10 -0
  45. package/dist/neutral/withDb.d.ts.map +1 -1
  46. package/dist/neutral/withDbByVersion.d.ts +11 -0
  47. package/dist/neutral/withDbByVersion.d.ts.map +1 -1
  48. package/dist/neutral/withReadOnlyStore.d.ts +8 -0
  49. package/dist/neutral/withReadOnlyStore.d.ts.map +1 -1
  50. package/dist/neutral/withReadWriteStore.d.ts +8 -0
  51. package/dist/neutral/withReadWriteStore.d.ts.map +1 -1
  52. package/dist/neutral/withStore.d.ts +10 -0
  53. package/dist/neutral/withStore.d.ts.map +1 -1
  54. package/dist/node/IndexDescription.d.ts +1 -0
  55. package/dist/node/IndexDescription.d.ts.map +1 -1
  56. package/dist/node/IndexedDbKeyValueStore.d.ts +23 -0
  57. package/dist/node/IndexedDbKeyValueStore.d.ts.map +1 -1
  58. package/dist/node/ObjectStore.d.ts +1 -0
  59. package/dist/node/ObjectStore.d.ts.map +1 -1
  60. package/dist/node/checkDbNeedsUpgrade.d.ts +7 -0
  61. package/dist/node/checkDbNeedsUpgrade.d.ts.map +1 -1
  62. package/dist/node/checkStoreNeedsUpgrade.d.ts +8 -0
  63. package/dist/node/checkStoreNeedsUpgrade.d.ts.map +1 -1
  64. package/dist/node/createStoreDuringUpgrade.d.ts +7 -0
  65. package/dist/node/createStoreDuringUpgrade.d.ts.map +1 -1
  66. package/dist/node/getExistingIndexes.d.ts +7 -0
  67. package/dist/node/getExistingIndexes.d.ts.map +1 -1
  68. package/dist/node/index.mjs +23 -0
  69. package/dist/node/index.mjs.map +1 -1
  70. package/dist/node/withDb.d.ts +10 -0
  71. package/dist/node/withDb.d.ts.map +1 -1
  72. package/dist/node/withDbByVersion.d.ts +11 -0
  73. package/dist/node/withDbByVersion.d.ts.map +1 -1
  74. package/dist/node/withReadOnlyStore.d.ts +8 -0
  75. package/dist/node/withReadOnlyStore.d.ts.map +1 -1
  76. package/dist/node/withReadWriteStore.d.ts +8 -0
  77. package/dist/node/withReadWriteStore.d.ts.map +1 -1
  78. package/dist/node/withStore.d.ts +10 -0
  79. package/dist/node/withStore.d.ts.map +1 -1
  80. package/package.json +8 -8
@@ -19,6 +19,7 @@ export type IndexDescription = {
19
19
  */
20
20
  unique?: boolean;
21
21
  };
22
+ /** Separator used between key names when building standard index names. */
22
23
  export declare const IndexSeparator = "-";
23
24
  /**
24
25
  * Given an index description, this will build the index
@@ -1 +1 @@
1
- {"version":3,"file":"IndexDescription.d.ts","sourceRoot":"","sources":["../../src/IndexDescription.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,GAAG,CAAC,CAAA;AAEnC;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAA;IACnC;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB,CAAA;AAED,eAAO,MAAM,cAAc,MAAM,CAAA;AAEjC;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,GAAI,OAAO,gBAAgB,WAK7D,CAAA"}
1
+ {"version":3,"file":"IndexDescription.d.ts","sourceRoot":"","sources":["../../src/IndexDescription.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,GAAG,CAAC,CAAA;AAEnC;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAA;IACnC;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB,CAAA;AAED,2EAA2E;AAC3E,eAAO,MAAM,cAAc,MAAM,CAAA;AAEjC;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,GAAI,OAAO,gBAAgB,WAK7D,CAAA"}
@@ -4,14 +4,37 @@ import type { DBSchema, IDBPDatabase, StoreKey, StoreNames, StoreValue } from 'i
4
4
  * An IndexedDB key/value store.
5
5
  */
6
6
  export declare class IndexedDbKeyValueStore<T extends DBSchema, S extends StoreNames<T>> implements KeyValueStore<StoreValue<T, S>, StoreKey<T, S>> {
7
+ /** The name of the IndexedDB database. */
7
8
  readonly dbName: string;
9
+ /** The name of the object store within the database. */
8
10
  readonly storeName: S;
9
11
  constructor(dbName: string, storeName: S);
12
+ /** Removes all entries from the store. */
10
13
  clear?(): Promise<void>;
14
+ /**
15
+ * Deletes the entry with the given key.
16
+ * @param key The key of the entry to delete
17
+ */
11
18
  delete(key: StoreKey<T, S>): Promise<void>;
19
+ /**
20
+ * Retrieves the value associated with the given key.
21
+ * @param key The key to look up
22
+ * @returns The value, or undefined if not found
23
+ */
12
24
  get(key: StoreKey<T, S>): Promise<StoreValue<T, S> | undefined>;
25
+ /** Returns all keys in the store. */
13
26
  keys?(): Promise<StoreKey<T, S>[]>;
27
+ /**
28
+ * Sets a value for the given key, creating or updating the entry.
29
+ * @param key The key to set
30
+ * @param value The value to store
31
+ */
14
32
  set(key: StoreKey<T, S>, value: StoreValue<T, S>): Promise<void>;
33
+ /**
34
+ * Opens the underlying IndexedDB database and passes it to the callback.
35
+ * @param callback Function to execute with the database
36
+ * @returns The result of the callback
37
+ */
15
38
  withDb<R = StoreValue<T, S>>(callback: (db: IDBPDatabase<T>) => Promise<R> | R): Promise<R>;
16
39
  }
17
40
  //# sourceMappingURL=IndexedDbKeyValueStore.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"IndexedDbKeyValueStore.d.ts","sourceRoot":"","sources":["../../src/IndexedDbKeyValueStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AACpD,OAAO,KAAK,EACV,QAAQ,EACR,YAAY,EAAE,QAAQ,EAAE,UAAU,EAAE,UAAU,EAC/C,MAAM,KAAK,CAAA;AAIZ;;GAEG;AACH,qBAAa,sBAAsB,CAAC,CAAC,SAAS,QAAQ,EAAE,CAAC,SAAS,UAAU,CAAC,CAAC,CAAC,CAAE,YAAW,aAAa,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACzI,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,SAAS,EAAE,CAAC,CAAA;gBAET,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;IAKlC,KAAK,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC;IAMvB,MAAM,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAM1C,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC;IAQvB,IAAI,CAAC,IAAI,OAAO,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;IAMlC,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,EAAE,CA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C,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;IAMxC;;;;OAIG;IACG,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAMtE;;;;OAIG;IACG,MAAM,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,EAC/B,QAAQ,EAAE,CAAC,EAAE,EAAE,YAAY,CAAC,CAAC,CAAC,KAC5B,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC;CAMnB"}
@@ -1,4 +1,5 @@
1
1
  import type { EmptyObject } from '@xylabs/object';
2
+ /** Generic IndexedDB schema type that maps store names to their value types. */
2
3
  export interface ObjectStore<T extends EmptyObject = EmptyObject> {
3
4
  [s: string]: T;
4
5
  }
@@ -1 +1 @@
1
- {"version":3,"file":"ObjectStore.d.ts","sourceRoot":"","sources":["../../src/ObjectStore.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAEjD,MAAM,WAAW,WAAW,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW;IAC9D,CAAC,CAAC,EAAE,MAAM,GAAG,CAAC,CAAA;CACf"}
1
+ {"version":3,"file":"ObjectStore.d.ts","sourceRoot":"","sources":["../../src/ObjectStore.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAEjD,gFAAgF;AAChF,MAAM,WAAW,WAAW,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW;IAC9D,CAAC,CAAC,EAAE,MAAM,GAAG,CAAC,CAAA;CACf"}
@@ -1,4 +1,11 @@
1
1
  import type { Logger } from '@xylabs/logger';
2
2
  import { type IndexDescription } from './IndexDescription.ts';
3
+ /**
4
+ * Checks whether any store in the database needs an upgrade and returns the appropriate version number.
5
+ * @param dbName The name of the database to check
6
+ * @param stores Map of store names to their expected index descriptions
7
+ * @param logger Optional logger for diagnostics
8
+ * @returns The version to open (current version + 1 if upgrade needed, otherwise current version)
9
+ */
3
10
  export declare function checkDbNeedsUpgrade(dbName: string, stores: Record<string, IndexDescription[]>, logger?: Logger): Promise<number>;
4
11
  //# sourceMappingURL=checkDbNeedsUpgrade.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"checkDbNeedsUpgrade.d.ts","sourceRoot":"","sources":["../../src/checkDbNeedsUpgrade.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAG5C,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAI7D,wBAAsB,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,EAAE,CAAC,EAAE,MAAM,CAAC,EAAE,MAAM,mBAUpH"}
1
+ {"version":3,"file":"checkDbNeedsUpgrade.d.ts","sourceRoot":"","sources":["../../src/checkDbNeedsUpgrade.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAG5C,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAI7D;;;;;;GAMG;AACH,wBAAsB,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,EAAE,CAAC,EAAE,MAAM,CAAC,EAAE,MAAM,mBAUpH"}
@@ -2,5 +2,13 @@ import type { Logger } from '@xylabs/logger';
2
2
  import type { IDBPDatabase } from 'idb';
3
3
  import { type IndexDescription } from './IndexDescription.ts';
4
4
  import type { ObjectStore } from './ObjectStore.ts';
5
+ /**
6
+ * Checks whether a store needs an upgrade by comparing its existing indexes against expected indexes.
7
+ * @param db The IndexedDB database instance
8
+ * @param storeName The name of the store to check
9
+ * @param indexes The expected index descriptions
10
+ * @param logger Optional logger for diagnostics
11
+ * @returns True if the store is missing or has missing indexes
12
+ */
5
13
  export declare function checkStoreNeedsUpgrade(db: IDBPDatabase<ObjectStore<object>>, storeName: string, indexes: IndexDescription[], logger?: Logger): Promise<boolean>;
6
14
  //# sourceMappingURL=checkStoreNeedsUpgrade.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"checkStoreNeedsUpgrade.d.ts","sourceRoot":"","sources":["../../src/checkStoreNeedsUpgrade.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,KAAK,CAAA;AAGvC,OAAO,EAA0B,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACrF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEnD,wBAAsB,sBAAsB,CAAC,EAAE,EAAE,YAAY,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,EAAE,EAAE,MAAM,CAAC,EAAE,MAAM,oBAelJ"}
1
+ {"version":3,"file":"checkStoreNeedsUpgrade.d.ts","sourceRoot":"","sources":["../../src/checkStoreNeedsUpgrade.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,KAAK,CAAA;AAGvC,OAAO,EAA0B,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACrF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEnD;;;;;;;GAOG;AACH,wBAAsB,sBAAsB,CAAC,EAAE,EAAE,YAAY,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,EAAE,EAAE,MAAM,CAAC,EAAE,MAAM,oBAelJ"}
@@ -1,5 +1,12 @@
1
1
  import type { Logger } from '@xylabs/logger';
2
2
  import type { DBSchema, IDBPDatabase, StoreNames } from 'idb';
3
3
  import { type IndexDescription } from './IndexDescription.ts';
4
+ /**
5
+ * Creates an object store with the specified indexes during a version upgrade transaction.
6
+ * @param db The IndexedDB database instance (during upgrade)
7
+ * @param storeName The name of the store to create
8
+ * @param indexes The index descriptions to create on the store
9
+ * @param logger Optional logger for diagnostics
10
+ */
4
11
  export declare function createStoreDuringUpgrade<DBTypes extends DBSchema | unknown = unknown>(db: IDBPDatabase<DBTypes>, storeName: StoreNames<DBTypes>, indexes: IndexDescription[], logger?: Logger): void;
5
12
  //# sourceMappingURL=createStoreDuringUpgrade.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"createStoreDuringUpgrade.d.ts","sourceRoot":"","sources":["../../src/createStoreDuringUpgrade.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EACV,QAAQ,EACR,YAAY,EAA+B,UAAU,EACtD,MAAM,KAAK,CAAA;AAEZ,OAAO,EAEL,KAAK,gBAAgB,EACtB,MAAM,uBAAuB,CAAA;AAE9B,wBAAgB,wBAAwB,CAAC,OAAO,SAAS,QAAQ,GAAG,OAAO,GAAG,OAAO,EACnF,EAAE,EAAE,YAAY,CAAC,OAAO,CAAC,EACzB,SAAS,EAAE,UAAU,CAAC,OAAO,CAAC,EAC9B,OAAO,EAAE,gBAAgB,EAAE,EAC3B,MAAM,CAAC,EAAE,MAAM,QA4BhB"}
1
+ {"version":3,"file":"createStoreDuringUpgrade.d.ts","sourceRoot":"","sources":["../../src/createStoreDuringUpgrade.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EACV,QAAQ,EACR,YAAY,EAA+B,UAAU,EACtD,MAAM,KAAK,CAAA;AAEZ,OAAO,EAEL,KAAK,gBAAgB,EACtB,MAAM,uBAAuB,CAAA;AAE9B;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,SAAS,QAAQ,GAAG,OAAO,GAAG,OAAO,EACnF,EAAE,EAAE,YAAY,CAAC,OAAO,CAAC,EACzB,SAAS,EAAE,UAAU,CAAC,OAAO,CAAC,EAC9B,OAAO,EAAE,gBAAgB,EAAE,EAC3B,MAAM,CAAC,EAAE,MAAM,QA4BhB"}
@@ -3,5 +3,12 @@ import type { EmptyObject } from '@xylabs/object';
3
3
  import type { IDBPDatabase, StoreNames } from 'idb';
4
4
  import { type IndexDescription } from './IndexDescription.ts';
5
5
  import type { ObjectStore } from './ObjectStore.ts';
6
+ /**
7
+ * Retrieves the existing index descriptions for a store. Accepts either a database instance or a database name.
8
+ * @param db The IndexedDB database instance or database name
9
+ * @param storeName The name of the store to inspect
10
+ * @param logger Optional logger for diagnostics
11
+ * @returns An array of index descriptions, or null if the store does not exist
12
+ */
6
13
  export declare function getExistingIndexes<T extends EmptyObject = EmptyObject>(db: IDBPDatabase<ObjectStore<T>> | string, storeName: StoreNames<ObjectStore<T>>, logger?: Logger): Promise<IndexDescription[] | null>;
7
14
  //# sourceMappingURL=getExistingIndexes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getExistingIndexes.d.ts","sourceRoot":"","sources":["../../src/getExistingIndexes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACjD,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,KAAK,CAAA;AAEnD,OAAO,EACL,KAAK,gBAAgB,EAEtB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAiCnD,wBAAsB,kBAAkB,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,EAC1E,EAAE,EAAE,YAAY,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,EACzC,SAAS,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,EACrC,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,gBAAgB,EAAE,GAAG,IAAI,CAAC,CAQpC"}
1
+ {"version":3,"file":"getExistingIndexes.d.ts","sourceRoot":"","sources":["../../src/getExistingIndexes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACjD,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,KAAK,CAAA;AAEnD,OAAO,EACL,KAAK,gBAAgB,EAEtB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAiCnD;;;;;;GAMG;AACH,wBAAsB,kBAAkB,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,EAC1E,EAAE,EAAE,YAAY,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,EACzC,SAAS,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,EACrC,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,gBAAgB,EAAE,GAAG,IAAI,CAAC,CAQpC"}
@@ -191,37 +191,60 @@ async function withDb(dbName, callback, expectedIndexes, logger, lock = true) {
191
191
 
192
192
  // src/IndexedDbKeyValueStore.ts
193
193
  var IndexedDbKeyValueStore = class {
194
+ /** The name of the IndexedDB database. */
194
195
  dbName;
196
+ /** The name of the object store within the database. */
195
197
  storeName;
196
198
  constructor(dbName, storeName) {
197
199
  this.dbName = dbName;
198
200
  this.storeName = storeName;
199
201
  }
202
+ /** Removes all entries from the store. */
200
203
  async clear() {
201
204
  return await this.withDb((db) => {
202
205
  return db.clear(this.storeName);
203
206
  });
204
207
  }
208
+ /**
209
+ * Deletes the entry with the given key.
210
+ * @param key The key of the entry to delete
211
+ */
205
212
  async delete(key) {
206
213
  return await this.withDb((db) => {
207
214
  return db.delete(this.storeName, key);
208
215
  });
209
216
  }
217
+ /**
218
+ * Retrieves the value associated with the given key.
219
+ * @param key The key to look up
220
+ * @returns The value, or undefined if not found
221
+ */
210
222
  async get(key) {
211
223
  return await this.withDb((db) => {
212
224
  return db.get(this.storeName, key) ?? void 0;
213
225
  });
214
226
  }
227
+ /** Returns all keys in the store. */
215
228
  async keys() {
216
229
  return await this.withDb((db) => {
217
230
  return db.getAllKeys(this.storeName);
218
231
  });
219
232
  }
233
+ /**
234
+ * Sets a value for the given key, creating or updating the entry.
235
+ * @param key The key to set
236
+ * @param value The value to store
237
+ */
220
238
  async set(key, value) {
221
239
  return await this.withDb(async (db) => {
222
240
  await db.put(this.storeName, value, key);
223
241
  });
224
242
  }
243
+ /**
244
+ * Opens the underlying IndexedDB database and passes it to the callback.
245
+ * @param callback Function to execute with the database
246
+ * @returns The result of the callback
247
+ */
225
248
  async withDb(callback) {
226
249
  return await withDb(this.dbName, (db) => {
227
250
  return callback(db);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/checkStoreNeedsUpgrade.ts","../../src/withDbByVersion.ts","../../src/IndexDescription.ts","../../src/createStoreDuringUpgrade.ts","../../src/withStore.ts","../../src/withReadOnlyStore.ts","../../src/getExistingIndexes.ts","../../src/checkDbNeedsUpgrade.ts","../../src/withDb.ts","../../src/IndexedDbKeyValueStore.ts","../../src/withReadWriteStore.ts"],"sourcesContent":["import { exists } from '@xylabs/exists'\nimport type { Logger } from '@xylabs/logger'\nimport type { IDBPDatabase } from 'idb'\n\nimport { getExistingIndexes } from './getExistingIndexes.ts'\nimport { buildStandardIndexName, type IndexDescription } from './IndexDescription.ts'\nimport type { ObjectStore } from './ObjectStore.ts'\n\nexport async function checkStoreNeedsUpgrade(db: IDBPDatabase<ObjectStore<object>>, storeName: string, indexes: IndexDescription[], logger?: Logger) {\n logger?.log('checkStoreNeedsUpgrade', storeName, indexes)\n const existingIndexes = await getExistingIndexes(db, storeName, logger)\n if (existingIndexes === null) {\n // the store does not exist, so we need to trigger upgrade (no existing indexes)\n return true\n }\n const existingIndexNames = new Set(existingIndexes.map(({ key, unique }) => buildStandardIndexName({ key, unique })).filter(exists))\n for (const { key, unique } of indexes) {\n const indexName = buildStandardIndexName({ key, unique })\n if (!existingIndexNames.has(indexName)) {\n return true\n }\n }\n return false\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\nimport { Mutex } from 'async-mutex'\nimport type {\n DBSchema, IDBPDatabase, StoreNames,\n} from 'idb'\nimport { openDB } from 'idb'\n\nimport { createStoreDuringUpgrade } from './createStoreDuringUpgrade.ts'\nimport { buildStandardIndexName, type IndexDescription } from './IndexDescription.ts'\n\nconst dbMutexes: Record<string, Mutex> = {}\n\nexport async function withDbByVersion<DBTypes extends DBSchema | unknown = unknown, R = EmptyObject>(\n dbName: string,\n callback: (db: IDBPDatabase<DBTypes>) => Promise<R> | R,\n version?: number,\n expectedIndexes?: Record<string, IndexDescription[]>,\n logger?: Logger,\n lock = true,\n): Promise<R> {\n dbMutexes[dbName] = dbMutexes[dbName] ?? new Mutex()\n const handler = async () => {\n const db = await openDB<DBTypes>(dbName, version, {\n /* v8 ignore next 2 */\n blocked(currentVersion, blockedVersion, event) {\n logger?.warn(`IndexedDb: Blocked from upgrading from ${currentVersion} to ${blockedVersion}`, event)\n },\n /* v8 ignore next 2 */\n blocking(currentVersion, blockedVersion, event) {\n logger?.warn(`IndexedDb: Blocking upgrade from ${currentVersion} to ${blockedVersion}`, event)\n },\n /* v8 ignore next 2 */\n terminated() {\n logger?.log('IndexedDb: Terminated')\n },\n upgrade(db, _oldVersion, _newVersion, _transaction) {\n /* if (oldVersion !== newVersion) {\n logger?.log(`IndexedDb: Upgrading from ${oldVersion} to ${newVersion}`)\n const objectStores = transaction.objectStoreNames\n for (const name of objectStores) {\n try {\n db.deleteObjectStore(name)\n } catch {\n console.log(`IndexedDb: Failed to delete existing object store ${name}`)\n }\n }\n } */\n if (expectedIndexes) {\n for (const [storeName, indexes] of Object.entries(expectedIndexes)) {\n if (db.objectStoreNames.contains(storeName as StoreNames<DBTypes>)) {\n continue\n }\n const indexesToCreate = indexes.map(idx => ({\n ...idx,\n name: buildStandardIndexName(idx),\n // eslint-disable-next-line unicorn/no-array-reduce\n })).reduce((acc, idx) => acc.set(idx.name, idx), new Map<string, IndexDescription>()).values()\n createStoreDuringUpgrade(db, storeName as StoreNames<DBTypes>, [...indexesToCreate], logger)\n }\n }\n },\n })\n return db\n }\n const db = lock ? await dbMutexes[dbName].runExclusive(handler) : await handler()\n try {\n return await callback(db)\n } finally {\n db.close()\n }\n}\n","/**\n * The index direction (1 for ascending, -1 for descending)\n */\nexport type IndexDirection = -1 | 1\n\n/**\n * Description of index(es) to be created on a store\n */\nexport type IndexDescription = {\n /**\n * The key(s) to index\n */\n key: Record<string, IndexDirection>\n /**\n * Is the indexed value an array\n */\n multiEntry?: boolean\n /**\n * If true, the index must enforce uniqueness on the key\n */\n unique?: boolean\n}\n\nexport const IndexSeparator = '-'\n\n/**\n * Given an index description, this will build the index\n * name in standard form\n * @param index The index description\n * @returns The index name in standard form\n */\nexport const buildStandardIndexName = (index: IndexDescription) => {\n const { key, unique } = index\n const prefix = unique ? 'UX' : 'IX'\n const indexKeys = Object.keys(key)\n return `${prefix}_${indexKeys.join(IndexSeparator)}`\n}\n","import type { Logger } from '@xylabs/logger'\nimport type {\n DBSchema,\n IDBPDatabase, IDBPObjectStore, IndexNames, StoreNames,\n} from 'idb'\n\nimport {\n buildStandardIndexName,\n type IndexDescription,\n} from './IndexDescription.ts'\n\nexport function createStoreDuringUpgrade<DBTypes extends DBSchema | unknown = unknown>(\n db: IDBPDatabase<DBTypes>,\n storeName: StoreNames<DBTypes>,\n indexes: IndexDescription[],\n logger?: Logger,\n) {\n logger?.log(`Creating store ${storeName}`)\n // Create the store\n let store: IDBPObjectStore<DBTypes, ArrayLike<StoreNames<DBTypes>>, StoreNames<DBTypes>, 'versionchange'> | undefined\n try {\n store = db.createObjectStore(storeName, {\n // If it isn't explicitly set, create a value by auto incrementing.\n autoIncrement: true,\n })\n } catch {\n logger?.warn(`Failed to create store: ${storeName} already exists`)\n return\n }\n logger?.log(`Creating store: created ${storeName}`)\n // Name the store\n store.name = storeName\n // Create an index on the hash\n for (const {\n key, multiEntry, unique,\n } of indexes) {\n logger?.log(`Creating store: index ${key}`)\n const indexKeys = Object.keys(key)\n const keys = indexKeys.length === 1 ? indexKeys[0] : indexKeys\n const indexName = buildStandardIndexName({ key, unique }) as IndexNames<DBTypes, StoreNames<DBTypes>>\n logger?.log('createIndex', indexName, keys, { multiEntry, unique })\n store.createIndex(indexName, keys, { multiEntry, unique })\n }\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\nimport type {\n IDBPDatabase, IDBPObjectStore, IDBPTransaction, StoreNames,\n} from 'idb'\n\nimport type { ObjectStore } from './ObjectStore.ts'\n\nexport async function withStore<T extends EmptyObject = EmptyObject, R = T, M extends 'readonly' | 'readwrite' = 'readonly'>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n callback: (store: IDBPObjectStore<ObjectStore<T>,\n [StoreNames<ObjectStore<T>>], StoreNames<ObjectStore<T>>, M> | null) => Promise<R> | R,\n mode: M,\n logger?: Logger,\n): Promise<R> {\n logger?.log('withStore', storeName, mode)\n let transaction: IDBPTransaction<ObjectStore<T>, [StoreNames<ObjectStore<T>>], M> | undefined = undefined\n logger?.log('withStore:transaction')\n let store = null\n try {\n transaction = db.transaction(storeName, mode)\n // we do this in a try/catch because the store might not exist\n store = transaction.objectStore(storeName)\n } catch (ex) {\n logger?.log('withStore:catch', ex)\n }\n try {\n return await callback(store)\n } finally {\n logger?.log('withStore:finally')\n await transaction?.done\n }\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\nimport type {\n IDBPDatabase, IDBPObjectStore, StoreNames,\n} from 'idb'\n\nimport type { ObjectStore } from './ObjectStore.ts'\nimport { withStore } from './withStore.ts'\n\nexport async function withReadOnlyStore<T extends EmptyObject = EmptyObject, R = T>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n callback: (store: IDBPObjectStore<ObjectStore<T>, [StoreNames<ObjectStore<T>>], StoreNames<ObjectStore<T>>, 'readonly'> | null) => Promise<R> | R,\n logger?: Logger,\n): Promise<R> {\n return await withStore(db, storeName, callback, 'readonly', logger)\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\nimport type { IDBPDatabase, StoreNames } from 'idb'\n\nimport {\n type IndexDescription,\n type IndexDirection,\n} from './IndexDescription.ts'\nimport type { ObjectStore } from './ObjectStore.ts'\nimport { withDbByVersion } from './withDbByVersion.ts'\nimport { withReadOnlyStore } from './withReadOnlyStore.ts'\n\nasync function getExistingIndexesInternal<T extends EmptyObject = EmptyObject>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n logger?: Logger,\n): Promise<IndexDescription[] | null> {\n logger?.log('getExistingIndexesInternal', storeName)\n return await withReadOnlyStore(db, storeName, (store) => {\n return store\n ? [...store.indexNames].map((indexName) => {\n const index = store.index(indexName)\n const key: Record<string, IndexDirection> = {}\n if (Array.isArray(index.keyPath)) {\n for (const keyPath of index.keyPath) {\n key[keyPath] = 1\n }\n } else {\n key[index.keyPath] = 1\n }\n const desc: IndexDescription = {\n key,\n unique: index.unique,\n multiEntry: index.multiEntry,\n }\n return desc\n })\n : null\n }, logger)\n}\n\nexport async function getExistingIndexes<T extends EmptyObject = EmptyObject>(\n db: IDBPDatabase<ObjectStore<T>> | string,\n storeName: StoreNames<ObjectStore<T>>,\n logger?: Logger,\n): Promise<IndexDescription[] | null> {\n logger?.log('getExistingIndexes', storeName)\n if (typeof db === 'string') {\n return await withDbByVersion<ObjectStore<T>, IndexDescription[] | null>(db, async (db) => {\n return await getExistingIndexesInternal(db, storeName, logger)\n })\n }\n return await getExistingIndexesInternal(db, storeName, logger)\n}\n","import type { Logger } from '@xylabs/logger'\n\nimport { checkStoreNeedsUpgrade } from './checkStoreNeedsUpgrade.ts'\nimport { type IndexDescription } from './IndexDescription.ts'\nimport type { ObjectStore } from './ObjectStore.ts'\nimport { withDbByVersion } from './withDbByVersion.ts'\n\nexport async function checkDbNeedsUpgrade(dbName: string, stores: Record<string, IndexDescription[]>, logger?: Logger) {\n logger?.log('checkDbNeedsUpgrade', dbName, stores)\n return await withDbByVersion<ObjectStore, number>(dbName, async (db) => {\n for (const [storeName, indexes] of Object.entries(stores)) {\n if (await checkStoreNeedsUpgrade(db, storeName, indexes, logger)) {\n return db.version + 1\n }\n }\n return db.version\n }, undefined, stores, logger)\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\nimport { Mutex } from 'async-mutex'\nimport type { DBSchema, IDBPDatabase } from 'idb'\n\nimport { checkDbNeedsUpgrade } from './checkDbNeedsUpgrade.ts'\nimport { type IndexDescription } from './IndexDescription.ts'\nimport { withDbByVersion } from './withDbByVersion.ts'\n\nconst dbMutexes: Record<string, Mutex> = {}\n\nexport async function withDb<DBTypes extends DBSchema | unknown = unknown, R = EmptyObject>(\n dbName: string,\n callback: (db: IDBPDatabase<DBTypes>) => Promise<R> | R,\n expectedIndexes?: Record<string, IndexDescription[]>,\n logger?: Logger,\n lock = true,\n): Promise<R> {\n dbMutexes[dbName] = dbMutexes[dbName] ?? new Mutex()\n const handler = async () => {\n const versionToOpen = expectedIndexes === undefined ? undefined : await checkDbNeedsUpgrade(dbName, expectedIndexes, logger)\n return await withDbByVersion<DBTypes, R>(dbName, async (db) => {\n return await callback(db)\n }, versionToOpen, expectedIndexes, logger, lock)\n }\n return lock ? await dbMutexes[dbName].runExclusive(handler) : await handler()\n}\n","import type { KeyValueStore } from '@xylabs/storage'\nimport type {\n DBSchema,\n IDBPDatabase, StoreKey, StoreNames, StoreValue,\n} from 'idb'\n\nimport { withDb } from './withDb.ts'\n\n/**\n * An IndexedDB key/value store.\n */\nexport class IndexedDbKeyValueStore<T extends DBSchema, S extends StoreNames<T>> implements KeyValueStore<StoreValue<T, S>, StoreKey<T, S>> {\n readonly dbName: string\n readonly storeName: S\n\n constructor(dbName: string, storeName: S) {\n this.dbName = dbName\n this.storeName = storeName\n }\n\n async clear?(): Promise<void> {\n return await this.withDb((db) => {\n return db.clear(this.storeName)\n })\n }\n\n async delete(key: StoreKey<T, S>): Promise<void> {\n return await this.withDb((db) => {\n return db.delete(this.storeName, key)\n })\n }\n\n async get(key: StoreKey<T, S>) {\n return await this.withDb((db) => {\n /* v8 ignore start */\n return db.get(this.storeName, key) ?? undefined\n /* v8 ignore stop */\n })\n }\n\n async keys?(): Promise<StoreKey<T, S>[]> {\n return await this.withDb((db) => {\n return db.getAllKeys(this.storeName)\n })\n }\n\n async set(key: StoreKey<T, S>, value: StoreValue<T, S>): Promise<void> {\n return await this.withDb(async (db) => {\n await db.put(this.storeName, value, key)\n })\n }\n\n async withDb<R = StoreValue<T, S>>(\n callback: (db: IDBPDatabase<T>) =>\n Promise<R> | R,\n ) {\n return await withDb<T, R>(this.dbName, (db) => {\n return callback(db)\n })\n }\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\nimport type {\n IDBPDatabase, IDBPObjectStore, StoreNames,\n} from 'idb'\n\nimport type { ObjectStore } from './ObjectStore.ts'\nimport { withStore } from './withStore.ts'\n\nexport async function withReadWriteStore<T extends EmptyObject = EmptyObject, R = T>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n callback: (store: IDBPObjectStore<ObjectStore<T>, [StoreNames<ObjectStore<T>>], StoreNames<ObjectStore<T>>, 'readwrite'> | null) => Promise<R> | R,\n logger?: Logger,\n): Promise<R> {\n return await withStore(db, storeName, callback, 'readwrite', logger)\n}\n"],"mappings":";AAAA,SAAS,cAAc;;;ACEvB,SAAS,aAAa;AAItB,SAAS,cAAc;;;ACiBhB,IAAM,iBAAiB;AAQvB,IAAM,yBAAyB,CAAC,UAA4B;AACjE,QAAM,EAAE,KAAK,OAAO,IAAI;AACxB,QAAM,SAAS,SAAS,OAAO;AAC/B,QAAM,YAAY,OAAO,KAAK,GAAG;AACjC,SAAO,GAAG,MAAM,IAAI,UAAU,KAAK,cAAc,CAAC;AACpD;;;ACzBO,SAAS,yBACd,IACA,WACA,SACA,QACA;AACA,UAAQ,IAAI,kBAAkB,SAAS,EAAE;AAEzC,MAAI;AACJ,MAAI;AACF,YAAQ,GAAG,kBAAkB,WAAW;AAAA;AAAA,MAEtC,eAAe;AAAA,IACjB,CAAC;AAAA,EACH,QAAQ;AACN,YAAQ,KAAK,2BAA2B,SAAS,iBAAiB;AAClE;AAAA,EACF;AACA,UAAQ,IAAI,2BAA2B,SAAS,EAAE;AAElD,QAAM,OAAO;AAEb,aAAW;AAAA,IACT;AAAA,IAAK;AAAA,IAAY;AAAA,EACnB,KAAK,SAAS;AACZ,YAAQ,IAAI,yBAAyB,GAAG,EAAE;AAC1C,UAAM,YAAY,OAAO,KAAK,GAAG;AACjC,UAAM,OAAO,UAAU,WAAW,IAAI,UAAU,CAAC,IAAI;AACrD,UAAM,YAAY,uBAAuB,EAAE,KAAK,OAAO,CAAC;AACxD,YAAQ,IAAI,eAAe,WAAW,MAAM,EAAE,YAAY,OAAO,CAAC;AAClE,UAAM,YAAY,WAAW,MAAM,EAAE,YAAY,OAAO,CAAC;AAAA,EAC3D;AACF;;;AFhCA,IAAM,YAAmC,CAAC;AAE1C,eAAsB,gBACpB,QACA,UACA,SACA,iBACA,QACA,OAAO,MACK;AACZ,YAAU,MAAM,IAAI,UAAU,MAAM,KAAK,IAAI,MAAM;AACnD,QA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Mutex","dbMutexes","Mutex"]}
1
+ {"version":3,"sources":["../../src/checkStoreNeedsUpgrade.ts","../../src/withDbByVersion.ts","../../src/IndexDescription.ts","../../src/createStoreDuringUpgrade.ts","../../src/withStore.ts","../../src/withReadOnlyStore.ts","../../src/getExistingIndexes.ts","../../src/checkDbNeedsUpgrade.ts","../../src/withDb.ts","../../src/IndexedDbKeyValueStore.ts","../../src/withReadWriteStore.ts"],"sourcesContent":["import { exists } from '@xylabs/exists'\nimport type { Logger } from '@xylabs/logger'\nimport type { IDBPDatabase } from 'idb'\n\nimport { getExistingIndexes } from './getExistingIndexes.ts'\nimport { buildStandardIndexName, type IndexDescription } from './IndexDescription.ts'\nimport type { ObjectStore } from './ObjectStore.ts'\n\n/**\n * Checks whether a store needs an upgrade by comparing its existing indexes against expected indexes.\n * @param db The IndexedDB database instance\n * @param storeName The name of the store to check\n * @param indexes The expected index descriptions\n * @param logger Optional logger for diagnostics\n * @returns True if the store is missing or has missing indexes\n */\nexport async function checkStoreNeedsUpgrade(db: IDBPDatabase<ObjectStore<object>>, storeName: string, indexes: IndexDescription[], logger?: Logger) {\n logger?.log('checkStoreNeedsUpgrade', storeName, indexes)\n const existingIndexes = await getExistingIndexes(db, storeName, logger)\n if (existingIndexes === null) {\n // the store does not exist, so we need to trigger upgrade (no existing indexes)\n return true\n }\n const existingIndexNames = new Set(existingIndexes.map(({ key, unique }) => buildStandardIndexName({ key, unique })).filter(exists))\n for (const { key, unique } of indexes) {\n const indexName = buildStandardIndexName({ key, unique })\n if (!existingIndexNames.has(indexName)) {\n return true\n }\n }\n return false\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\nimport { Mutex } from 'async-mutex'\nimport type {\n DBSchema, IDBPDatabase, StoreNames,\n} from 'idb'\nimport { openDB } from 'idb'\n\nimport { createStoreDuringUpgrade } from './createStoreDuringUpgrade.ts'\nimport { buildStandardIndexName, type IndexDescription } from './IndexDescription.ts'\n\nconst dbMutexes: Record<string, Mutex> = {}\n\n/**\n * Opens an IndexedDB database at a specific version, handling upgrade events, and passes it to the callback.\n * The database is automatically closed after the callback completes.\n * @param dbName The name of the database to open\n * @param callback Function to execute with the opened database\n * @param version Optional specific version to open (undefined for latest)\n * @param expectedIndexes Optional map of store names to indexes to create during upgrade\n * @param logger Optional logger for diagnostics\n * @param lock Whether to use a mutex to serialize access (defaults to true)\n * @returns The result of the callback\n */\nexport async function withDbByVersion<DBTypes extends DBSchema | unknown = unknown, R = EmptyObject>(\n dbName: string,\n callback: (db: IDBPDatabase<DBTypes>) => Promise<R> | R,\n version?: number,\n expectedIndexes?: Record<string, IndexDescription[]>,\n logger?: Logger,\n lock = true,\n): Promise<R> {\n dbMutexes[dbName] = dbMutexes[dbName] ?? new Mutex()\n const handler = async () => {\n const db = await openDB<DBTypes>(dbName, version, {\n /* v8 ignore next 2 */\n blocked(currentVersion, blockedVersion, event) {\n logger?.warn(`IndexedDb: Blocked from upgrading from ${currentVersion} to ${blockedVersion}`, event)\n },\n /* v8 ignore next 2 */\n blocking(currentVersion, blockedVersion, event) {\n logger?.warn(`IndexedDb: Blocking upgrade from ${currentVersion} to ${blockedVersion}`, event)\n },\n /* v8 ignore next 2 */\n terminated() {\n logger?.log('IndexedDb: Terminated')\n },\n upgrade(db, _oldVersion, _newVersion, _transaction) {\n /* if (oldVersion !== newVersion) {\n logger?.log(`IndexedDb: Upgrading from ${oldVersion} to ${newVersion}`)\n const objectStores = transaction.objectStoreNames\n for (const name of objectStores) {\n try {\n db.deleteObjectStore(name)\n } catch {\n console.log(`IndexedDb: Failed to delete existing object store ${name}`)\n }\n }\n } */\n if (expectedIndexes) {\n for (const [storeName, indexes] of Object.entries(expectedIndexes)) {\n if (db.objectStoreNames.contains(storeName as StoreNames<DBTypes>)) {\n continue\n }\n const indexesToCreate = indexes.map(idx => ({\n ...idx,\n name: buildStandardIndexName(idx),\n // eslint-disable-next-line unicorn/no-array-reduce\n })).reduce((acc, idx) => acc.set(idx.name, idx), new Map<string, IndexDescription>()).values()\n createStoreDuringUpgrade(db, storeName as StoreNames<DBTypes>, [...indexesToCreate], logger)\n }\n }\n },\n })\n return db\n }\n const db = lock ? await dbMutexes[dbName].runExclusive(handler) : await handler()\n try {\n return await callback(db)\n } finally {\n db.close()\n }\n}\n","/**\n * The index direction (1 for ascending, -1 for descending)\n */\nexport type IndexDirection = -1 | 1\n\n/**\n * Description of index(es) to be created on a store\n */\nexport type IndexDescription = {\n /**\n * The key(s) to index\n */\n key: Record<string, IndexDirection>\n /**\n * Is the indexed value an array\n */\n multiEntry?: boolean\n /**\n * If true, the index must enforce uniqueness on the key\n */\n unique?: boolean\n}\n\n/** Separator used between key names when building standard index names. */\nexport const IndexSeparator = '-'\n\n/**\n * Given an index description, this will build the index\n * name in standard form\n * @param index The index description\n * @returns The index name in standard form\n */\nexport const buildStandardIndexName = (index: IndexDescription) => {\n const { key, unique } = index\n const prefix = unique ? 'UX' : 'IX'\n const indexKeys = Object.keys(key)\n return `${prefix}_${indexKeys.join(IndexSeparator)}`\n}\n","import type { Logger } from '@xylabs/logger'\nimport type {\n DBSchema,\n IDBPDatabase, IDBPObjectStore, IndexNames, StoreNames,\n} from 'idb'\n\nimport {\n buildStandardIndexName,\n type IndexDescription,\n} from './IndexDescription.ts'\n\n/**\n * Creates an object store with the specified indexes during a version upgrade transaction.\n * @param db The IndexedDB database instance (during upgrade)\n * @param storeName The name of the store to create\n * @param indexes The index descriptions to create on the store\n * @param logger Optional logger for diagnostics\n */\nexport function createStoreDuringUpgrade<DBTypes extends DBSchema | unknown = unknown>(\n db: IDBPDatabase<DBTypes>,\n storeName: StoreNames<DBTypes>,\n indexes: IndexDescription[],\n logger?: Logger,\n) {\n logger?.log(`Creating store ${storeName}`)\n // Create the store\n let store: IDBPObjectStore<DBTypes, ArrayLike<StoreNames<DBTypes>>, StoreNames<DBTypes>, 'versionchange'> | undefined\n try {\n store = db.createObjectStore(storeName, {\n // If it isn't explicitly set, create a value by auto incrementing.\n autoIncrement: true,\n })\n } catch {\n logger?.warn(`Failed to create store: ${storeName} already exists`)\n return\n }\n logger?.log(`Creating store: created ${storeName}`)\n // Name the store\n store.name = storeName\n // Create an index on the hash\n for (const {\n key, multiEntry, unique,\n } of indexes) {\n logger?.log(`Creating store: index ${key}`)\n const indexKeys = Object.keys(key)\n const keys = indexKeys.length === 1 ? indexKeys[0] : indexKeys\n const indexName = buildStandardIndexName({ key, unique }) as IndexNames<DBTypes, StoreNames<DBTypes>>\n logger?.log('createIndex', indexName, keys, { multiEntry, unique })\n store.createIndex(indexName, keys, { multiEntry, unique })\n }\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\nimport type {\n IDBPDatabase, IDBPObjectStore, IDBPTransaction, StoreNames,\n} from 'idb'\n\nimport type { ObjectStore } from './ObjectStore.ts'\n\n/**\n * Opens a transaction on the specified store with the given mode and passes the store to the callback.\n * If the store does not exist, the callback receives null.\n * @param db The IndexedDB database instance\n * @param storeName The name of the object store to open\n * @param callback Function to execute with the store (or null if it doesn't exist)\n * @param mode The transaction mode ('readonly' or 'readwrite')\n * @param logger Optional logger for diagnostics\n * @returns The result of the callback\n */\nexport async function withStore<T extends EmptyObject = EmptyObject, R = T, M extends 'readonly' | 'readwrite' = 'readonly'>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n callback: (store: IDBPObjectStore<ObjectStore<T>,\n [StoreNames<ObjectStore<T>>], StoreNames<ObjectStore<T>>, M> | null) => Promise<R> | R,\n mode: M,\n logger?: Logger,\n): Promise<R> {\n logger?.log('withStore', storeName, mode)\n let transaction: IDBPTransaction<ObjectStore<T>, [StoreNames<ObjectStore<T>>], M> | undefined = undefined\n logger?.log('withStore:transaction')\n let store = null\n try {\n transaction = db.transaction(storeName, mode)\n // we do this in a try/catch because the store might not exist\n store = transaction.objectStore(storeName)\n } catch (ex) {\n logger?.log('withStore:catch', ex)\n }\n try {\n return await callback(store)\n } finally {\n logger?.log('withStore:finally')\n await transaction?.done\n }\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\nimport type {\n IDBPDatabase, IDBPObjectStore, StoreNames,\n} from 'idb'\n\nimport type { ObjectStore } from './ObjectStore.ts'\nimport { withStore } from './withStore.ts'\n\n/**\n * Opens a read-only transaction on the specified store and passes it to the callback.\n * @param db The IndexedDB database instance\n * @param storeName The name of the object store to open\n * @param callback Function to execute with the read-only store\n * @param logger Optional logger for diagnostics\n * @returns The result of the callback\n */\nexport async function withReadOnlyStore<T extends EmptyObject = EmptyObject, R = T>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n callback: (store: IDBPObjectStore<ObjectStore<T>, [StoreNames<ObjectStore<T>>], StoreNames<ObjectStore<T>>, 'readonly'> | null) => Promise<R> | R,\n logger?: Logger,\n): Promise<R> {\n return await withStore(db, storeName, callback, 'readonly', logger)\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\nimport type { IDBPDatabase, StoreNames } from 'idb'\n\nimport {\n type IndexDescription,\n type IndexDirection,\n} from './IndexDescription.ts'\nimport type { ObjectStore } from './ObjectStore.ts'\nimport { withDbByVersion } from './withDbByVersion.ts'\nimport { withReadOnlyStore } from './withReadOnlyStore.ts'\n\nasync function getExistingIndexesInternal<T extends EmptyObject = EmptyObject>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n logger?: Logger,\n): Promise<IndexDescription[] | null> {\n logger?.log('getExistingIndexesInternal', storeName)\n return await withReadOnlyStore(db, storeName, (store) => {\n return store\n ? [...store.indexNames].map((indexName) => {\n const index = store.index(indexName)\n const key: Record<string, IndexDirection> = {}\n if (Array.isArray(index.keyPath)) {\n for (const keyPath of index.keyPath) {\n key[keyPath] = 1\n }\n } else {\n key[index.keyPath] = 1\n }\n const desc: IndexDescription = {\n key,\n unique: index.unique,\n multiEntry: index.multiEntry,\n }\n return desc\n })\n : null\n }, logger)\n}\n\n/**\n * Retrieves the existing index descriptions for a store. Accepts either a database instance or a database name.\n * @param db The IndexedDB database instance or database name\n * @param storeName The name of the store to inspect\n * @param logger Optional logger for diagnostics\n * @returns An array of index descriptions, or null if the store does not exist\n */\nexport async function getExistingIndexes<T extends EmptyObject = EmptyObject>(\n db: IDBPDatabase<ObjectStore<T>> | string,\n storeName: StoreNames<ObjectStore<T>>,\n logger?: Logger,\n): Promise<IndexDescription[] | null> {\n logger?.log('getExistingIndexes', storeName)\n if (typeof db === 'string') {\n return await withDbByVersion<ObjectStore<T>, IndexDescription[] | null>(db, async (db) => {\n return await getExistingIndexesInternal(db, storeName, logger)\n })\n }\n return await getExistingIndexesInternal(db, storeName, logger)\n}\n","import type { Logger } from '@xylabs/logger'\n\nimport { checkStoreNeedsUpgrade } from './checkStoreNeedsUpgrade.ts'\nimport { type IndexDescription } from './IndexDescription.ts'\nimport type { ObjectStore } from './ObjectStore.ts'\nimport { withDbByVersion } from './withDbByVersion.ts'\n\n/**\n * Checks whether any store in the database needs an upgrade and returns the appropriate version number.\n * @param dbName The name of the database to check\n * @param stores Map of store names to their expected index descriptions\n * @param logger Optional logger for diagnostics\n * @returns The version to open (current version + 1 if upgrade needed, otherwise current version)\n */\nexport async function checkDbNeedsUpgrade(dbName: string, stores: Record<string, IndexDescription[]>, logger?: Logger) {\n logger?.log('checkDbNeedsUpgrade', dbName, stores)\n return await withDbByVersion<ObjectStore, number>(dbName, async (db) => {\n for (const [storeName, indexes] of Object.entries(stores)) {\n if (await checkStoreNeedsUpgrade(db, storeName, indexes, logger)) {\n return db.version + 1\n }\n }\n return db.version\n }, undefined, stores, logger)\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\nimport { Mutex } from 'async-mutex'\nimport type { DBSchema, IDBPDatabase } from 'idb'\n\nimport { checkDbNeedsUpgrade } from './checkDbNeedsUpgrade.ts'\nimport { type IndexDescription } from './IndexDescription.ts'\nimport { withDbByVersion } from './withDbByVersion.ts'\n\nconst dbMutexes: Record<string, Mutex> = {}\n\n/**\n * Opens an IndexedDB database, automatically upgrading if needed, and passes it to the callback.\n * Uses a mutex to serialize access to the same database by default.\n * @param dbName The name of the database to open\n * @param callback Function to execute with the opened database\n * @param expectedIndexes Optional map of store names to their expected indexes (triggers upgrade check)\n * @param logger Optional logger for diagnostics\n * @param lock Whether to use a mutex to serialize access (defaults to true)\n * @returns The result of the callback\n */\nexport async function withDb<DBTypes extends DBSchema | unknown = unknown, R = EmptyObject>(\n dbName: string,\n callback: (db: IDBPDatabase<DBTypes>) => Promise<R> | R,\n expectedIndexes?: Record<string, IndexDescription[]>,\n logger?: Logger,\n lock = true,\n): Promise<R> {\n dbMutexes[dbName] = dbMutexes[dbName] ?? new Mutex()\n const handler = async () => {\n const versionToOpen = expectedIndexes === undefined ? undefined : await checkDbNeedsUpgrade(dbName, expectedIndexes, logger)\n return await withDbByVersion<DBTypes, R>(dbName, async (db) => {\n return await callback(db)\n }, versionToOpen, expectedIndexes, logger, lock)\n }\n return lock ? await dbMutexes[dbName].runExclusive(handler) : await handler()\n}\n","import type { KeyValueStore } from '@xylabs/storage'\nimport type {\n DBSchema,\n IDBPDatabase, StoreKey, StoreNames, StoreValue,\n} from 'idb'\n\nimport { withDb } from './withDb.ts'\n\n/**\n * An IndexedDB key/value store.\n */\nexport class IndexedDbKeyValueStore<T extends DBSchema, S extends StoreNames<T>> implements KeyValueStore<StoreValue<T, S>, StoreKey<T, S>> {\n /** The name of the IndexedDB database. */\n readonly dbName: string\n /** The name of the object store within the database. */\n readonly storeName: S\n\n constructor(dbName: string, storeName: S) {\n this.dbName = dbName\n this.storeName = storeName\n }\n\n /** Removes all entries from the store. */\n async clear?(): Promise<void> {\n return await this.withDb((db) => {\n return db.clear(this.storeName)\n })\n }\n\n /**\n * Deletes the entry with the given key.\n * @param key The key of the entry to delete\n */\n async delete(key: StoreKey<T, S>): Promise<void> {\n return await this.withDb((db) => {\n return db.delete(this.storeName, key)\n })\n }\n\n /**\n * Retrieves the value associated with the given key.\n * @param key The key to look up\n * @returns The value, or undefined if not found\n */\n async get(key: StoreKey<T, S>) {\n return await this.withDb((db) => {\n /* v8 ignore start */\n return db.get(this.storeName, key) ?? undefined\n /* v8 ignore stop */\n })\n }\n\n /** Returns all keys in the store. */\n async keys?(): Promise<StoreKey<T, S>[]> {\n return await this.withDb((db) => {\n return db.getAllKeys(this.storeName)\n })\n }\n\n /**\n * Sets a value for the given key, creating or updating the entry.\n * @param key The key to set\n * @param value The value to store\n */\n async set(key: StoreKey<T, S>, value: StoreValue<T, S>): Promise<void> {\n return await this.withDb(async (db) => {\n await db.put(this.storeName, value, key)\n })\n }\n\n /**\n * Opens the underlying IndexedDB database and passes it to the callback.\n * @param callback Function to execute with the database\n * @returns The result of the callback\n */\n async withDb<R = StoreValue<T, S>>(\n callback: (db: IDBPDatabase<T>) =>\n Promise<R> | R,\n ) {\n return await withDb<T, R>(this.dbName, (db) => {\n return callback(db)\n })\n }\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\nimport type {\n IDBPDatabase, IDBPObjectStore, StoreNames,\n} from 'idb'\n\nimport type { ObjectStore } from './ObjectStore.ts'\nimport { withStore } from './withStore.ts'\n\n/**\n * Opens a read-write transaction on the specified store and passes it to the callback.\n * @param db The IndexedDB database instance\n * @param storeName The name of the object store to open\n * @param callback Function to execute with the read-write store\n * @param logger Optional logger for diagnostics\n * @returns The result of the callback\n */\nexport async function withReadWriteStore<T extends EmptyObject = EmptyObject, R = T>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n callback: (store: IDBPObjectStore<ObjectStore<T>, [StoreNames<ObjectStore<T>>], StoreNames<ObjectStore<T>>, 'readwrite'> | null) => Promise<R> | R,\n logger?: Logger,\n): Promise<R> {\n return await withStore(db, storeName, callback, 'readwrite', logger)\n}\n"],"mappings":";AAAA,SAAS,cAAc;;;ACEv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rE;","names":["db","db","Mutex","dbMutexes","Mutex"]}
@@ -2,5 +2,15 @@ import type { Logger } from '@xylabs/logger';
2
2
  import type { EmptyObject } from '@xylabs/object';
3
3
  import type { DBSchema, IDBPDatabase } from 'idb';
4
4
  import { type IndexDescription } from './IndexDescription.ts';
5
+ /**
6
+ * Opens an IndexedDB database, automatically upgrading if needed, and passes it to the callback.
7
+ * Uses a mutex to serialize access to the same database by default.
8
+ * @param dbName The name of the database to open
9
+ * @param callback Function to execute with the opened database
10
+ * @param expectedIndexes Optional map of store names to their expected indexes (triggers upgrade check)
11
+ * @param logger Optional logger for diagnostics
12
+ * @param lock Whether to use a mutex to serialize access (defaults to true)
13
+ * @returns The result of the callback
14
+ */
5
15
  export declare function withDb<DBTypes extends DBSchema | unknown = unknown, R = EmptyObject>(dbName: string, callback: (db: IDBPDatabase<DBTypes>) => Promise<R> | R, expectedIndexes?: Record<string, IndexDescription[]>, logger?: Logger, lock?: boolean): Promise<R>;
6
16
  //# sourceMappingURL=withDb.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"withDb.d.ts","sourceRoot":"","sources":["../../src/withDb.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAEjD,OAAO,KAAK,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,KAAK,CAAA;AAGjD,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAK7D,wBAAsB,MAAM,CAAC,OAAO,SAAS,QAAQ,GAAG,OAAO,GAAG,OAAO,EAAE,CAAC,GAAG,WAAW,EACxF,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,CAAC,EAAE,EAAE,YAAY,CAAC,OAAO,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,EACvD,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,EAAE,CAAC,EACpD,MAAM,CAAC,EAAE,MAAM,EACf,IAAI,UAAO,GACV,OAAO,CAAC,CAAC,CAAC,CASZ"}
1
+ {"version":3,"file":"withDb.d.ts","sourceRoot":"","sources":["../../src/withDb.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAEjD,OAAO,KAAK,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,KAAK,CAAA;AAGjD,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAK7D;;;;;;;;;GASG;AACH,wBAAsB,MAAM,CAAC,OAAO,SAAS,QAAQ,GAAG,OAAO,GAAG,OAAO,EAAE,CAAC,GAAG,WAAW,EACxF,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,CAAC,EAAE,EAAE,YAAY,CAAC,OAAO,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,EACvD,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,EAAE,CAAC,EACpD,MAAM,CAAC,EAAE,MAAM,EACf,IAAI,UAAO,GACV,OAAO,CAAC,CAAC,CAAC,CASZ"}
@@ -2,5 +2,16 @@ import type { Logger } from '@xylabs/logger';
2
2
  import type { EmptyObject } from '@xylabs/object';
3
3
  import type { DBSchema, IDBPDatabase } from 'idb';
4
4
  import { type IndexDescription } from './IndexDescription.ts';
5
+ /**
6
+ * Opens an IndexedDB database at a specific version, handling upgrade events, and passes it to the callback.
7
+ * The database is automatically closed after the callback completes.
8
+ * @param dbName The name of the database to open
9
+ * @param callback Function to execute with the opened database
10
+ * @param version Optional specific version to open (undefined for latest)
11
+ * @param expectedIndexes Optional map of store names to indexes to create during upgrade
12
+ * @param logger Optional logger for diagnostics
13
+ * @param lock Whether to use a mutex to serialize access (defaults to true)
14
+ * @returns The result of the callback
15
+ */
5
16
  export declare function withDbByVersion<DBTypes extends DBSchema | unknown = unknown, R = EmptyObject>(dbName: string, callback: (db: IDBPDatabase<DBTypes>) => Promise<R> | R, version?: number, expectedIndexes?: Record<string, IndexDescription[]>, logger?: Logger, lock?: boolean): Promise<R>;
6
17
  //# sourceMappingURL=withDbByVersion.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"withDbByVersion.d.ts","sourceRoot":"","sources":["../../src/withDbByVersion.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAEjD,OAAO,KAAK,EACV,QAAQ,EAAE,YAAY,EACvB,MAAM,KAAK,CAAA;AAIZ,OAAO,EAA0B,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAIrF,wBAAsB,eAAe,CAAC,OAAO,SAAS,QAAQ,GAAG,OAAO,GAAG,OAAO,EAAE,CAAC,GAAG,WAAW,EACjG,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,CAAC,EAAE,EAAE,YAAY,CAAC,OAAO,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,EACvD,OAAO,CAAC,EAAE,MAAM,EAChB,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,EAAE,CAAC,EACpD,MAAM,CAAC,EAAE,MAAM,EACf,IAAI,UAAO,GACV,OAAO,CAAC,CAAC,CAAC,CAmDZ"}
1
+ {"version":3,"file":"withDbByVersion.d.ts","sourceRoot":"","sources":["../../src/withDbByVersion.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAEjD,OAAO,KAAK,EACV,QAAQ,EAAE,YAAY,EACvB,MAAM,KAAK,CAAA;AAIZ,OAAO,EAA0B,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAIrF;;;;;;;;;;GAUG;AACH,wBAAsB,eAAe,CAAC,OAAO,SAAS,QAAQ,GAAG,OAAO,GAAG,OAAO,EAAE,CAAC,GAAG,WAAW,EACjG,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,CAAC,EAAE,EAAE,YAAY,CAAC,OAAO,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,EACvD,OAAO,CAAC,EAAE,MAAM,EAChB,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,EAAE,CAAC,EACpD,MAAM,CAAC,EAAE,MAAM,EACf,IAAI,UAAO,GACV,OAAO,CAAC,CAAC,CAAC,CAmDZ"}
@@ -2,5 +2,13 @@ import type { Logger } from '@xylabs/logger';
2
2
  import type { EmptyObject } from '@xylabs/object';
3
3
  import type { IDBPDatabase, IDBPObjectStore, StoreNames } from 'idb';
4
4
  import type { ObjectStore } from './ObjectStore.ts';
5
+ /**
6
+ * Opens a read-only transaction on the specified store and passes it to the callback.
7
+ * @param db The IndexedDB database instance
8
+ * @param storeName The name of the object store to open
9
+ * @param callback Function to execute with the read-only store
10
+ * @param logger Optional logger for diagnostics
11
+ * @returns The result of the callback
12
+ */
5
13
  export declare function withReadOnlyStore<T extends EmptyObject = EmptyObject, R = T>(db: IDBPDatabase<ObjectStore<T>>, storeName: StoreNames<ObjectStore<T>>, callback: (store: IDBPObjectStore<ObjectStore<T>, [StoreNames<ObjectStore<T>>], StoreNames<ObjectStore<T>>, 'readonly'> | null) => Promise<R> | R, logger?: Logger): Promise<R>;
6
14
  //# sourceMappingURL=withReadOnlyStore.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"withReadOnlyStore.d.ts","sourceRoot":"","sources":["../../src/withReadOnlyStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAA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,CAAC,CAAC,CAAC,CAAC,EACrC,QAAQ,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC,GAAG,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,EACjJ,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,CAAC,CAAC,CAEZ"}
@@ -2,5 +2,13 @@ import type { Logger } from '@xylabs/logger';
2
2
  import type { EmptyObject } from '@xylabs/object';
3
3
  import type { IDBPDatabase, IDBPObjectStore, StoreNames } from 'idb';
4
4
  import type { ObjectStore } from './ObjectStore.ts';
5
+ /**
6
+ * Opens a read-write transaction on the specified store and passes it to the callback.
7
+ * @param db The IndexedDB database instance
8
+ * @param storeName The name of the object store to open
9
+ * @param callback Function to execute with the read-write store
10
+ * @param logger Optional logger for diagnostics
11
+ * @returns The result of the callback
12
+ */
5
13
  export declare function withReadWriteStore<T extends EmptyObject = EmptyObject, R = T>(db: IDBPDatabase<ObjectStore<T>>, storeName: StoreNames<ObjectStore<T>>, callback: (store: IDBPObjectStore<ObjectStore<T>, [StoreNames<ObjectStore<T>>], StoreNames<ObjectStore<T>>, 'readwrite'> | null) => Promise<R> | R, logger?: Logger): Promise<R>;
6
14
  //# sourceMappingURL=withReadWriteStore.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"withReadWriteStore.d.ts","sourceRoot":"","sources":["../../src/withReadWriteStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACjD,OAAO,KAAK,EACV,YAAY,EAAE,eAAe,EAAE,UAAU,EAC1C,MAAM,KAAK,CAAA;AAEZ,OAAO,KAA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C,CAAC,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,EAAE,WAAW,CAAC,GAAG,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,EAClJ,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,CAAC,CAAC,CAEZ"}
@@ -2,6 +2,16 @@ import type { Logger } from '@xylabs/logger';
2
2
  import type { EmptyObject } from '@xylabs/object';
3
3
  import type { IDBPDatabase, IDBPObjectStore, StoreNames } from 'idb';
4
4
  import type { ObjectStore } from './ObjectStore.ts';
5
+ /**
6
+ * Opens a transaction on the specified store with the given mode and passes the store to the callback.
7
+ * If the store does not exist, the callback receives null.
8
+ * @param db The IndexedDB database instance
9
+ * @param storeName The name of the object store to open
10
+ * @param callback Function to execute with the store (or null if it doesn't exist)
11
+ * @param mode The transaction mode ('readonly' or 'readwrite')
12
+ * @param logger Optional logger for diagnostics
13
+ * @returns The result of the callback
14
+ */
5
15
  export declare function withStore<T extends EmptyObject = EmptyObject, R = T, M extends 'readonly' | 'readwrite' = 'readonly'>(db: IDBPDatabase<ObjectStore<T>>, storeName: StoreNames<ObjectStore<T>>, callback: (store: IDBPObjectStore<ObjectStore<T>, [
6
16
  StoreNames<ObjectStore<T>>
7
17
  ], StoreNames<ObjectStore<T>>, M> | null) => Promise<R> | R, mode: M, logger?: Logger): Promise<R>;
@@ -1 +1 @@
1
- {"version":3,"file":"withStore.d.ts","sourceRoot":"","sources":["../../src/withStore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACjD,OAAO,KAAK,EACV,YAAY,EAAE,eAAe,EAAmB,UAAU,EAC3D,MAAM,KAAK,CAAA;AAEZ,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEnD,wBAAsB,SAAS,CAAC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AAC,EAC9C;IAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;CAAC,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,EACxF,IAAI,EAAE,CAAC,EACP,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,CAAC,CAAC,CAkBZ"}
@@ -19,6 +19,7 @@ export type IndexDescription = {
19
19
  */
20
20
  unique?: boolean;
21
21
  };
22
+ /** Separator used between key names when building standard index names. */
22
23
  export declare const IndexSeparator = "-";
23
24
  /**
24
25
  * Given an index description, this will build the index
@@ -1 +1 @@
1
- {"version":3,"file":"IndexDescription.d.ts","sourceRoot":"","sources":["../../src/IndexDescription.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,GAAG,CAAC,CAAA;AAEnC;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAA;IACnC;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB,CAAA;AAED,eAAO,MAAM,cAAc,MAAM,CAAA;AAEjC;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,GAAI,OAAO,gBAAgB,WAK7D,CAAA"}
1
+ {"version":3,"file":"IndexDescription.d.ts","sourceRoot":"","sources":["../../src/IndexDescription.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,GAAG,CAAC,CAAA;AAEnC;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAA;IACnC;;OAEG;IACH,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;CACjB,CAAA;AAED,2EAA2E;AAC3E,eAAO,MAAM,cAAc,MAAM,CAAA;AAEjC;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,GAAI,OAAO,gBAAgB,WAK7D,CAAA"}
@@ -4,14 +4,37 @@ import type { DBSchema, IDBPDatabase, StoreKey, StoreNames, StoreValue } from 'i
4
4
  * An IndexedDB key/value store.
5
5
  */
6
6
  export declare class IndexedDbKeyValueStore<T extends DBSchema, S extends StoreNames<T>> implements KeyValueStore<StoreValue<T, S>, StoreKey<T, S>> {
7
+ /** The name of the IndexedDB database. */
7
8
  readonly dbName: string;
9
+ /** The name of the object store within the database. */
8
10
  readonly storeName: S;
9
11
  constructor(dbName: string, storeName: S);
12
+ /** Removes all entries from the store. */
10
13
  clear?(): Promise<void>;
14
+ /**
15
+ * Deletes the entry with the given key.
16
+ * @param key The key of the entry to delete
17
+ */
11
18
  delete(key: StoreKey<T, S>): Promise<void>;
19
+ /**
20
+ * Retrieves the value associated with the given key.
21
+ * @param key The key to look up
22
+ * @returns The value, or undefined if not found
23
+ */
12
24
  get(key: StoreKey<T, S>): Promise<StoreValue<T, S> | undefined>;
25
+ /** Returns all keys in the store. */
13
26
  keys?(): Promise<StoreKey<T, S>[]>;
27
+ /**
28
+ * Sets a value for the given key, creating or updating the entry.
29
+ * @param key The key to set
30
+ * @param value The value to store
31
+ */
14
32
  set(key: StoreKey<T, S>, value: StoreValue<T, S>): Promise<void>;
33
+ /**
34
+ * Opens the underlying IndexedDB database and passes it to the callback.
35
+ * @param callback Function to execute with the database
36
+ * @returns The result of the callback
37
+ */
15
38
  withDb<R = StoreValue<T, S>>(callback: (db: IDBPDatabase<T>) => Promise<R> | R): Promise<R>;
16
39
  }
17
40
  //# sourceMappingURL=IndexedDbKeyValueStore.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"IndexedDbKeyValueStore.d.ts","sourceR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,EAAE,EAAE,YAAY,CAAC,CAAC,CAAC,KAC5B,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC;CAMnB"}
@@ -1,4 +1,5 @@
1
1
  import type { EmptyObject } from '@xylabs/object';
2
+ /** Generic IndexedDB schema type that maps store names to their value types. */
2
3
  export interface ObjectStore<T extends EmptyObject = EmptyObject> {
3
4
  [s: string]: T;
4
5
  }
@@ -1 +1 @@
1
- {"version":3,"file":"ObjectStore.d.ts","sourceRoot":"","sources":["../../src/ObjectStore.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAEjD,MAAM,WAAW,WAAW,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW;IAC9D,CAAC,CAAC,EAAE,MAAM,GAAG,CAAC,CAAA;CACf"}
1
+ {"version":3,"file":"ObjectStore.d.ts","sourceRoot":"","sources":["../../src/ObjectStore.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAEjD,gFAAgF;AAChF,MAAM,WAAW,WAAW,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW;IAC9D,CAAC,CAAC,EAAE,MAAM,GAAG,CAAC,CAAA;CACf"}
@@ -1,4 +1,11 @@
1
1
  import type { Logger } from '@xylabs/logger';
2
2
  import { type IndexDescription } from './IndexDescription.ts';
3
+ /**
4
+ * Checks whether any store in the database needs an upgrade and returns the appropriate version number.
5
+ * @param dbName The name of the database to check
6
+ * @param stores Map of store names to their expected index descriptions
7
+ * @param logger Optional logger for diagnostics
8
+ * @returns The version to open (current version + 1 if upgrade needed, otherwise current version)
9
+ */
3
10
  export declare function checkDbNeedsUpgrade(dbName: string, stores: Record<string, IndexDescription[]>, logger?: Logger): Promise<number>;
4
11
  //# sourceMappingURL=checkDbNeedsUpgrade.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"checkDbNeedsUpgrade.d.ts","sourceRoot":"","sources":["../../src/checkDbNeedsUpgrade.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAG5C,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAI7D,wBAAsB,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,EAAE,CAAC,EAAE,MAAM,CAAC,EAAE,MAAM,mBAUpH"}
1
+ {"version":3,"file":"checkDbNeedsUpgrade.d.ts","sourceRoot":"","sources":["../../src/checkDbNeedsUpgrade.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAG5C,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAI7D;;;;;;GAMG;AACH,wBAAsB,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,gBAAgB,EAAE,CAAC,EAAE,MAAM,CAAC,EAAE,MAAM,mBAUpH"}
@@ -2,5 +2,13 @@ import type { Logger } from '@xylabs/logger';
2
2
  import type { IDBPDatabase } from 'idb';
3
3
  import { type IndexDescription } from './IndexDescription.ts';
4
4
  import type { ObjectStore } from './ObjectStore.ts';
5
+ /**
6
+ * Checks whether a store needs an upgrade by comparing its existing indexes against expected indexes.
7
+ * @param db The IndexedDB database instance
8
+ * @param storeName The name of the store to check
9
+ * @param indexes The expected index descriptions
10
+ * @param logger Optional logger for diagnostics
11
+ * @returns True if the store is missing or has missing indexes
12
+ */
5
13
  export declare function checkStoreNeedsUpgrade(db: IDBPDatabase<ObjectStore<object>>, storeName: string, indexes: IndexDescription[], logger?: Logger): Promise<boolean>;
6
14
  //# sourceMappingURL=checkStoreNeedsUpgrade.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"checkStoreNeedsUpgrade.d.ts","sourceRoot":"","sources":["../../src/checkStoreNeedsUpgrade.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,KAAK,CAAA;AAGvC,OAAO,EAA0B,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACrF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEnD,wBAAsB,sBAAsB,CAAC,EAAE,EAAE,YAAY,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,EAAE,EAAE,MAAM,CAAC,EAAE,MAAM,oBAelJ"}
1
+ {"version":3,"file":"checkStoreNeedsUpgrade.d.ts","sourceRoot":"","sources":["../../src/checkStoreNeedsUpgrade.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,KAAK,CAAA;AAGvC,OAAO,EAA0B,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACrF,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEnD;;;;;;;GAOG;AACH,wBAAsB,sBAAsB,CAAC,EAAE,EAAE,YAAY,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,EAAE,EAAE,MAAM,CAAC,EAAE,MAAM,oBAelJ"}
@@ -1,5 +1,12 @@
1
1
  import type { Logger } from '@xylabs/logger';
2
2
  import type { DBSchema, IDBPDatabase, StoreNames } from 'idb';
3
3
  import { type IndexDescription } from './IndexDescription.ts';
4
+ /**
5
+ * Creates an object store with the specified indexes during a version upgrade transaction.
6
+ * @param db The IndexedDB database instance (during upgrade)
7
+ * @param storeName The name of the store to create
8
+ * @param indexes The index descriptions to create on the store
9
+ * @param logger Optional logger for diagnostics
10
+ */
4
11
  export declare function createStoreDuringUpgrade<DBTypes extends DBSchema | unknown = unknown>(db: IDBPDatabase<DBTypes>, storeName: StoreNames<DBTypes>, indexes: IndexDescription[], logger?: Logger): void;
5
12
  //# sourceMappingURL=createStoreDuringUpgrade.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"createStoreDuringUpgrade.d.ts","sourceRoot":"","sources":["../../src/createStoreDuringUpgrade.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EACV,QAAQ,EACR,YAAY,EAA+B,UAAU,EACtD,MAAM,KAAK,CAAA;AAEZ,OAAO,EAEL,KAAK,gBAAgB,EACtB,MAAM,uBAAuB,CAAA;AAE9B,wBAAgB,wBAAwB,CAAC,OAAO,SAAS,QAAQ,GAAG,OAAO,GAAG,OAAO,EACnF,EAAE,EAAE,YAAY,CAAC,OAAO,CAAC,EACzB,SAAS,EAAE,UAAU,CAAC,OAAO,CAAC,EAC9B,OAAO,EAAE,gBAAgB,EAAE,EAC3B,MAAM,CAAC,EAAE,MAAM,QA4BhB"}
1
+ {"version":3,"file":"createStoreDuringUpgrade.d.ts","sourceRoot":"","sources":["../../src/createStoreDuringUpgrade.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EACV,QAAQ,EACR,YAAY,EAA+B,UAAU,EACtD,MAAM,KAAK,CAAA;AAEZ,OAAO,EAEL,KAAK,gBAAgB,EACtB,MAAM,uBAAuB,CAAA;AAE9B;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,SAAS,QAAQ,GAAG,OAAO,GAAG,OAAO,EACnF,EAAE,EAAE,YAAY,CAAC,OAAO,CAAC,EACzB,SAAS,EAAE,UAAU,CAAC,OAAO,CAAC,EAC9B,OAAO,EAAE,gBAAgB,EAAE,EAC3B,MAAM,CAAC,EAAE,MAAM,QA4BhB"}