import { SyncMode, InferSchemaOutput, InferSchemaInput, Transaction, WritableDeep, Collection } from '@tanstack/db'; import { Table } from 'drizzle-orm'; import { CollectionUtils } from '@firtoz/db-helpers'; import { SelectSchema, IdOf, InsertSchema, InsertToSelectSchema } from '@firtoz/drizzle-utils'; import { Migration } from './function-migrator.js'; import { IDBCreator, IDBDatabaseLike } from './idb-types.js'; import '@firtoz/idb-collections'; /** * Configuration for creating a standalone IndexedDB collection */ interface StandaloneCollectionConfig { /** * Name of the IndexedDB database */ dbName: string; /** * The Drizzle table definition */ table: TTable; /** * The name of the IndexedDB object store (defaults to table name) */ storeName?: string; /** * Migrations to apply (optional) */ migrations?: Migration[]; /** * Custom database creator (for testing/mocking) */ dbCreator?: IDBCreator; /** * Enable debug logging */ debug?: boolean; /** * Sync mode: 'eager' (immediate) or 'lazy' (on-demand) */ syncMode?: SyncMode; } /** * Type for the underlying collection */ type InternalCollection = Collection>, IdOf, CollectionUtils>>, InsertToSelectSchema, InferSchemaInput>>; /** * Transaction type for mutations */ type MutationTransaction = Transaction>>; /** * Insert input type (what you pass to insert) */ type InsertInput = InferSchemaInput>; /** * Item type (what you get back from getAll, etc.) */ type ItemType = InferSchemaOutput>; /** * Writable draft type for update callbacks */ type DraftType = WritableDeep>; /** * Standalone IndexedDB collection API */ interface StandaloneCollection { /** * Promise that resolves when the collection is ready */ ready: Promise; /** * Check if the collection is ready (sync) */ isReady(): boolean; /** * Get all items (sync - returns current state) */ getAll(): ItemType[]; /** * Get an item by key (sync) */ get(key: IdOf): ItemType | undefined; /** * Insert item(s) * @returns Promise that resolves when persisted */ insert(data: InsertInput | InsertInput[], callback?: (transaction: MutationTransaction) => void): Promise>; /** * Update an item by key using a callback that receives a draft * @returns Promise that resolves when persisted */ update(key: IdOf, updater: (draft: DraftType) => void, callback?: (transaction: MutationTransaction) => void): Promise>; /** * Delete item(s) by key * @returns Promise that resolves when persisted */ delete(key: IdOf | IdOf[], callback?: (transaction: MutationTransaction) => void): Promise>; /** * Clear all items from the store * @returns Promise that resolves when truncate is complete */ truncate(): Promise; /** * Access to collection utils (truncate, receiveSync) */ utils: CollectionUtils>; /** * The underlying TanStack DB collection (for advanced usage) */ collection: InternalCollection; /** * The IndexedDB database instance (available after ready) */ db: IDBDatabaseLike | null; /** * Close the database connection */ close(): void; } /** * Create a standalone IndexedDB collection for use outside of React. * * @example * ```ts * const db = await createStandaloneCollection({ * dbName: "myapp.db", * table: schema.todos, * migrations, * }); * * // Wait for ready * await db.ready; * * // Get all items * const items = db.getAll(); * * // Insert * await db.insert({ title: "New todo" }); * * // Update * await db.update(itemId, { title: "Updated" }); * * // Delete * await db.delete(itemId); * * // Truncate * await db.truncate(); * * // Clean up * db.close(); * ``` */ declare function createStandaloneCollection(config: StandaloneCollectionConfig): StandaloneCollection; export { type StandaloneCollection, type StandaloneCollectionConfig, createStandaloneCollection };