@firtoz/drizzle-indexeddb 5.0.0 → 5.0.2
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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# @firtoz/drizzle-indexeddb
|
|
2
2
|
|
|
3
|
+
## 5.0.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [`65c1758`](https://github.com/firtoz/fullstack-toolkit/commit/65c1758a2d598865e341c0d72a536be3e8536ce9) Thanks [@firtoz](https://github.com/firtoz)! - Remove `any` / double cast in standalone collection creation; satisfy Biome in concurrent submitter tests (no non-null assertions).
|
|
8
|
+
|
|
9
|
+
## 5.0.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Updated dependencies [[`7c4983f`](https://github.com/firtoz/fullstack-toolkit/commit/7c4983fd27adb9709ee844547259e0f22040fded)]:
|
|
14
|
+
- @firtoz/drizzle-utils@1.3.1
|
|
15
|
+
- @firtoz/db-helpers@2.2.1
|
|
16
|
+
- @firtoz/maybe-error@1.6.1
|
|
17
|
+
- @firtoz/idb-collections@0.3.1
|
|
18
|
+
|
|
3
19
|
## 5.0.0
|
|
4
20
|
|
|
5
21
|
### Minor Changes
|
|
@@ -125,5 +125,5 @@ function createStandaloneCollection(config) {
|
|
|
125
125
|
}
|
|
126
126
|
|
|
127
127
|
export { createStandaloneCollection };
|
|
128
|
-
//# sourceMappingURL=chunk-
|
|
129
|
-
//# sourceMappingURL=chunk-
|
|
128
|
+
//# sourceMappingURL=chunk-OMTW2TX5.js.map
|
|
129
|
+
//# sourceMappingURL=chunk-OMTW2TX5.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/standalone-collection.ts"],"names":[],"mappings":";;;;;AA+MO,SAAS,2BACf,MAAA,EAC+B;AAC/B,EAAA,MAAM;AAAA,IACL,MAAA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Output,\n\ttype SyncMode,\n\ttype Transaction,\n\ttype WritableDeep,\n} from \"@tanstack/db\";\nimport type { Table } from \"drizzle-orm\";\nimport type { CollectionUtils } from \"@firtoz/db-helpers\";\nimport type {\n\tIdOf,\n\tInsertSchema,\n\tInsertToSelectSchema,\n\tSelectSchema,\n} from \"@firtoz/drizzle-utils\";\nimport {\n\tdrizzleIndexedDBCollectionOptions,\n\ttype DrizzleIndexedDBCollectionConfig,\n} from \"./collections/drizzle-indexeddb-collection\";\nimport {\n\tmigrateIndexedDBWithFunctions,\n\ttype Migration,\n} from \"./function-migrator\";\nimport { openIndexedDb } from \"./idb-operations\";\nimport type { IDBCreator, IDBDatabaseLike } from \"./idb-types\";\n\n/**\n * Configuration for creating a standalone IndexedDB collection\n */\nexport interface StandaloneCollectionConfig<TTable extends Table> {\n\t/**\n\t * Name of the IndexedDB database\n\t */\n\tdbName: string;\n\t/**\n\t * The Drizzle table definition\n\t */\n\ttable: TTable;\n\t/**\n\t * The name of the IndexedDB object store (defaults to table name)\n\t */\n\tstoreName?: string;\n\t/**\n\t * Migrations to apply (optional)\n\t */\n\tmigrations?: Migration[];\n\t/**\n\t * Custom database creator (for testing/mocking)\n\t */\n\tdbCreator?: IDBCreator;\n\t/**\n\t * Enable debug logging\n\t */\n\tdebug?: boolean;\n\t/**\n\t * Sync mode: 'eager' (immediate) or 'lazy' (on-demand)\n\t */\n\tsyncMode?: SyncMode;\n}\n\n/**\n * Type for the underlying collection\n */\ntype InternalCollection<TTable extends Table> = Collection<\n\tInferSchemaOutput<SelectSchema<TTable>>,\n\tIdOf<TTable>,\n\tCollectionUtils<InferSchemaOutput<SelectSchema<TTable>>>,\n\tInsertToSelectSchema<TTable>,\n\tInferSchemaInput<InsertSchema<TTable>>\n>;\n\n/**\n * Transaction type for mutations\n */\ntype MutationTransaction<TTable extends Table> = Transaction<\n\tInferSchemaOutput<SelectSchema<TTable>>\n>;\n\n/**\n * Insert input type (what you pass to insert)\n */\ntype InsertInput<TTable extends Table> = InferSchemaInput<InsertSchema<TTable>>;\n\n/**\n * Item type (what you get back from getAll, etc.)\n */\ntype ItemType<TTable extends Table> = InferSchemaOutput<SelectSchema<TTable>>;\n\n/**\n * Writable draft type for update callbacks\n */\ntype DraftType<TTable extends Table> = WritableDeep<InsertInput<TTable>>;\n\n/**\n * Standalone IndexedDB collection API\n */\nexport interface StandaloneCollection<TTable extends Table> {\n\t/**\n\t * Promise that resolves when the collection is ready\n\t */\n\tready: Promise<void>;\n\n\t/**\n\t * Check if the collection is ready (sync)\n\t */\n\tisReady(): boolean;\n\n\t/**\n\t * Get all items (sync - returns current state)\n\t */\n\tgetAll(): ItemType<TTable>[];\n\n\t/**\n\t * Get an item by key (sync)\n\t */\n\tget(key: IdOf<TTable>): ItemType<TTable> | undefined;\n\n\t/**\n\t * Insert item(s)\n\t * @returns Promise that resolves when persisted\n\t */\n\tinsert(\n\t\tdata: InsertInput<TTable> | InsertInput<TTable>[],\n\t\tcallback?: (transaction: MutationTransaction<TTable>) => void,\n\t): Promise<MutationTransaction<TTable>>;\n\n\t/**\n\t * Update an item by key using a callback that receives a draft\n\t * @returns Promise that resolves when persisted\n\t */\n\tupdate(\n\t\tkey: IdOf<TTable>,\n\t\tupdater: (draft: DraftType<TTable>) => void,\n\t\tcallback?: (transaction: MutationTransaction<TTable>) => void,\n\t): Promise<MutationTransaction<TTable>>;\n\n\t/**\n\t * Delete item(s) by key\n\t * @returns Promise that resolves when persisted\n\t */\n\tdelete(\n\t\tkey: IdOf<TTable> | IdOf<TTable>[],\n\t\tcallback?: (transaction: MutationTransaction<TTable>) => void,\n\t): Promise<MutationTransaction<TTable>>;\n\n\t/**\n\t * Clear all items from the store\n\t * @returns Promise that resolves when truncate is complete\n\t */\n\ttruncate(): Promise<void>;\n\n\t/**\n\t * Access to collection utils (truncate, receiveSync)\n\t */\n\tutils: CollectionUtils<ItemType<TTable>>;\n\n\t/**\n\t * The underlying TanStack DB collection (for advanced usage)\n\t */\n\tcollection: InternalCollection<TTable>;\n\n\t/**\n\t * The IndexedDB database instance (available after ready)\n\t */\n\tdb: IDBDatabaseLike | null;\n\n\t/**\n\t * Close the database connection\n\t */\n\tclose(): void;\n}\n\n/**\n * Create a standalone IndexedDB collection for use outside of React.\n *\n * @example\n * ```ts\n * const db = await createStandaloneCollection({\n * dbName: \"myapp.db\",\n * table: schema.todos,\n * migrations,\n * });\n *\n * // Wait for ready\n * await db.ready;\n *\n * // Get all items\n * const items = db.getAll();\n *\n * // Insert\n * await db.insert({ title: \"New todo\" });\n *\n * // Update\n * await db.update(itemId, { title: \"Updated\" });\n *\n * // Delete\n * await db.delete(itemId);\n *\n * // Truncate\n * await db.truncate();\n *\n * // Clean up\n * db.close();\n * ```\n */\nexport function createStandaloneCollection<TTable extends Table>(\n\tconfig: StandaloneCollectionConfig<TTable>,\n): StandaloneCollection<TTable> {\n\tconst {\n\t\tdbName,\n\t\ttable,\n\t\tstoreName = (table as unknown as { _: { name: string } })._.name,\n\t\tmigrations = [],\n\t\tdbCreator,\n\t\tdebug = false,\n\t\tsyncMode = \"eager\",\n\t} = config;\n\n\t// Create ready promise\n\tlet resolveReady: () => void;\n\tconst readyPromise = new Promise<void>((resolve) => {\n\t\tresolveReady = resolve;\n\t});\n\n\t// Database ref\n\tconst indexedDBRef: { current: IDBDatabaseLike | null } = { current: null };\n\n\t// Initialize database\n\tconst initDB = async () => {\n\t\ttry {\n\t\t\tif (migrations.length === 0) {\n\t\t\t\tif (debug) {\n\t\t\t\t\tconsole.log(\n\t\t\t\t\t\t`[StandaloneCollection] Opening database \"${dbName}\" directly`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tindexedDBRef.current = await openIndexedDb(dbName, dbCreator);\n\t\t\t} else {\n\t\t\t\tif (debug) {\n\t\t\t\t\tconsole.log(`[StandaloneCollection] Migrating database \"${dbName}\"`);\n\t\t\t\t}\n\t\t\t\tindexedDBRef.current = await migrateIndexedDBWithFunctions(\n\t\t\t\t\tdbName,\n\t\t\t\t\tmigrations,\n\t\t\t\t\tdebug,\n\t\t\t\t\tdbCreator,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tif (debug) {\n\t\t\t\tconsole.log(`[StandaloneCollection] Database \"${dbName}\" initialized`);\n\t\t\t}\n\n\t\t\tresolveReady();\n\t\t} catch (error) {\n\t\t\tconsole.error(\n\t\t\t\t`[StandaloneCollection] Failed to initialize database \"${dbName}\":`,\n\t\t\t\terror,\n\t\t\t);\n\t\t\tthrow error;\n\t\t}\n\t};\n\n\t// Start initialization\n\tinitDB();\n\n\t// Create collection config\n\tconst collectionConfig = drizzleIndexedDBCollectionOptions({\n\t\tindexedDBRef,\n\t\ttable,\n\t\tstoreName,\n\t\treadyPromise,\n\t\tdebug,\n\t\tsyncMode,\n\t} as DrizzleIndexedDBCollectionConfig<TTable>);\n\n\t// biome-ignore lint/suspicious/noExplicitAny: createCollection overloads can't resolve InsertToSelectSchema for generic TTable; collection is re-typed below\n\tconst collection = createCollection(\n\t\tcollectionConfig as any,\n\t) as unknown as InternalCollection<TTable>;\n\n\t// Wait for collection to be ready\n\tconst collectionReady = new Promise<void>((resolve) => {\n\t\tif (collection.isReady()) {\n\t\t\tresolve();\n\t\t\treturn;\n\t\t}\n\t\tcollection.preload();\n\t\tcollection.onFirstReady(() => resolve());\n\t});\n\n\t// Combined ready promise\n\tconst ready = Promise.all([readyPromise, collectionReady]).then(() => {});\n\n\t// Helper to wait for transaction to persist\n\tconst waitForPersist = async (\n\t\ttransaction: MutationTransaction<TTable>,\n\t\tcallback?: (transaction: MutationTransaction<TTable>) => void,\n\t): Promise<MutationTransaction<TTable>> => {\n\t\tif (callback) {\n\t\t\tcallback(transaction);\n\t\t}\n\t\tawait transaction.isPersisted.promise;\n\t\treturn transaction;\n\t};\n\n\treturn {\n\t\tready,\n\n\t\tisReady(): boolean {\n\t\t\treturn collection.isReady();\n\t\t},\n\n\t\tgetAll(): ItemType<TTable>[] {\n\t\t\treturn collection.toArray;\n\t\t},\n\n\t\tget(key: IdOf<TTable>): ItemType<TTable> | undefined {\n\t\t\treturn collection.state.get(key);\n\t\t},\n\n\t\tinsert(\n\t\t\tdata: InsertInput<TTable> | InsertInput<TTable>[],\n\t\t\tcallback?: (transaction: MutationTransaction<TTable>) => void,\n\t\t): Promise<MutationTransaction<TTable>> {\n\t\t\tconst items = (Array.isArray(data) ? data : [data]) as InferSchemaOutput<\n\t\t\t\tSelectSchema<TTable>\n\t\t\t>;\n\t\t\tconst transaction = collection.insert(\n\t\t\t\titems,\n\t\t\t) as MutationTransaction<TTable>;\n\t\t\treturn waitForPersist(transaction, callback);\n\t\t},\n\n\t\tupdate(\n\t\t\tkey: IdOf<TTable>,\n\t\t\tupdater: (draft: DraftType<TTable>) => void,\n\t\t\tcallback?: (transaction: MutationTransaction<TTable>) => void,\n\t\t): Promise<MutationTransaction<TTable>> {\n\t\t\tconst transaction = collection.update(\n\t\t\t\tkey,\n\t\t\t\tupdater,\n\t\t\t) as MutationTransaction<TTable>;\n\t\t\treturn waitForPersist(transaction, callback);\n\t\t},\n\n\t\tdelete(\n\t\t\tkey: IdOf<TTable> | IdOf<TTable>[],\n\t\t\tcallback?: (transaction: MutationTransaction<TTable>) => void,\n\t\t): Promise<MutationTransaction<TTable>> {\n\t\t\tconst keys = Array.isArray(key) ? key : [key];\n\t\t\tconst transaction = collection.delete(keys);\n\t\t\treturn waitForPersist(transaction, callback);\n\t\t},\n\n\t\ttruncate(): Promise<void> {\n\t\t\treturn collection.utils.truncate();\n\t\t},\n\n\t\tutils: collection.utils,\n\n\t\tcollection,\n\n\t\tget db(): IDBDatabaseLike | null {\n\t\t\treturn indexedDBRef.current;\n\t\t},\n\n\t\tclose(): void {\n\t\t\tindexedDBRef.current?.close();\n\t\t\tindexedDBRef.current = null;\n\t\t},\n\t};\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/standalone-collection.ts"],"names":[],"mappings":";;;;;AA+MO,SAAS,2BACf,MAAA,EAC+B;AAC/B,EAAA,MAAM;AAA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n\ttype InferSchemaOutput,\n\ttype SyncMode,\n\ttype Transaction,\n\ttype WritableDeep,\n} from \"@tanstack/db\";\nimport type { Table } from \"drizzle-orm\";\nimport type { CollectionUtils } from \"@firtoz/db-helpers\";\nimport type {\n\tIdOf,\n\tInsertSchema,\n\tInsertToSelectSchema,\n\tSelectSchema,\n} from \"@firtoz/drizzle-utils\";\nimport {\n\tdrizzleIndexedDBCollectionOptions,\n\ttype DrizzleIndexedDBCollectionConfig,\n} from \"./collections/drizzle-indexeddb-collection\";\nimport {\n\tmigrateIndexedDBWithFunctions,\n\ttype Migration,\n} from \"./function-migrator\";\nimport { openIndexedDb } from \"./idb-operations\";\nimport type { IDBCreator, IDBDatabaseLike } from \"./idb-types\";\n\n/**\n * Configuration for creating a standalone IndexedDB collection\n */\nexport interface StandaloneCollectionConfig<TTable extends Table> {\n\t/**\n\t * Name of the IndexedDB database\n\t */\n\tdbName: string;\n\t/**\n\t * The Drizzle table definition\n\t */\n\ttable: TTable;\n\t/**\n\t * The name of the IndexedDB object store (defaults to table name)\n\t */\n\tstoreName?: string;\n\t/**\n\t * Migrations to apply (optional)\n\t */\n\tmigrations?: Migration[];\n\t/**\n\t * Custom database creator (for testing/mocking)\n\t */\n\tdbCreator?: IDBCreator;\n\t/**\n\t * Enable debug logging\n\t */\n\tdebug?: boolean;\n\t/**\n\t * Sync mode: 'eager' (immediate) or 'lazy' (on-demand)\n\t */\n\tsyncMode?: SyncMode;\n}\n\n/**\n * Type for the underlying collection\n */\ntype InternalCollection<TTable extends Table> = Collection<\n\tInferSchemaOutput<SelectSchema<TTable>>,\n\tIdOf<TTable>,\n\tCollectionUtils<InferSchemaOutput<SelectSchema<TTable>>>,\n\tInsertToSelectSchema<TTable>,\n\tInferSchemaInput<InsertSchema<TTable>>\n>;\n\n/**\n * Transaction type for mutations\n */\ntype MutationTransaction<TTable extends Table> = Transaction<\n\tInferSchemaOutput<SelectSchema<TTable>>\n>;\n\n/**\n * Insert input type (what you pass to insert)\n */\ntype InsertInput<TTable extends Table> = InferSchemaInput<InsertSchema<TTable>>;\n\n/**\n * Item type (what you get back from getAll, etc.)\n */\ntype ItemType<TTable extends Table> = InferSchemaOutput<SelectSchema<TTable>>;\n\n/**\n * Writable draft type for update callbacks\n */\ntype DraftType<TTable extends Table> = WritableDeep<InsertInput<TTable>>;\n\n/**\n * Standalone IndexedDB collection API\n */\nexport interface StandaloneCollection<TTable extends Table> {\n\t/**\n\t * Promise that resolves when the collection is ready\n\t */\n\tready: Promise<void>;\n\n\t/**\n\t * Check if the collection is ready (sync)\n\t */\n\tisReady(): boolean;\n\n\t/**\n\t * Get all items (sync - returns current state)\n\t */\n\tgetAll(): ItemType<TTable>[];\n\n\t/**\n\t * Get an item by key (sync)\n\t */\n\tget(key: IdOf<TTable>): ItemType<TTable> | undefined;\n\n\t/**\n\t * Insert item(s)\n\t * @returns Promise that resolves when persisted\n\t */\n\tinsert(\n\t\tdata: InsertInput<TTable> | InsertInput<TTable>[],\n\t\tcallback?: (transaction: MutationTransaction<TTable>) => void,\n\t): Promise<MutationTransaction<TTable>>;\n\n\t/**\n\t * Update an item by key using a callback that receives a draft\n\t * @returns Promise that resolves when persisted\n\t */\n\tupdate(\n\t\tkey: IdOf<TTable>,\n\t\tupdater: (draft: DraftType<TTable>) => void,\n\t\tcallback?: (transaction: MutationTransaction<TTable>) => void,\n\t): Promise<MutationTransaction<TTable>>;\n\n\t/**\n\t * Delete item(s) by key\n\t * @returns Promise that resolves when persisted\n\t */\n\tdelete(\n\t\tkey: IdOf<TTable> | IdOf<TTable>[],\n\t\tcallback?: (transaction: MutationTransaction<TTable>) => void,\n\t): Promise<MutationTransaction<TTable>>;\n\n\t/**\n\t * Clear all items from the store\n\t * @returns Promise that resolves when truncate is complete\n\t */\n\ttruncate(): Promise<void>;\n\n\t/**\n\t * Access to collection utils (truncate, receiveSync)\n\t */\n\tutils: CollectionUtils<ItemType<TTable>>;\n\n\t/**\n\t * The underlying TanStack DB collection (for advanced usage)\n\t */\n\tcollection: InternalCollection<TTable>;\n\n\t/**\n\t * The IndexedDB database instance (available after ready)\n\t */\n\tdb: IDBDatabaseLike | null;\n\n\t/**\n\t * Close the database connection\n\t */\n\tclose(): void;\n}\n\n/**\n * Create a standalone IndexedDB collection for use outside of React.\n *\n * @example\n * ```ts\n * const db = await createStandaloneCollection({\n * dbName: \"myapp.db\",\n * table: schema.todos,\n * migrations,\n * });\n *\n * // Wait for ready\n * await db.ready;\n *\n * // Get all items\n * const items = db.getAll();\n *\n * // Insert\n * await db.insert({ title: \"New todo\" });\n *\n * // Update\n * await db.update(itemId, { title: \"Updated\" });\n *\n * // Delete\n * await db.delete(itemId);\n *\n * // Truncate\n * await db.truncate();\n *\n * // Clean up\n * db.close();\n * ```\n */\nexport function createStandaloneCollection<TTable extends Table>(\n\tconfig: StandaloneCollectionConfig<TTable>,\n): StandaloneCollection<TTable> {\n\tconst {\n\t\tdbName,\n\t\ttable,\n\t\tstoreName = (table as unknown as { _: { name: string } })._.name,\n\t\tmigrations = [],\n\t\tdbCreator,\n\t\tdebug = false,\n\t\tsyncMode = \"eager\",\n\t} = config;\n\n\t// Create ready promise\n\tlet resolveReady: () => void;\n\tconst readyPromise = new Promise<void>((resolve) => {\n\t\tresolveReady = resolve;\n\t});\n\n\t// Database ref\n\tconst indexedDBRef: { current: IDBDatabaseLike | null } = { current: null };\n\n\t// Initialize database\n\tconst initDB = async () => {\n\t\ttry {\n\t\t\tif (migrations.length === 0) {\n\t\t\t\tif (debug) {\n\t\t\t\t\tconsole.log(\n\t\t\t\t\t\t`[StandaloneCollection] Opening database \"${dbName}\" directly`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tindexedDBRef.current = await openIndexedDb(dbName, dbCreator);\n\t\t\t} else {\n\t\t\t\tif (debug) {\n\t\t\t\t\tconsole.log(`[StandaloneCollection] Migrating database \"${dbName}\"`);\n\t\t\t\t}\n\t\t\t\tindexedDBRef.current = await migrateIndexedDBWithFunctions(\n\t\t\t\t\tdbName,\n\t\t\t\t\tmigrations,\n\t\t\t\t\tdebug,\n\t\t\t\t\tdbCreator,\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tif (debug) {\n\t\t\t\tconsole.log(`[StandaloneCollection] Database \"${dbName}\" initialized`);\n\t\t\t}\n\n\t\t\tresolveReady();\n\t\t} catch (error) {\n\t\t\tconsole.error(\n\t\t\t\t`[StandaloneCollection] Failed to initialize database \"${dbName}\":`,\n\t\t\t\terror,\n\t\t\t);\n\t\t\tthrow error;\n\t\t}\n\t};\n\n\t// Start initialization\n\tinitDB();\n\n\t// Create collection config\n\tconst collectionConfig = drizzleIndexedDBCollectionOptions({\n\t\tindexedDBRef,\n\t\ttable,\n\t\tstoreName,\n\t\treadyPromise,\n\t\tdebug,\n\t\tsyncMode,\n\t} as DrizzleIndexedDBCollectionConfig<TTable>);\n\n\tconst collection = createCollection(\n\t\tcollectionConfig,\n\t) as InternalCollection<TTable>;\n\n\t// Wait for collection to be ready\n\tconst collectionReady = new Promise<void>((resolve) => {\n\t\tif (collection.isReady()) {\n\t\t\tresolve();\n\t\t\treturn;\n\t\t}\n\t\tcollection.preload();\n\t\tcollection.onFirstReady(() => resolve());\n\t});\n\n\t// Combined ready promise\n\tconst ready = Promise.all([readyPromise, collectionReady]).then(() => {});\n\n\t// Helper to wait for transaction to persist\n\tconst waitForPersist = async (\n\t\ttransaction: MutationTransaction<TTable>,\n\t\tcallback?: (transaction: MutationTransaction<TTable>) => void,\n\t): Promise<MutationTransaction<TTable>> => {\n\t\tif (callback) {\n\t\t\tcallback(transaction);\n\t\t}\n\t\tawait transaction.isPersisted.promise;\n\t\treturn transaction;\n\t};\n\n\treturn {\n\t\tready,\n\n\t\tisReady(): boolean {\n\t\t\treturn collection.isReady();\n\t\t},\n\n\t\tgetAll(): ItemType<TTable>[] {\n\t\t\treturn collection.toArray;\n\t\t},\n\n\t\tget(key: IdOf<TTable>): ItemType<TTable> | undefined {\n\t\t\treturn collection.state.get(key);\n\t\t},\n\n\t\tinsert(\n\t\t\tdata: InsertInput<TTable> | InsertInput<TTable>[],\n\t\t\tcallback?: (transaction: MutationTransaction<TTable>) => void,\n\t\t): Promise<MutationTransaction<TTable>> {\n\t\t\tconst items = (Array.isArray(data) ? data : [data]) as InferSchemaOutput<\n\t\t\t\tSelectSchema<TTable>\n\t\t\t>;\n\t\t\tconst transaction = collection.insert(\n\t\t\t\titems,\n\t\t\t) as MutationTransaction<TTable>;\n\t\t\treturn waitForPersist(transaction, callback);\n\t\t},\n\n\t\tupdate(\n\t\t\tkey: IdOf<TTable>,\n\t\t\tupdater: (draft: DraftType<TTable>) => void,\n\t\t\tcallback?: (transaction: MutationTransaction<TTable>) => void,\n\t\t): Promise<MutationTransaction<TTable>> {\n\t\t\tconst transaction = collection.update(\n\t\t\t\tkey,\n\t\t\t\tupdater,\n\t\t\t) as MutationTransaction<TTable>;\n\t\t\treturn waitForPersist(transaction, callback);\n\t\t},\n\n\t\tdelete(\n\t\t\tkey: IdOf<TTable> | IdOf<TTable>[],\n\t\t\tcallback?: (transaction: MutationTransaction<TTable>) => void,\n\t\t): Promise<MutationTransaction<TTable>> {\n\t\t\tconst keys = Array.isArray(key) ? key : [key];\n\t\t\tconst transaction = collection.delete(keys);\n\t\t\treturn waitForPersist(transaction, callback);\n\t\t},\n\n\t\ttruncate(): Promise<void> {\n\t\t\treturn collection.utils.truncate();\n\t\t},\n\n\t\tutils: collection.utils,\n\n\t\tcollection,\n\n\t\tget db(): IDBDatabaseLike | null {\n\t\t\treturn indexedDBRef.current;\n\t\t},\n\n\t\tclose(): void {\n\t\t\tindexedDBRef.current?.close();\n\t\t\tindexedDBRef.current = null;\n\t\t},\n\t};\n}\n"]}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { createInstrumentedDbCreator } from './chunk-OSOLYU2M.js';
|
|
2
|
-
export { createStandaloneCollection } from './chunk-
|
|
2
|
+
export { createStandaloneCollection } from './chunk-OMTW2TX5.js';
|
|
3
3
|
export { useDrizzleIndexedDB } from './chunk-HJFI7QKW.js';
|
|
4
4
|
export { DrizzleIndexedDBContext, DrizzleIndexedDBProvider, useIndexedDBCollection } from './chunk-Y6XE3FVT.js';
|
|
5
5
|
export { drizzleIndexedDBCollectionOptions } from './chunk-5KCMKETG.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@firtoz/drizzle-indexeddb",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.2",
|
|
4
4
|
"description": "IndexedDB migrations powered by Drizzle ORM",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -73,26 +73,26 @@
|
|
|
73
73
|
"access": "public"
|
|
74
74
|
},
|
|
75
75
|
"peerDependencies": {
|
|
76
|
-
"@firtoz/drizzle-utils": ">=1.3.
|
|
77
|
-
"@tanstack/db": ">=0.6.
|
|
76
|
+
"@firtoz/drizzle-utils": ">=1.3.1",
|
|
77
|
+
"@tanstack/db": ">=0.6.5",
|
|
78
78
|
"drizzle-orm": ">=0.45.2",
|
|
79
79
|
"drizzle-valibot": ">=0.4.0",
|
|
80
|
-
"react": ">=19.2.
|
|
80
|
+
"react": ">=19.2.5",
|
|
81
81
|
"valibot": ">=1.3.1"
|
|
82
82
|
},
|
|
83
83
|
"devDependencies": {
|
|
84
|
-
"@firtoz/drizzle-utils": "^1.3.
|
|
85
|
-
"@tanstack/db": "^0.6.
|
|
84
|
+
"@firtoz/drizzle-utils": "^1.3.1",
|
|
85
|
+
"@tanstack/db": "^0.6.5",
|
|
86
86
|
"@types/react": "^19.2.14",
|
|
87
87
|
"drizzle-orm": "^0.45.2",
|
|
88
88
|
"drizzle-valibot": "^0.4.2",
|
|
89
|
-
"react": "^19.2.
|
|
89
|
+
"react": "^19.2.5",
|
|
90
90
|
"valibot": "^1.3.1"
|
|
91
91
|
},
|
|
92
92
|
"dependencies": {
|
|
93
|
-
"@firtoz/db-helpers": "^2.2.
|
|
94
|
-
"@firtoz/idb-collections": "^0.3.
|
|
95
|
-
"@firtoz/maybe-error": "^1.6.
|
|
96
|
-
"citty": "^0.2.
|
|
93
|
+
"@firtoz/db-helpers": "^2.2.1",
|
|
94
|
+
"@firtoz/idb-collections": "^0.3.1",
|
|
95
|
+
"@firtoz/maybe-error": "^1.6.1",
|
|
96
|
+
"citty": "^0.2.2"
|
|
97
97
|
}
|
|
98
98
|
}
|
|
@@ -276,10 +276,9 @@ export function createStandaloneCollection<TTable extends Table>(
|
|
|
276
276
|
syncMode,
|
|
277
277
|
} as DrizzleIndexedDBCollectionConfig<TTable>);
|
|
278
278
|
|
|
279
|
-
// biome-ignore lint/suspicious/noExplicitAny: createCollection overloads can't resolve InsertToSelectSchema for generic TTable; collection is re-typed below
|
|
280
279
|
const collection = createCollection(
|
|
281
|
-
collectionConfig
|
|
282
|
-
) as
|
|
280
|
+
collectionConfig,
|
|
281
|
+
) as InternalCollection<TTable>;
|
|
283
282
|
|
|
284
283
|
// Wait for collection to be ready
|
|
285
284
|
const collectionReady = new Promise<void>((resolve) => {
|