@xylabs/indexed-db 4.4.28 → 4.4.29
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/IndexDescription.ts","../../src/createStore.ts","../../src/withReadOnlyStore.ts","../../src/getExistingIndexes.ts","../../src/withDb.ts","../../src/withReadWriteStore.ts"],"sourcesContent":["/**\n * The index direction (1 for ascending, -1 for descending)\n */\nexport type IndexDirection = -1 | 1\n\n/**\n * Description of index(es) to be created on a store\n */\nexport type IndexDescription = {\n /**\n * The key(s) to index\n */\n key: Record<string, IndexDirection>\n /**\n * Is the indexed value an array\n */\n multiEntry?: boolean\n /**\n * The name of the index\n */\n name?: string\n /**\n * If true, the index must enforce uniqueness on the key\n */\n unique?: boolean\n}\n\nexport const IndexSeparator = '-'\n\n/**\n * Given an index description, this will build the index\n * name in standard form\n * @param index The index description\n * @returns The index name in standard form\n */\nexport const buildStandardIndexName = (index: IndexDescription) => {\n const { key, unique } = index\n const prefix = unique ? 'UX' : 'IX'\n const indexKeys = Object.keys(key)\n return `${prefix}_${indexKeys.join(IndexSeparator)}`\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\nimport type {\n IDBPDatabase, IndexNames, StoreNames,\n} from 'idb'\n\nimport {\n buildStandardIndexName,\n type IndexDescription,\n} from './IndexDescription.ts'\nimport type { ObjectStore } from './ObjectStore.ts'\n\nexport function createStore<T extends EmptyObject = EmptyObject>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n indexes: IndexDescription[],\n logger?: Logger,\n) {\n logger?.log(`Creating store ${storeName}`)\n // Create the store\n const store = db.createObjectStore(storeName, {\n // If it isn't explicitly set, create a value by auto incrementing.\n autoIncrement: true,\n })\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 const indexKeys = Object.keys(key)\n const keys = indexKeys.length === 1 ? indexKeys[0] : indexKeys\n const indexName = buildStandardIndexName({ key, unique }) as IndexNames<ObjectStore<T>, StoreNames<ObjectStore<T>>>\n console.log('createIndex', indexName, keys, { multiEntry, unique })\n store.createIndex(indexName, keys, { multiEntry, unique })\n }\n}\n","import type { EmptyObject } from '@xylabs/object'\nimport type {\n IDBPDatabase, IDBPObjectStore, StoreNames,\n} from 'idb'\n\nimport type { ObjectStore } from './ObjectStore.ts'\n\nexport async function withReadOnlyStore<T extends EmptyObject = EmptyObject, R = T>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n callback: (store: IDBPObjectStore<ObjectStore<T>, [StoreNames<ObjectStore<T>>], StoreNames<ObjectStore<T>>, string>) => Promise<R> | R,\n): Promise<R> {\n const transaction = db.transaction(storeName, 'readonly')\n const store = transaction.objectStore(storeName)\n try {\n return await callback(store)\n } finally {\n await transaction.done\n }\n}\n","import 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 { withReadOnlyStore } from './withReadOnlyStore.ts'\n\nexport async function getExistingIndexes<T extends EmptyObject = EmptyObject>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n): Promise<IndexDescription[]> {\n return await withReadOnlyStore(db, storeName, (store) => {\n return [...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 name: indexName as string,\n key,\n unique: index.unique,\n multiEntry: index.multiEntry,\n }\n return desc\n })\n })\n}\n","import type { EmptyObject } from '@xylabs/object'\nimport type { IDBPDatabase } from 'idb'\nimport { openDB } from 'idb'\n\nimport type { ObjectStore } from './ObjectStore.ts'\n\nexport async function withDb<T extends EmptyObject = EmptyObject>(dbName: string
|
|
1
|
+
{"version":3,"sources":["../../src/IndexDescription.ts","../../src/createStore.ts","../../src/withReadOnlyStore.ts","../../src/getExistingIndexes.ts","../../src/withDb.ts","../../src/withReadWriteStore.ts"],"sourcesContent":["/**\n * The index direction (1 for ascending, -1 for descending)\n */\nexport type IndexDirection = -1 | 1\n\n/**\n * Description of index(es) to be created on a store\n */\nexport type IndexDescription = {\n /**\n * The key(s) to index\n */\n key: Record<string, IndexDirection>\n /**\n * Is the indexed value an array\n */\n multiEntry?: boolean\n /**\n * The name of the index\n */\n name?: string\n /**\n * If true, the index must enforce uniqueness on the key\n */\n unique?: boolean\n}\n\nexport const IndexSeparator = '-'\n\n/**\n * Given an index description, this will build the index\n * name in standard form\n * @param index The index description\n * @returns The index name in standard form\n */\nexport const buildStandardIndexName = (index: IndexDescription) => {\n const { key, unique } = index\n const prefix = unique ? 'UX' : 'IX'\n const indexKeys = Object.keys(key)\n return `${prefix}_${indexKeys.join(IndexSeparator)}`\n}\n","import type { Logger } from '@xylabs/logger'\nimport type { EmptyObject } from '@xylabs/object'\nimport type {\n IDBPDatabase, IndexNames, StoreNames,\n} from 'idb'\n\nimport {\n buildStandardIndexName,\n type IndexDescription,\n} from './IndexDescription.ts'\nimport type { ObjectStore } from './ObjectStore.ts'\n\nexport function createStore<T extends EmptyObject = EmptyObject>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n indexes: IndexDescription[],\n logger?: Logger,\n) {\n logger?.log(`Creating store ${storeName}`)\n // Create the store\n const store = db.createObjectStore(storeName, {\n // If it isn't explicitly set, create a value by auto incrementing.\n autoIncrement: true,\n })\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 const indexKeys = Object.keys(key)\n const keys = indexKeys.length === 1 ? indexKeys[0] : indexKeys\n const indexName = buildStandardIndexName({ key, unique }) as IndexNames<ObjectStore<T>, StoreNames<ObjectStore<T>>>\n console.log('createIndex', indexName, keys, { multiEntry, unique })\n store.createIndex(indexName, keys, { multiEntry, unique })\n }\n}\n","import type { EmptyObject } from '@xylabs/object'\nimport type {\n IDBPDatabase, IDBPObjectStore, StoreNames,\n} from 'idb'\n\nimport type { ObjectStore } from './ObjectStore.ts'\n\nexport async function withReadOnlyStore<T extends EmptyObject = EmptyObject, R = T>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n callback: (store: IDBPObjectStore<ObjectStore<T>, [StoreNames<ObjectStore<T>>], StoreNames<ObjectStore<T>>, string>) => Promise<R> | R,\n): Promise<R> {\n const transaction = db.transaction(storeName, 'readonly')\n const store = transaction.objectStore(storeName)\n try {\n return await callback(store)\n } finally {\n await transaction.done\n }\n}\n","import 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 { withReadOnlyStore } from './withReadOnlyStore.ts'\n\nexport async function getExistingIndexes<T extends EmptyObject = EmptyObject>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n): Promise<IndexDescription[]> {\n return await withReadOnlyStore(db, storeName, (store) => {\n return [...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 name: indexName as string,\n key,\n unique: index.unique,\n multiEntry: index.multiEntry,\n }\n return desc\n })\n })\n}\n","import type { EmptyObject } from '@xylabs/object'\nimport type { IDBPDatabase } from 'idb'\nimport { openDB } from 'idb'\n\nimport type { ObjectStore } from './ObjectStore.ts'\n\nexport async function withDb<T extends EmptyObject = EmptyObject, R = T>(\n dbName: string,\n callback: (db: IDBPDatabase<ObjectStore<T>>) => Promise<R> | R,\n): Promise<R> {\n const db = await openDB<ObjectStore<T>>(dbName)\n try {\n return await callback(db)\n } finally {\n db.close()\n }\n}\n","import type { EmptyObject } from '@xylabs/object'\nimport type {\n IDBPDatabase, IDBPObjectStore, StoreNames,\n} from 'idb'\n\nimport type { ObjectStore } from './ObjectStore.ts'\n\nexport async function withReadWriteStore<T extends EmptyObject = EmptyObject, R = T>(\n db: IDBPDatabase<ObjectStore<T>>,\n storeName: StoreNames<ObjectStore<T>>,\n callback: (store: IDBPObjectStore<ObjectStore<T>, [StoreNames<ObjectStore<T>>], StoreNames<ObjectStore<T>>, string>) => Promise<R> | R,\n): Promise<R> {\n const transaction = db.transaction(storeName, 'readwrite')\n const store = transaction.objectStore(storeName)\n try {\n return await callback(store)\n } finally {\n await transaction.done\n }\n}\n"],"mappings":";AA2BO,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;;;AC5BO,SAAS,YACd,IACA,WACA,SACA,QACA;AACA,UAAQ,IAAI,kBAAkB,SAAS,EAAE;AAEzC,QAAM,QAAQ,GAAG,kBAAkB,WAAW;AAAA;AAAA,IAE5C,eAAe;AAAA,EACjB,CAAC;AAED,QAAM,OAAO;AAEb,aAAW;AAAA,IACT;AAAA,IAAK;AAAA,IAAY;AAAA,EACnB,KAAK,SAAS;AACZ,UAAM,YAAY,OAAO,KAAK,GAAG;AACjC,UAAM,OAAO,UAAU,WAAW,IAAI,UAAU,CAAC,IAAI;AACrD,UAAM,YAAY,uBAAuB,EAAE,KAAK,OAAO,CAAC;AACxD,YAAQ,IAAI,eAAe,WAAW,MAAM,EAAE,YAAY,OAAO,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package/dist/neutral/withDb.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { EmptyObject } from '@xylabs/object';
|
|
2
2
|
import type { IDBPDatabase } from 'idb';
|
|
3
3
|
import type { ObjectStore } from './ObjectStore.ts';
|
|
4
|
-
export declare function withDb<T extends EmptyObject = EmptyObject>(dbName: string, callback: (db: IDBPDatabase<ObjectStore<T>>) => Promise<
|
|
4
|
+
export declare function withDb<T extends EmptyObject = EmptyObject, R = T>(dbName: string, callback: (db: IDBPDatabase<ObjectStore<T>>) => Promise<R> | R): Promise<R>;
|
|
5
5
|
//# 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,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACjD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,KAAK,CAAA;AAGvC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEnD,wBAAsB,MAAM,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,EAAE,MAAM,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"withDb.d.ts","sourceRoot":"","sources":["../../src/withDb.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACjD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,KAAK,CAAA;AAGvC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAEnD,wBAAsB,MAAM,CAAC,CAAC,SAAS,WAAW,GAAG,WAAW,EAAE,CAAC,GAAG,CAAC,EACrE,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,CAAC,EAAE,EAAE,YAAY,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,GAC7D,OAAO,CAAC,CAAC,CAAC,CAOZ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xylabs/indexed-db",
|
|
3
|
-
"version": "4.4.
|
|
3
|
+
"version": "4.4.29",
|
|
4
4
|
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"hex",
|
|
@@ -39,14 +39,14 @@
|
|
|
39
39
|
"packages/**/*"
|
|
40
40
|
],
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@xylabs/logger": "^4.4.
|
|
43
|
-
"@xylabs/object": "^4.4.
|
|
42
|
+
"@xylabs/logger": "^4.4.29",
|
|
43
|
+
"@xylabs/object": "^4.4.29",
|
|
44
44
|
"idb": "^8.0.1"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@xylabs/ts-scripts-yarn3": "^4.2.6",
|
|
48
48
|
"@xylabs/tsconfig": "^4.2.6",
|
|
49
|
-
"@xylabs/vitest-extended": "^4.4.
|
|
49
|
+
"@xylabs/vitest-extended": "^4.4.29",
|
|
50
50
|
"typescript": "^5.7.2",
|
|
51
51
|
"vitest": "^2.1.8"
|
|
52
52
|
},
|
package/src/withDb.ts
CHANGED
|
@@ -4,7 +4,10 @@ import { openDB } from 'idb'
|
|
|
4
4
|
|
|
5
5
|
import type { ObjectStore } from './ObjectStore.ts'
|
|
6
6
|
|
|
7
|
-
export async function withDb<T extends EmptyObject = EmptyObject
|
|
7
|
+
export async function withDb<T extends EmptyObject = EmptyObject, R = T>(
|
|
8
|
+
dbName: string,
|
|
9
|
+
callback: (db: IDBPDatabase<ObjectStore<T>>) => Promise<R> | R,
|
|
10
|
+
): Promise<R> {
|
|
8
11
|
const db = await openDB<ObjectStore<T>>(dbName)
|
|
9
12
|
try {
|
|
10
13
|
return await callback(db)
|