@xylabs/indexed-db 6.0.2 → 6.0.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/README.md +5 -5
- package/dist/browser/ObjectStore.d.ts +14 -1
- package/dist/browser/ObjectStore.d.ts.map +1 -1
- package/dist/browser/checkStoreNeedsUpgrade.d.ts +1 -2
- package/dist/browser/checkStoreNeedsUpgrade.d.ts.map +1 -1
- package/dist/browser/createStoreDuringUpgrade.d.ts +2 -2
- package/dist/browser/createStoreDuringUpgrade.d.ts.map +1 -1
- package/dist/browser/getExistingIndexes.d.ts +2 -4
- package/dist/browser/getExistingIndexes.d.ts.map +1 -1
- package/dist/browser/index.mjs +7 -6
- package/dist/browser/index.mjs.map +2 -2
- package/dist/browser/withDb.d.ts +2 -2
- package/dist/browser/withDb.d.ts.map +1 -1
- package/dist/browser/withDbByVersion.d.ts +2 -2
- package/dist/browser/withDbByVersion.d.ts.map +1 -1
- package/dist/neutral/ObjectStore.d.ts +14 -1
- package/dist/neutral/ObjectStore.d.ts.map +1 -1
- package/dist/neutral/checkStoreNeedsUpgrade.d.ts +1 -2
- package/dist/neutral/checkStoreNeedsUpgrade.d.ts.map +1 -1
- package/dist/neutral/createStoreDuringUpgrade.d.ts +2 -2
- package/dist/neutral/createStoreDuringUpgrade.d.ts.map +1 -1
- package/dist/neutral/getExistingIndexes.d.ts +2 -4
- package/dist/neutral/getExistingIndexes.d.ts.map +1 -1
- package/dist/neutral/index.mjs +7 -6
- package/dist/neutral/index.mjs.map +2 -2
- package/dist/neutral/withDb.d.ts +2 -2
- package/dist/neutral/withDb.d.ts.map +1 -1
- package/dist/neutral/withDbByVersion.d.ts +2 -2
- package/dist/neutral/withDbByVersion.d.ts.map +1 -1
- package/dist/node/ObjectStore.d.ts +14 -1
- package/dist/node/ObjectStore.d.ts.map +1 -1
- package/dist/node/checkStoreNeedsUpgrade.d.ts +1 -2
- package/dist/node/checkStoreNeedsUpgrade.d.ts.map +1 -1
- package/dist/node/createStoreDuringUpgrade.d.ts +2 -2
- package/dist/node/createStoreDuringUpgrade.d.ts.map +1 -1
- package/dist/node/getExistingIndexes.d.ts +2 -4
- package/dist/node/getExistingIndexes.d.ts.map +1 -1
- package/dist/node/index.mjs +7 -6
- package/dist/node/index.mjs.map +2 -2
- package/dist/node/withDb.d.ts +2 -2
- package/dist/node/withDb.d.ts.map +1 -1
- package/dist/node/withDbByVersion.d.ts +2 -2
- package/dist/node/withDbByVersion.d.ts.map +1 -1
- package/package.json +9 -9
package/README.md
CHANGED
|
@@ -326,7 +326,7 @@ The name of the database to check
|
|
|
326
326
|
|
|
327
327
|
### stores
|
|
328
328
|
|
|
329
|
-
`Record`\<`string`, [`IndexDescription`]
|
|
329
|
+
`Record`\<`string`, [`IndexDescription`][#../interfaces/IndexDescription]()\>
|
|
330
330
|
|
|
331
331
|
Map of store names to their expected index descriptions
|
|
332
332
|
|
|
@@ -380,7 +380,7 @@ The name of the store to create
|
|
|
380
380
|
|
|
381
381
|
### indexes
|
|
382
382
|
|
|
383
|
-
[`IndexDescription`]
|
|
383
|
+
[`IndexDescription`][#../interfaces/IndexDescription]()
|
|
384
384
|
|
|
385
385
|
The index descriptions to create on the store
|
|
386
386
|
|
|
@@ -438,7 +438,7 @@ Optional logger for diagnostics
|
|
|
438
438
|
|
|
439
439
|
## Returns
|
|
440
440
|
|
|
441
|
-
`Promise`\<[`IndexDescription`]
|
|
441
|
+
`Promise`\<[`IndexDescription`][#../interfaces/IndexDescription]() \| `null`\>
|
|
442
442
|
|
|
443
443
|
An array of index descriptions, or null if the store does not exist
|
|
444
444
|
|
|
@@ -486,7 +486,7 @@ Function to execute with the opened database
|
|
|
486
486
|
|
|
487
487
|
### expectedIndexes?
|
|
488
488
|
|
|
489
|
-
`Record`\<`string`, [`IndexDescription`]
|
|
489
|
+
`Record`\<`string`, [`IndexDescription`][#../interfaces/IndexDescription]()\>
|
|
490
490
|
|
|
491
491
|
Optional map of store names to their expected indexes (triggers upgrade check)
|
|
492
492
|
|
|
@@ -559,7 +559,7 @@ Optional specific version to open (undefined for latest)
|
|
|
559
559
|
|
|
560
560
|
### expectedIndexes?
|
|
561
561
|
|
|
562
|
-
`Record`\<`string`, [`IndexDescription`]
|
|
562
|
+
`Record`\<`string`, [`IndexDescription`][#../interfaces/IndexDescription]()\>
|
|
563
563
|
|
|
564
564
|
Optional map of store names to indexes to create during upgrade
|
|
565
565
|
|
|
@@ -1,4 +1,17 @@
|
|
|
1
1
|
import type { EmptyObject } from '@xylabs/object';
|
|
2
|
-
/**
|
|
2
|
+
/** IDB schema value shape for a single store (key/value/indexes), exported for callers that want it. */
|
|
3
|
+
export interface ObjectStoreValue<T extends object = EmptyObject> {
|
|
4
|
+
indexes?: Record<string, IDBValidKey>;
|
|
5
|
+
key: IDBValidKey;
|
|
6
|
+
value: T;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Generic IndexedDB schema type that maps store names to their value types.
|
|
10
|
+
*
|
|
11
|
+
* Intentionally does NOT extend `idb`'s `DBSchema` and does not embed `ObjectStoreValue` —
|
|
12
|
+
* `idb`'s `StoreNames<T>` filters out string index signatures via `KeyToKeyNoIndex`, so any type
|
|
13
|
+
* structurally matching `DBSchema` (i.e. whose values satisfy `DBSchemaValue`'s key/value/indexes
|
|
14
|
+
* shape) resolves `StoreNames<T>` to `never`. That makes runtime store-name strings unassignable.
|
|
15
|
+
*/
|
|
3
16
|
export type ObjectStore<T extends EmptyObject = EmptyObject> = Record<string, T>;
|
|
4
17
|
//# sourceMappingURL=ObjectStore.d.ts.map
|
|
@@ -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,
|
|
1
|
+
{"version":3,"file":"ObjectStore.d.ts","sourceRoot":"","sources":["../../src/ObjectStore.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAEjD,wGAAwG;AACxG,MAAM,WAAW,gBAAgB,CAAC,CAAC,SAAS,MAAM,GAAG,WAAW;IAC9D,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAA;IACrC,GAAG,EAAE,WAAW,CAAA;IAChB,KAAK,EAAE,CAAC,CAAA;CACT;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { Logger } from '@xylabs/logger';
|
|
2
2
|
import type { IDBPDatabase } from 'idb';
|
|
3
3
|
import { type IndexDescription } from './IndexDescription.ts';
|
|
4
|
-
import type { ObjectStore } from './ObjectStore.ts';
|
|
5
4
|
/**
|
|
6
5
|
* Checks whether a store needs an upgrade by comparing its existing indexes against expected indexes.
|
|
7
6
|
* @param db The IndexedDB database instance
|
|
@@ -10,5 +9,5 @@ import type { ObjectStore } from './ObjectStore.ts';
|
|
|
10
9
|
* @param logger Optional logger for diagnostics
|
|
11
10
|
* @returns True if the store is missing or has missing indexes
|
|
12
11
|
*/
|
|
13
|
-
export declare function checkStoreNeedsUpgrade(db: IDBPDatabase<
|
|
12
|
+
export declare function checkStoreNeedsUpgrade<DBTypes = unknown>(db: IDBPDatabase<DBTypes>, storeName: string, indexes: IndexDescription[], logger?: Logger): Promise<boolean>;
|
|
14
13
|
//# 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;
|
|
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;AAErF;;;;;;;GAOG;AACH,wBAAsB,sBAAsB,CAAC,OAAO,GAAG,OAAO,EAC5D,EAAE,EAAE,YAAY,CAAC,OAAO,CAAC,EACzB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,gBAAgB,EAAE,EAC3B,MAAM,CAAC,EAAE,MAAM,oBAgBhB"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Logger } from '@xylabs/logger';
|
|
2
|
-
import type {
|
|
2
|
+
import type { IDBPDatabase, StoreNames } from 'idb';
|
|
3
3
|
import { type IndexDescription } from './IndexDescription.ts';
|
|
4
4
|
/**
|
|
5
5
|
* Creates an object store with the specified indexes during a version upgrade transaction.
|
|
@@ -8,5 +8,5 @@ import { type IndexDescription } from './IndexDescription.ts';
|
|
|
8
8
|
* @param indexes The index descriptions to create on the store
|
|
9
9
|
* @param logger Optional logger for diagnostics
|
|
10
10
|
*/
|
|
11
|
-
export declare function createStoreDuringUpgrade<DBTypes
|
|
11
|
+
export declare function createStoreDuringUpgrade<DBTypes = unknown>(db: IDBPDatabase<DBTypes>, storeName: StoreNames<DBTypes>, indexes: IndexDescription[], logger?: Logger): void;
|
|
12
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,
|
|
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,YAAY,EAA+B,UAAU,EACtD,MAAM,KAAK,CAAA;AAEZ,OAAO,EAEL,KAAK,gBAAgB,EACtB,MAAM,uBAAuB,CAAA;AAE9B;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,GAAG,OAAO,EACxD,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,8 +1,6 @@
|
|
|
1
1
|
import type { Logger } from '@xylabs/logger';
|
|
2
|
-
import type {
|
|
3
|
-
import type { IDBPDatabase, StoreNames } from 'idb';
|
|
2
|
+
import type { IDBPDatabase } from 'idb';
|
|
4
3
|
import { type IndexDescription } from './IndexDescription.ts';
|
|
5
|
-
import type { ObjectStore } from './ObjectStore.ts';
|
|
6
4
|
/**
|
|
7
5
|
* Retrieves the existing index descriptions for a store. Accepts either a database instance or a database name.
|
|
8
6
|
* @param db The IndexedDB database instance or database name
|
|
@@ -10,5 +8,5 @@ import type { ObjectStore } from './ObjectStore.ts';
|
|
|
10
8
|
* @param logger Optional logger for diagnostics
|
|
11
9
|
* @returns An array of index descriptions, or null if the store does not exist
|
|
12
10
|
*/
|
|
13
|
-
export declare function getExistingIndexes<
|
|
11
|
+
export declare function getExistingIndexes<DBTypes = unknown>(db: IDBPDatabase<DBTypes> | string, storeName: string, logger?: Logger): Promise<IndexDescription[] | null>;
|
|
14
12
|
//# 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,
|
|
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,YAAY,EAAc,MAAM,KAAK,CAAA;AAEnD,OAAO,EACL,KAAK,gBAAgB,EAEtB,MAAM,uBAAuB,CAAA;AAmC9B;;;;;;GAMG;AACH,wBAAsB,kBAAkB,CAAC,OAAO,GAAG,OAAO,EACxD,EAAE,EAAE,YAAY,CAAC,OAAO,CAAC,GAAG,MAAM,EAClC,SAAS,EAAE,MAAM,EACjB,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,gBAAgB,EAAE,GAAG,IAAI,CAAC,CAQpC"}
|
package/dist/browser/index.mjs
CHANGED
|
@@ -16,7 +16,7 @@ var buildStandardIndexName = (index) => {
|
|
|
16
16
|
|
|
17
17
|
// src/createStoreDuringUpgrade.ts
|
|
18
18
|
function createStoreDuringUpgrade(db, storeName, indexes, logger) {
|
|
19
|
-
logger?.log(`Creating store ${storeName}`);
|
|
19
|
+
logger?.log(`Creating store ${String(storeName)}`);
|
|
20
20
|
let store;
|
|
21
21
|
try {
|
|
22
22
|
store = db.createObjectStore(storeName, {
|
|
@@ -24,17 +24,17 @@ function createStoreDuringUpgrade(db, storeName, indexes, logger) {
|
|
|
24
24
|
autoIncrement: true
|
|
25
25
|
});
|
|
26
26
|
} catch {
|
|
27
|
-
logger?.warn(`Failed to create store: ${storeName} already exists`);
|
|
27
|
+
logger?.warn(`Failed to create store: ${String(storeName)} already exists`);
|
|
28
28
|
return;
|
|
29
29
|
}
|
|
30
|
-
logger?.log(`Creating store: created ${storeName}`);
|
|
31
|
-
store.name = storeName;
|
|
30
|
+
logger?.log(`Creating store: created ${String(storeName)}`);
|
|
31
|
+
store.name = String(storeName);
|
|
32
32
|
for (const {
|
|
33
33
|
key,
|
|
34
34
|
multiEntry,
|
|
35
35
|
unique
|
|
36
36
|
} of indexes) {
|
|
37
|
-
logger?.log(`Creating store: index ${key}`);
|
|
37
|
+
logger?.log(`Creating store: index ${JSON.stringify(key)}`);
|
|
38
38
|
const indexKeys = Object.keys(key);
|
|
39
39
|
const keys = indexKeys.length === 1 ? indexKeys[0] : indexKeys;
|
|
40
40
|
const indexName = buildStandardIndexName({ key, unique });
|
|
@@ -115,7 +115,8 @@ async function withReadOnlyStore(db, storeName, callback, logger) {
|
|
|
115
115
|
// src/getExistingIndexes.ts
|
|
116
116
|
async function getExistingIndexesInternal(db, storeName, logger) {
|
|
117
117
|
logger?.log("getExistingIndexesInternal", storeName);
|
|
118
|
-
|
|
118
|
+
const typedStoreName = storeName;
|
|
119
|
+
return await withReadOnlyStore(db, typedStoreName, (store) => {
|
|
119
120
|
return store ? [...store.indexNames].map((indexName) => {
|
|
120
121
|
const index = store.index(indexName);
|
|
121
122
|
const key = {};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
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"],
|
|
4
|
-
"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 interface 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"],
|
|
5
|
-
"mappings": ";AAAA,SAAS,cAAc;;;ACEvB,SAAS,aAAa;
|
|
4
|
+
"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'\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<DBTypes = unknown>(\n db: IDBPDatabase<DBTypes>,\n storeName: string,\n indexes: IndexDescription[],\n logger?: Logger,\n) {\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 { IDBPDatabase, StoreNames } 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 = 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 interface 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 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 = unknown>(\n db: IDBPDatabase<DBTypes>,\n storeName: StoreNames<DBTypes>,\n indexes: IndexDescription[],\n logger?: Logger,\n) {\n logger?.log(`Creating store ${String(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: ${String(storeName)} already exists`)\n return\n }\n logger?.log(`Creating store: created ${String(storeName)}`)\n // Name the store\n store.name = String(storeName)\n // Create an index on the hash\n for (const {\n key, multiEntry, unique,\n } of indexes) {\n logger?.log(`Creating store: index ${JSON.stringify(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 { 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<DBTypes = unknown>(\n db: IDBPDatabase<DBTypes>,\n storeName: string,\n logger?: Logger,\n): Promise<IndexDescription[] | null> {\n logger?.log('getExistingIndexesInternal', storeName)\n const typedStoreName = storeName as StoreNames<DBTypes>\n return await withReadOnlyStore(db as unknown as IDBPDatabase<ObjectStore>, typedStoreName as StoreNames<ObjectStore>, (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<DBTypes = unknown>(\n db: IDBPDatabase<DBTypes> | string,\n storeName: string,\n logger?: Logger,\n): Promise<IndexDescription[] | null> {\n logger?.log('getExistingIndexes', storeName)\n if (typeof db === 'string') {\n return await withDbByVersion<DBTypes, 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 { 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 = 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"],
|
|
5
|
+
"mappings": ";AAAA,SAAS,cAAc;;;ACEvB,SAAS,aAAa;AAEtB,SAAS,cAAc;;;ACoBhB,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;;;ACpBO,SAAS,yBACd,IACA,WACA,SACA,QACA;AACA,UAAQ,IAAI,kBAAkB,OAAO,SAAS,CAAC,EAAE;AAEjD,MAAI;AACJ,MAAI;AACF,YAAQ,GAAG,kBAAkB,WAAW;AAAA;AAAA,MAEtC,eAAe;AAAA,IACjB,CAAC;AAAA,EACH,QAAQ;AACN,YAAQ,KAAK,2BAA2B,OAAO,SAAS,CAAC,iBAAiB;AAC1E;AAAA,EACF;AACA,UAAQ,IAAI,2BAA2B,OAAO,SAAS,CAAC,EAAE;AAE1D,QAAM,OAAO,OAAO,SAA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,KAAK,OAAO,CAAC,OAAO;AAE/B,aAAO,GAAG,IAAI,KAAK,WAAW,GAAG,KAAK;AAAA,IAExC,CAAC;AAAA,EACH;AAAA;AAAA,EAGA,MAAM,OAAmC;AACvC,WAAO,MAAM,KAAK,OAAO,CAAC,OAAO;AAC/B,aAAO,GAAG,WAAW,KAAK,SAAS;AAAA,IACrC,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,IAAI,KAAqB,OAAwC;AACrE,WAAO,MAAM,KAAK,OAAO,OAAO,OAAO;AACrC,YAAM,GAAG,IAAI,KAAK,WAAW,OAAO,GAAG;AAAA,IACzC,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,OACJ,UAEA;AACA,WAAO,MAAM,OAAa,KAAK,QAAQ,CAAC,OAAO;AAC7C,aAAO,SAAS,EAAE;AAAA,IACpB,CAAC;AAAA,EACH;AACF;;;AClEA,eAAsB,mBACpB,IACA,WACA,UACA,QACY;AACZ,SAAO,MAAM,UAAU,IAAI,WAAW,UAAU,aAAa,MAAM;AACrE;",
|
|
6
6
|
"names": ["db", "db", "Mutex", "dbMutexes", "Mutex"]
|
|
7
7
|
}
|
package/dist/browser/withDb.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Logger } from '@xylabs/logger';
|
|
2
2
|
import type { EmptyObject } from '@xylabs/object';
|
|
3
|
-
import type {
|
|
3
|
+
import type { IDBPDatabase } from 'idb';
|
|
4
4
|
import { type IndexDescription } from './IndexDescription.ts';
|
|
5
5
|
/**
|
|
6
6
|
* Opens an IndexedDB database, automatically upgrading if needed, and passes it to the callback.
|
|
@@ -12,5 +12,5 @@ import { type IndexDescription } from './IndexDescription.ts';
|
|
|
12
12
|
* @param lock Whether to use a mutex to serialize access (defaults to true)
|
|
13
13
|
* @returns The result of the callback
|
|
14
14
|
*/
|
|
15
|
-
export declare function withDb<DBTypes
|
|
15
|
+
export declare function withDb<DBTypes = unknown, R = EmptyObject>(dbName: string, callback: (db: IDBPDatabase<DBTypes>) => Promise<R> | R, expectedIndexes?: Record<string, IndexDescription[]>, logger?: Logger, lock?: boolean): Promise<R>;
|
|
16
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,
|
|
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,YAAY,EAAE,MAAM,KAAK,CAAA;AAGvC,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAK7D;;;;;;;;;GASG;AACH,wBAAsB,MAAM,CAAC,OAAO,GAAG,OAAO,EAAE,CAAC,GAAG,WAAW,EAC7D,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,6 +1,6 @@
|
|
|
1
1
|
import type { Logger } from '@xylabs/logger';
|
|
2
2
|
import type { EmptyObject } from '@xylabs/object';
|
|
3
|
-
import type {
|
|
3
|
+
import type { IDBPDatabase } from 'idb';
|
|
4
4
|
import { type IndexDescription } from './IndexDescription.ts';
|
|
5
5
|
/**
|
|
6
6
|
* Opens an IndexedDB database at a specific version, handling upgrade events, and passes it to the callback.
|
|
@@ -13,5 +13,5 @@ import { type IndexDescription } from './IndexDescription.ts';
|
|
|
13
13
|
* @param lock Whether to use a mutex to serialize access (defaults to true)
|
|
14
14
|
* @returns The result of the callback
|
|
15
15
|
*/
|
|
16
|
-
export declare function withDbByVersion<DBTypes
|
|
16
|
+
export declare function withDbByVersion<DBTypes = unknown, R = EmptyObject>(dbName: string, callback: (db: IDBPDatabase<DBTypes>) => Promise<R> | R, version?: number, expectedIndexes?: Record<string, IndexDescription[]>, logger?: Logger, lock?: boolean): Promise<R>;
|
|
17
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,
|
|
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,EAAE,YAAY,EAAc,MAAM,KAAK,CAAA;AAInD,OAAO,EAA0B,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAIrF;;;;;;;;;;GAUG;AACH,wBAAsB,eAAe,CAAC,OAAO,GAAG,OAAO,EAAE,CAAC,GAAG,WAAW,EACtE,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,4 +1,17 @@
|
|
|
1
1
|
import type { EmptyObject } from '@xylabs/object';
|
|
2
|
-
/**
|
|
2
|
+
/** IDB schema value shape for a single store (key/value/indexes), exported for callers that want it. */
|
|
3
|
+
export interface ObjectStoreValue<T extends object = EmptyObject> {
|
|
4
|
+
indexes?: Record<string, IDBValidKey>;
|
|
5
|
+
key: IDBValidKey;
|
|
6
|
+
value: T;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Generic IndexedDB schema type that maps store names to their value types.
|
|
10
|
+
*
|
|
11
|
+
* Intentionally does NOT extend `idb`'s `DBSchema` and does not embed `ObjectStoreValue` —
|
|
12
|
+
* `idb`'s `StoreNames<T>` filters out string index signatures via `KeyToKeyNoIndex`, so any type
|
|
13
|
+
* structurally matching `DBSchema` (i.e. whose values satisfy `DBSchemaValue`'s key/value/indexes
|
|
14
|
+
* shape) resolves `StoreNames<T>` to `never`. That makes runtime store-name strings unassignable.
|
|
15
|
+
*/
|
|
3
16
|
export type ObjectStore<T extends EmptyObject = EmptyObject> = Record<string, T>;
|
|
4
17
|
//# sourceMappingURL=ObjectStore.d.ts.map
|
|
@@ -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,
|
|
1
|
+
{"version":3,"file":"ObjectStore.d.ts","sourceRoot":"","sources":["../../src/ObjectStore.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAEjD,wGAAwG;AACxG,MAAM,WAAW,gBAAgB,CAAC,CAAC,SAAS,MAAM,GAAG,WAAW;IAC9D,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAA;IACrC,GAAG,EAAE,WAAW,CAAA;IAChB,KAAK,EAAE,CAAC,CAAA;CACT;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { Logger } from '@xylabs/logger';
|
|
2
2
|
import type { IDBPDatabase } from 'idb';
|
|
3
3
|
import { type IndexDescription } from './IndexDescription.ts';
|
|
4
|
-
import type { ObjectStore } from './ObjectStore.ts';
|
|
5
4
|
/**
|
|
6
5
|
* Checks whether a store needs an upgrade by comparing its existing indexes against expected indexes.
|
|
7
6
|
* @param db The IndexedDB database instance
|
|
@@ -10,5 +9,5 @@ import type { ObjectStore } from './ObjectStore.ts';
|
|
|
10
9
|
* @param logger Optional logger for diagnostics
|
|
11
10
|
* @returns True if the store is missing or has missing indexes
|
|
12
11
|
*/
|
|
13
|
-
export declare function checkStoreNeedsUpgrade(db: IDBPDatabase<
|
|
12
|
+
export declare function checkStoreNeedsUpgrade<DBTypes = unknown>(db: IDBPDatabase<DBTypes>, storeName: string, indexes: IndexDescription[], logger?: Logger): Promise<boolean>;
|
|
14
13
|
//# 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;
|
|
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;AAErF;;;;;;;GAOG;AACH,wBAAsB,sBAAsB,CAAC,OAAO,GAAG,OAAO,EAC5D,EAAE,EAAE,YAAY,CAAC,OAAO,CAAC,EACzB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,gBAAgB,EAAE,EAC3B,MAAM,CAAC,EAAE,MAAM,oBAgBhB"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Logger } from '@xylabs/logger';
|
|
2
|
-
import type {
|
|
2
|
+
import type { IDBPDatabase, StoreNames } from 'idb';
|
|
3
3
|
import { type IndexDescription } from './IndexDescription.ts';
|
|
4
4
|
/**
|
|
5
5
|
* Creates an object store with the specified indexes during a version upgrade transaction.
|
|
@@ -8,5 +8,5 @@ import { type IndexDescription } from './IndexDescription.ts';
|
|
|
8
8
|
* @param indexes The index descriptions to create on the store
|
|
9
9
|
* @param logger Optional logger for diagnostics
|
|
10
10
|
*/
|
|
11
|
-
export declare function createStoreDuringUpgrade<DBTypes
|
|
11
|
+
export declare function createStoreDuringUpgrade<DBTypes = unknown>(db: IDBPDatabase<DBTypes>, storeName: StoreNames<DBTypes>, indexes: IndexDescription[], logger?: Logger): void;
|
|
12
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,
|
|
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,YAAY,EAA+B,UAAU,EACtD,MAAM,KAAK,CAAA;AAEZ,OAAO,EAEL,KAAK,gBAAgB,EACtB,MAAM,uBAAuB,CAAA;AAE9B;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,GAAG,OAAO,EACxD,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,8 +1,6 @@
|
|
|
1
1
|
import type { Logger } from '@xylabs/logger';
|
|
2
|
-
import type {
|
|
3
|
-
import type { IDBPDatabase, StoreNames } from 'idb';
|
|
2
|
+
import type { IDBPDatabase } from 'idb';
|
|
4
3
|
import { type IndexDescription } from './IndexDescription.ts';
|
|
5
|
-
import type { ObjectStore } from './ObjectStore.ts';
|
|
6
4
|
/**
|
|
7
5
|
* Retrieves the existing index descriptions for a store. Accepts either a database instance or a database name.
|
|
8
6
|
* @param db The IndexedDB database instance or database name
|
|
@@ -10,5 +8,5 @@ import type { ObjectStore } from './ObjectStore.ts';
|
|
|
10
8
|
* @param logger Optional logger for diagnostics
|
|
11
9
|
* @returns An array of index descriptions, or null if the store does not exist
|
|
12
10
|
*/
|
|
13
|
-
export declare function getExistingIndexes<
|
|
11
|
+
export declare function getExistingIndexes<DBTypes = unknown>(db: IDBPDatabase<DBTypes> | string, storeName: string, logger?: Logger): Promise<IndexDescription[] | null>;
|
|
14
12
|
//# 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,
|
|
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,YAAY,EAAc,MAAM,KAAK,CAAA;AAEnD,OAAO,EACL,KAAK,gBAAgB,EAEtB,MAAM,uBAAuB,CAAA;AAmC9B;;;;;;GAMG;AACH,wBAAsB,kBAAkB,CAAC,OAAO,GAAG,OAAO,EACxD,EAAE,EAAE,YAAY,CAAC,OAAO,CAAC,GAAG,MAAM,EAClC,SAAS,EAAE,MAAM,EACjB,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,gBAAgB,EAAE,GAAG,IAAI,CAAC,CAQpC"}
|
package/dist/neutral/index.mjs
CHANGED
|
@@ -16,7 +16,7 @@ var buildStandardIndexName = (index) => {
|
|
|
16
16
|
|
|
17
17
|
// src/createStoreDuringUpgrade.ts
|
|
18
18
|
function createStoreDuringUpgrade(db, storeName, indexes, logger) {
|
|
19
|
-
logger?.log(`Creating store ${storeName}`);
|
|
19
|
+
logger?.log(`Creating store ${String(storeName)}`);
|
|
20
20
|
let store;
|
|
21
21
|
try {
|
|
22
22
|
store = db.createObjectStore(storeName, {
|
|
@@ -24,17 +24,17 @@ function createStoreDuringUpgrade(db, storeName, indexes, logger) {
|
|
|
24
24
|
autoIncrement: true
|
|
25
25
|
});
|
|
26
26
|
} catch {
|
|
27
|
-
logger?.warn(`Failed to create store: ${storeName} already exists`);
|
|
27
|
+
logger?.warn(`Failed to create store: ${String(storeName)} already exists`);
|
|
28
28
|
return;
|
|
29
29
|
}
|
|
30
|
-
logger?.log(`Creating store: created ${storeName}`);
|
|
31
|
-
store.name = storeName;
|
|
30
|
+
logger?.log(`Creating store: created ${String(storeName)}`);
|
|
31
|
+
store.name = String(storeName);
|
|
32
32
|
for (const {
|
|
33
33
|
key,
|
|
34
34
|
multiEntry,
|
|
35
35
|
unique
|
|
36
36
|
} of indexes) {
|
|
37
|
-
logger?.log(`Creating store: index ${key}`);
|
|
37
|
+
logger?.log(`Creating store: index ${JSON.stringify(key)}`);
|
|
38
38
|
const indexKeys = Object.keys(key);
|
|
39
39
|
const keys = indexKeys.length === 1 ? indexKeys[0] : indexKeys;
|
|
40
40
|
const indexName = buildStandardIndexName({ key, unique });
|
|
@@ -115,7 +115,8 @@ async function withReadOnlyStore(db, storeName, callback, logger) {
|
|
|
115
115
|
// src/getExistingIndexes.ts
|
|
116
116
|
async function getExistingIndexesInternal(db, storeName, logger) {
|
|
117
117
|
logger?.log("getExistingIndexesInternal", storeName);
|
|
118
|
-
|
|
118
|
+
const typedStoreName = storeName;
|
|
119
|
+
return await withReadOnlyStore(db, typedStoreName, (store) => {
|
|
119
120
|
return store ? [...store.indexNames].map((indexName) => {
|
|
120
121
|
const index = store.index(indexName);
|
|
121
122
|
const key = {};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
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"],
|
|
4
|
-
"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 interface 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"],
|
|
5
|
-
"mappings": ";AAAA,SAAS,cAAc;;;ACEvB,SAAS,aAAa;
|
|
4
|
+
"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'\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<DBTypes = unknown>(\n db: IDBPDatabase<DBTypes>,\n storeName: string,\n indexes: IndexDescription[],\n logger?: Logger,\n) {\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 { IDBPDatabase, StoreNames } 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 = 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 interface 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 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 = unknown>(\n db: IDBPDatabase<DBTypes>,\n storeName: StoreNames<DBTypes>,\n indexes: IndexDescription[],\n logger?: Logger,\n) {\n logger?.log(`Creating store ${String(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: ${String(storeName)} already exists`)\n return\n }\n logger?.log(`Creating store: created ${String(storeName)}`)\n // Name the store\n store.name = String(storeName)\n // Create an index on the hash\n for (const {\n key, multiEntry, unique,\n } of indexes) {\n logger?.log(`Creating store: index ${JSON.stringify(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 { 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<DBTypes = unknown>(\n db: IDBPDatabase<DBTypes>,\n storeName: string,\n logger?: Logger,\n): Promise<IndexDescription[] | null> {\n logger?.log('getExistingIndexesInternal', storeName)\n const typedStoreName = storeName as StoreNames<DBTypes>\n return await withReadOnlyStore(db as unknown as IDBPDatabase<ObjectStore>, typedStoreName as StoreNames<ObjectStore>, (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<DBTypes = unknown>(\n db: IDBPDatabase<DBTypes> | string,\n storeName: string,\n logger?: Logger,\n): Promise<IndexDescription[] | null> {\n logger?.log('getExistingIndexes', storeName)\n if (typeof db === 'string') {\n return await withDbByVersion<DBTypes, 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 { 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 = 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"],
|
|
5
|
+
"mappings": ";AAAA,SAAS,cAAc;;;ACEvB,SAAS,aAAa;AAEtB,SAAS,cAAc;;;ACoBhB,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;;;ACpBO,SAAS,yBACd,IACA,WACA,SACA,QACA;AACA,UAAQ,IAAI,kBAAkB,OAAO,SAAS,CAAC,EAAE;AAEjD,MAAI;AACJ,MAAI;AACF,YAAQ,GAAG,kBAAkB,WAAW;AAAA;AAAA,MAEtC,eAAe;AAAA,IACjB,CAAC;AAAA,EACH,QAAQ;AACN,YAAQ,KAAK,2BAA2B,OAAO,SAAS,CAAC,iBAAiB;AAC1E;AAAA,EACF;AACA,UAAQ,IAAI,2BAA2B,OAAO,SAAS,CAAC,EAAE;AAE1D,QAAM,OAAO,OAAO,SAAS;AAE7B,aAAW;AAAA,IACT;AAAA,IA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AO,GAAG,IAAI,KAAK,WAAW,GAAG,KAAK;AAAA,IAExC,CAAC;AAAA,EACH;AAAA;AAAA,EAGA,MAAM,OAAmC;AACvC,WAAO,MAAM,KAAK,OAAO,CAAC,OAAO;AAC/B,aAAO,GAAG,WAAW,KAAK,SAAS;AAAA,IACrC,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,IAAI,KAAqB,OAAwC;AACrE,WAAO,MAAM,KAAK,OAAO,OAAO,OAAO;AACrC,YAAM,GAAG,IAAI,KAAK,WAAW,OAAO,GAAG;AAAA,IACzC,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,OACJ,UAEA;AACA,WAAO,MAAM,OAAa,KAAK,QAAQ,CAAC,OAAO;AAC7C,aAAO,SAAS,EAAE;AAAA,IACpB,CAAC;AAAA,EACH;AACF;;;AClEA,eAAsB,mBACpB,IACA,WACA,UACA,QACY;AACZ,SAAO,MAAM,UAAU,IAAI,WAAW,UAAU,aAAa,MAAM;AACrE;",
|
|
6
6
|
"names": ["db", "db", "Mutex", "dbMutexes", "Mutex"]
|
|
7
7
|
}
|
package/dist/neutral/withDb.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Logger } from '@xylabs/logger';
|
|
2
2
|
import type { EmptyObject } from '@xylabs/object';
|
|
3
|
-
import type {
|
|
3
|
+
import type { IDBPDatabase } from 'idb';
|
|
4
4
|
import { type IndexDescription } from './IndexDescription.ts';
|
|
5
5
|
/**
|
|
6
6
|
* Opens an IndexedDB database, automatically upgrading if needed, and passes it to the callback.
|
|
@@ -12,5 +12,5 @@ import { type IndexDescription } from './IndexDescription.ts';
|
|
|
12
12
|
* @param lock Whether to use a mutex to serialize access (defaults to true)
|
|
13
13
|
* @returns The result of the callback
|
|
14
14
|
*/
|
|
15
|
-
export declare function withDb<DBTypes
|
|
15
|
+
export declare function withDb<DBTypes = unknown, R = EmptyObject>(dbName: string, callback: (db: IDBPDatabase<DBTypes>) => Promise<R> | R, expectedIndexes?: Record<string, IndexDescription[]>, logger?: Logger, lock?: boolean): Promise<R>;
|
|
16
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,
|
|
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,YAAY,EAAE,MAAM,KAAK,CAAA;AAGvC,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAK7D;;;;;;;;;GASG;AACH,wBAAsB,MAAM,CAAC,OAAO,GAAG,OAAO,EAAE,CAAC,GAAG,WAAW,EAC7D,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,6 +1,6 @@
|
|
|
1
1
|
import type { Logger } from '@xylabs/logger';
|
|
2
2
|
import type { EmptyObject } from '@xylabs/object';
|
|
3
|
-
import type {
|
|
3
|
+
import type { IDBPDatabase } from 'idb';
|
|
4
4
|
import { type IndexDescription } from './IndexDescription.ts';
|
|
5
5
|
/**
|
|
6
6
|
* Opens an IndexedDB database at a specific version, handling upgrade events, and passes it to the callback.
|
|
@@ -13,5 +13,5 @@ import { type IndexDescription } from './IndexDescription.ts';
|
|
|
13
13
|
* @param lock Whether to use a mutex to serialize access (defaults to true)
|
|
14
14
|
* @returns The result of the callback
|
|
15
15
|
*/
|
|
16
|
-
export declare function withDbByVersion<DBTypes
|
|
16
|
+
export declare function withDbByVersion<DBTypes = unknown, R = EmptyObject>(dbName: string, callback: (db: IDBPDatabase<DBTypes>) => Promise<R> | R, version?: number, expectedIndexes?: Record<string, IndexDescription[]>, logger?: Logger, lock?: boolean): Promise<R>;
|
|
17
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,
|
|
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,EAAE,YAAY,EAAc,MAAM,KAAK,CAAA;AAInD,OAAO,EAA0B,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAIrF;;;;;;;;;;GAUG;AACH,wBAAsB,eAAe,CAAC,OAAO,GAAG,OAAO,EAAE,CAAC,GAAG,WAAW,EACtE,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,4 +1,17 @@
|
|
|
1
1
|
import type { EmptyObject } from '@xylabs/object';
|
|
2
|
-
/**
|
|
2
|
+
/** IDB schema value shape for a single store (key/value/indexes), exported for callers that want it. */
|
|
3
|
+
export interface ObjectStoreValue<T extends object = EmptyObject> {
|
|
4
|
+
indexes?: Record<string, IDBValidKey>;
|
|
5
|
+
key: IDBValidKey;
|
|
6
|
+
value: T;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Generic IndexedDB schema type that maps store names to their value types.
|
|
10
|
+
*
|
|
11
|
+
* Intentionally does NOT extend `idb`'s `DBSchema` and does not embed `ObjectStoreValue` —
|
|
12
|
+
* `idb`'s `StoreNames<T>` filters out string index signatures via `KeyToKeyNoIndex`, so any type
|
|
13
|
+
* structurally matching `DBSchema` (i.e. whose values satisfy `DBSchemaValue`'s key/value/indexes
|
|
14
|
+
* shape) resolves `StoreNames<T>` to `never`. That makes runtime store-name strings unassignable.
|
|
15
|
+
*/
|
|
3
16
|
export type ObjectStore<T extends EmptyObject = EmptyObject> = Record<string, T>;
|
|
4
17
|
//# sourceMappingURL=ObjectStore.d.ts.map
|
|
@@ -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,
|
|
1
|
+
{"version":3,"file":"ObjectStore.d.ts","sourceRoot":"","sources":["../../src/ObjectStore.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAEjD,wGAAwG;AACxG,MAAM,WAAW,gBAAgB,CAAC,CAAC,SAAS,MAAM,GAAG,WAAW;IAC9D,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAA;IACrC,GAAG,EAAE,WAAW,CAAA;IAChB,KAAK,EAAE,CAAC,CAAA;CACT;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAA"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { Logger } from '@xylabs/logger';
|
|
2
2
|
import type { IDBPDatabase } from 'idb';
|
|
3
3
|
import { type IndexDescription } from './IndexDescription.ts';
|
|
4
|
-
import type { ObjectStore } from './ObjectStore.ts';
|
|
5
4
|
/**
|
|
6
5
|
* Checks whether a store needs an upgrade by comparing its existing indexes against expected indexes.
|
|
7
6
|
* @param db The IndexedDB database instance
|
|
@@ -10,5 +9,5 @@ import type { ObjectStore } from './ObjectStore.ts';
|
|
|
10
9
|
* @param logger Optional logger for diagnostics
|
|
11
10
|
* @returns True if the store is missing or has missing indexes
|
|
12
11
|
*/
|
|
13
|
-
export declare function checkStoreNeedsUpgrade(db: IDBPDatabase<
|
|
12
|
+
export declare function checkStoreNeedsUpgrade<DBTypes = unknown>(db: IDBPDatabase<DBTypes>, storeName: string, indexes: IndexDescription[], logger?: Logger): Promise<boolean>;
|
|
14
13
|
//# 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;
|
|
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;AAErF;;;;;;;GAOG;AACH,wBAAsB,sBAAsB,CAAC,OAAO,GAAG,OAAO,EAC5D,EAAE,EAAE,YAAY,CAAC,OAAO,CAAC,EACzB,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,gBAAgB,EAAE,EAC3B,MAAM,CAAC,EAAE,MAAM,oBAgBhB"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Logger } from '@xylabs/logger';
|
|
2
|
-
import type {
|
|
2
|
+
import type { IDBPDatabase, StoreNames } from 'idb';
|
|
3
3
|
import { type IndexDescription } from './IndexDescription.ts';
|
|
4
4
|
/**
|
|
5
5
|
* Creates an object store with the specified indexes during a version upgrade transaction.
|
|
@@ -8,5 +8,5 @@ import { type IndexDescription } from './IndexDescription.ts';
|
|
|
8
8
|
* @param indexes The index descriptions to create on the store
|
|
9
9
|
* @param logger Optional logger for diagnostics
|
|
10
10
|
*/
|
|
11
|
-
export declare function createStoreDuringUpgrade<DBTypes
|
|
11
|
+
export declare function createStoreDuringUpgrade<DBTypes = unknown>(db: IDBPDatabase<DBTypes>, storeName: StoreNames<DBTypes>, indexes: IndexDescription[], logger?: Logger): void;
|
|
12
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,
|
|
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,YAAY,EAA+B,UAAU,EACtD,MAAM,KAAK,CAAA;AAEZ,OAAO,EAEL,KAAK,gBAAgB,EACtB,MAAM,uBAAuB,CAAA;AAE9B;;;;;;GAMG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,GAAG,OAAO,EACxD,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,8 +1,6 @@
|
|
|
1
1
|
import type { Logger } from '@xylabs/logger';
|
|
2
|
-
import type {
|
|
3
|
-
import type { IDBPDatabase, StoreNames } from 'idb';
|
|
2
|
+
import type { IDBPDatabase } from 'idb';
|
|
4
3
|
import { type IndexDescription } from './IndexDescription.ts';
|
|
5
|
-
import type { ObjectStore } from './ObjectStore.ts';
|
|
6
4
|
/**
|
|
7
5
|
* Retrieves the existing index descriptions for a store. Accepts either a database instance or a database name.
|
|
8
6
|
* @param db The IndexedDB database instance or database name
|
|
@@ -10,5 +8,5 @@ import type { ObjectStore } from './ObjectStore.ts';
|
|
|
10
8
|
* @param logger Optional logger for diagnostics
|
|
11
9
|
* @returns An array of index descriptions, or null if the store does not exist
|
|
12
10
|
*/
|
|
13
|
-
export declare function getExistingIndexes<
|
|
11
|
+
export declare function getExistingIndexes<DBTypes = unknown>(db: IDBPDatabase<DBTypes> | string, storeName: string, logger?: Logger): Promise<IndexDescription[] | null>;
|
|
14
12
|
//# 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,
|
|
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,YAAY,EAAc,MAAM,KAAK,CAAA;AAEnD,OAAO,EACL,KAAK,gBAAgB,EAEtB,MAAM,uBAAuB,CAAA;AAmC9B;;;;;;GAMG;AACH,wBAAsB,kBAAkB,CAAC,OAAO,GAAG,OAAO,EACxD,EAAE,EAAE,YAAY,CAAC,OAAO,CAAC,GAAG,MAAM,EAClC,SAAS,EAAE,MAAM,EACjB,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,gBAAgB,EAAE,GAAG,IAAI,CAAC,CAQpC"}
|
package/dist/node/index.mjs
CHANGED
|
@@ -16,7 +16,7 @@ var buildStandardIndexName = (index) => {
|
|
|
16
16
|
|
|
17
17
|
// src/createStoreDuringUpgrade.ts
|
|
18
18
|
function createStoreDuringUpgrade(db, storeName, indexes, logger) {
|
|
19
|
-
logger?.log(`Creating store ${storeName}`);
|
|
19
|
+
logger?.log(`Creating store ${String(storeName)}`);
|
|
20
20
|
let store;
|
|
21
21
|
try {
|
|
22
22
|
store = db.createObjectStore(storeName, {
|
|
@@ -24,17 +24,17 @@ function createStoreDuringUpgrade(db, storeName, indexes, logger) {
|
|
|
24
24
|
autoIncrement: true
|
|
25
25
|
});
|
|
26
26
|
} catch {
|
|
27
|
-
logger?.warn(`Failed to create store: ${storeName} already exists`);
|
|
27
|
+
logger?.warn(`Failed to create store: ${String(storeName)} already exists`);
|
|
28
28
|
return;
|
|
29
29
|
}
|
|
30
|
-
logger?.log(`Creating store: created ${storeName}`);
|
|
31
|
-
store.name = storeName;
|
|
30
|
+
logger?.log(`Creating store: created ${String(storeName)}`);
|
|
31
|
+
store.name = String(storeName);
|
|
32
32
|
for (const {
|
|
33
33
|
key,
|
|
34
34
|
multiEntry,
|
|
35
35
|
unique
|
|
36
36
|
} of indexes) {
|
|
37
|
-
logger?.log(`Creating store: index ${key}`);
|
|
37
|
+
logger?.log(`Creating store: index ${JSON.stringify(key)}`);
|
|
38
38
|
const indexKeys = Object.keys(key);
|
|
39
39
|
const keys = indexKeys.length === 1 ? indexKeys[0] : indexKeys;
|
|
40
40
|
const indexName = buildStandardIndexName({ key, unique });
|
|
@@ -115,7 +115,8 @@ async function withReadOnlyStore(db, storeName, callback, logger) {
|
|
|
115
115
|
// src/getExistingIndexes.ts
|
|
116
116
|
async function getExistingIndexesInternal(db, storeName, logger) {
|
|
117
117
|
logger?.log("getExistingIndexesInternal", storeName);
|
|
118
|
-
|
|
118
|
+
const typedStoreName = storeName;
|
|
119
|
+
return await withReadOnlyStore(db, typedStoreName, (store) => {
|
|
119
120
|
return store ? [...store.indexNames].map((indexName) => {
|
|
120
121
|
const index = store.index(indexName);
|
|
121
122
|
const key = {};
|
package/dist/node/index.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
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"],
|
|
4
|
-
"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 interface 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"],
|
|
5
|
-
"mappings": ";AAAA,SAAS,cAAc;;;ACEvB,SAAS,aAAa;
|
|
4
|
+
"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'\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<DBTypes = unknown>(\n db: IDBPDatabase<DBTypes>,\n storeName: string,\n indexes: IndexDescription[],\n logger?: Logger,\n) {\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 { IDBPDatabase, StoreNames } 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 = 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 interface 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 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 = unknown>(\n db: IDBPDatabase<DBTypes>,\n storeName: StoreNames<DBTypes>,\n indexes: IndexDescription[],\n logger?: Logger,\n) {\n logger?.log(`Creating store ${String(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: ${String(storeName)} already exists`)\n return\n }\n logger?.log(`Creating store: created ${String(storeName)}`)\n // Name the store\n store.name = String(storeName)\n // Create an index on the hash\n for (const {\n key, multiEntry, unique,\n } of indexes) {\n logger?.log(`Creating store: index ${JSON.stringify(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 { 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<DBTypes = unknown>(\n db: IDBPDatabase<DBTypes>,\n storeName: string,\n logger?: Logger,\n): Promise<IndexDescription[] | null> {\n logger?.log('getExistingIndexesInternal', storeName)\n const typedStoreName = storeName as StoreNames<DBTypes>\n return await withReadOnlyStore(db as unknown as IDBPDatabase<ObjectStore>, typedStoreName as StoreNames<ObjectStore>, (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<DBTypes = unknown>(\n db: IDBPDatabase<DBTypes> | string,\n storeName: string,\n logger?: Logger,\n): Promise<IndexDescription[] | null> {\n logger?.log('getExistingIndexes', storeName)\n if (typeof db === 'string') {\n return await withDbByVersion<DBTypes, 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 { 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 = 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"],
|
|
5
|
+
"mappings": ";AAAA,SAAS,cAAc;;;ACEvB,SAAS,aAAa;AAEtB,SAAS,cAAc;;;ACoBhB,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;;;ACpBO,SAAS,yBACd,IACA,WACA,SACA,QACA;AACA,UAAQ,IAAI,kBAAkB,OAAO,SAAS,CAAC,EAAE;AAEjD,MAAI;AACJ,MAAI;AACF,YAAQ,GAAG,kBAAkB,WAAW;AAAA;AAAA,MAEtC,eAAe;AAAA,IACjB,CAAC;AAAA,EACH,QAAQ;AACN,YAAQ,KAAK,2BAA2B,OAAO,SAAS,CAAC,iBAAiB;AAC1E;AAAA,EACF;AACA,UAAQ,IAAI,2BAA2B,OAAO,SAAS,CAAC,EAAE;AAE1D,QAAM,OAAO,OAAO,SAAS;AAE7B,aAAW;AAAA,IACT;AAAA,IAAK;AAAA,I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AAI,KAAK,WAAW,GAAG,KAAK;AAAA,IAExC,CAAC;AAAA,EACH;AAAA;AAAA,EAGA,MAAM,OAAmC;AACvC,WAAO,MAAM,KAAK,OAAO,CAAC,OAAO;AAC/B,aAAO,GAAG,WAAW,KAAK,SAAS;AAAA,IACrC,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,IAAI,KAAqB,OAAwC;AACrE,WAAO,MAAM,KAAK,OAAO,OAAO,OAAO;AACrC,YAAM,GAAG,IAAI,KAAK,WAAW,OAAO,GAAG;AAAA,IACzC,CAAC;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,OACJ,UAEA;AACA,WAAO,MAAM,OAAa,KAAK,QAAQ,CAAC,OAAO;AAC7C,aAAO,SAAS,EAAE;AAAA,IACpB,CAAC;AAAA,EACH;AACF;;;AClEA,eAAsB,mBACpB,IACA,WACA,UACA,QACY;AACZ,SAAO,MAAM,UAAU,IAAI,WAAW,UAAU,aAAa,MAAM;AACrE;",
|
|
6
6
|
"names": ["db", "db", "Mutex", "dbMutexes", "Mutex"]
|
|
7
7
|
}
|
package/dist/node/withDb.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Logger } from '@xylabs/logger';
|
|
2
2
|
import type { EmptyObject } from '@xylabs/object';
|
|
3
|
-
import type {
|
|
3
|
+
import type { IDBPDatabase } from 'idb';
|
|
4
4
|
import { type IndexDescription } from './IndexDescription.ts';
|
|
5
5
|
/**
|
|
6
6
|
* Opens an IndexedDB database, automatically upgrading if needed, and passes it to the callback.
|
|
@@ -12,5 +12,5 @@ import { type IndexDescription } from './IndexDescription.ts';
|
|
|
12
12
|
* @param lock Whether to use a mutex to serialize access (defaults to true)
|
|
13
13
|
* @returns The result of the callback
|
|
14
14
|
*/
|
|
15
|
-
export declare function withDb<DBTypes
|
|
15
|
+
export declare function withDb<DBTypes = unknown, R = EmptyObject>(dbName: string, callback: (db: IDBPDatabase<DBTypes>) => Promise<R> | R, expectedIndexes?: Record<string, IndexDescription[]>, logger?: Logger, lock?: boolean): Promise<R>;
|
|
16
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,
|
|
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,YAAY,EAAE,MAAM,KAAK,CAAA;AAGvC,OAAO,EAAE,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAK7D;;;;;;;;;GASG;AACH,wBAAsB,MAAM,CAAC,OAAO,GAAG,OAAO,EAAE,CAAC,GAAG,WAAW,EAC7D,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,6 +1,6 @@
|
|
|
1
1
|
import type { Logger } from '@xylabs/logger';
|
|
2
2
|
import type { EmptyObject } from '@xylabs/object';
|
|
3
|
-
import type {
|
|
3
|
+
import type { IDBPDatabase } from 'idb';
|
|
4
4
|
import { type IndexDescription } from './IndexDescription.ts';
|
|
5
5
|
/**
|
|
6
6
|
* Opens an IndexedDB database at a specific version, handling upgrade events, and passes it to the callback.
|
|
@@ -13,5 +13,5 @@ import { type IndexDescription } from './IndexDescription.ts';
|
|
|
13
13
|
* @param lock Whether to use a mutex to serialize access (defaults to true)
|
|
14
14
|
* @returns The result of the callback
|
|
15
15
|
*/
|
|
16
|
-
export declare function withDbByVersion<DBTypes
|
|
16
|
+
export declare function withDbByVersion<DBTypes = unknown, R = EmptyObject>(dbName: string, callback: (db: IDBPDatabase<DBTypes>) => Promise<R> | R, version?: number, expectedIndexes?: Record<string, IndexDescription[]>, logger?: Logger, lock?: boolean): Promise<R>;
|
|
17
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,
|
|
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,EAAE,YAAY,EAAc,MAAM,KAAK,CAAA;AAInD,OAAO,EAA0B,KAAK,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AAIrF;;;;;;;;;;GAUG;AACH,wBAAsB,eAAe,CAAC,OAAO,GAAG,OAAO,EAAE,CAAC,GAAG,WAAW,EACtE,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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xylabs/indexed-db",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.4",
|
|
4
4
|
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"hex",
|
|
@@ -65,21 +65,21 @@
|
|
|
65
65
|
"README.md"
|
|
66
66
|
],
|
|
67
67
|
"dependencies": {
|
|
68
|
-
"@xylabs/
|
|
69
|
-
"@xylabs/
|
|
70
|
-
"@xylabs/object": "~6.0.
|
|
71
|
-
"@xylabs/
|
|
68
|
+
"@xylabs/logger": "~6.0.4",
|
|
69
|
+
"@xylabs/storage": "~6.0.4",
|
|
70
|
+
"@xylabs/object": "~6.0.4",
|
|
71
|
+
"@xylabs/exists": "~6.0.4"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
|
-
"@xylabs/toolchain": "^8.1.
|
|
75
|
-
"@xylabs/tsconfig": "^8.1.
|
|
76
|
-
"@xylabs/tsconfig-dom": "^8.1.
|
|
74
|
+
"@xylabs/toolchain": "^8.1.5",
|
|
75
|
+
"@xylabs/tsconfig": "^8.1.5",
|
|
76
|
+
"@xylabs/tsconfig-dom": "^8.1.5",
|
|
77
77
|
"async-mutex": "^0.5.0",
|
|
78
78
|
"eslint": "^10.4.0",
|
|
79
79
|
"fake-indexeddb": "^6.2.5",
|
|
80
80
|
"idb": "^8.0.3",
|
|
81
81
|
"typescript": "^6.0.3",
|
|
82
|
-
"vite": "^8.0.
|
|
82
|
+
"vite": "^8.0.14",
|
|
83
83
|
"vitest": "^4.1.7",
|
|
84
84
|
"zod": "^4.4.3"
|
|
85
85
|
},
|