@anfenn/dync 1.0.30 → 1.0.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/README.md +31 -9
  2. package/dist/capacitor.cjs +2 -2
  3. package/dist/capacitor.cjs.map +1 -1
  4. package/dist/capacitor.d.cts +1 -1
  5. package/dist/capacitor.d.ts +1 -1
  6. package/dist/capacitor.js +1 -1
  7. package/dist/{chunk-SQB6E7V2.js → chunk-XAHQXK76.js} +3 -3
  8. package/dist/{chunk-SQB6E7V2.js.map → chunk-XAHQXK76.js.map} +1 -1
  9. package/dist/{dexie-T9m1mP1h.d.cts → dexie-3VOQSn1s.d.cts} +5 -5
  10. package/dist/{dexie-BFPA0JU2.d.ts → dexie-D85rTx4g.d.ts} +5 -5
  11. package/dist/dexie.d.cts +2 -2
  12. package/dist/dexie.d.ts +2 -2
  13. package/dist/expoSqlite.d.cts +1 -1
  14. package/dist/expoSqlite.d.ts +1 -1
  15. package/dist/index.cjs +11 -23
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.d.cts +9 -9
  18. package/dist/index.d.ts +9 -9
  19. package/dist/index.js +11 -23
  20. package/dist/index.js.map +1 -1
  21. package/dist/node.cjs +5 -5
  22. package/dist/node.cjs.map +1 -1
  23. package/dist/node.d.cts +9 -9
  24. package/dist/node.d.ts +9 -9
  25. package/dist/node.js +4 -4
  26. package/dist/node.js.map +1 -1
  27. package/dist/react/index.d.cts +3 -3
  28. package/dist/react/index.d.ts +3 -3
  29. package/dist/{types-CSbIAfu2.d.cts → types-6-NyRQ0D.d.cts} +3 -11
  30. package/dist/{types-CSbIAfu2.d.ts → types-6-NyRQ0D.d.ts} +3 -11
  31. package/dist/{types-DW42y281.d.cts → types-BszcepJK.d.cts} +1 -1
  32. package/dist/{types-n8Zge2zF.d.ts → types-Dhx9MuUp.d.ts} +1 -1
  33. package/dist/wa-sqlite.cjs +9 -9
  34. package/dist/wa-sqlite.cjs.map +1 -1
  35. package/dist/wa-sqlite.d.cts +13 -13
  36. package/dist/wa-sqlite.d.ts +13 -13
  37. package/dist/wa-sqlite.js +8 -8
  38. package/dist/wa-sqlite.js.map +1 -1
  39. package/package.json +2 -2
  40. package/src/index.native.ts +1 -1
  41. package/src/index.shared.ts +5 -14
  42. package/src/index.ts +1 -1
  43. package/src/node.ts +1 -1
  44. package/src/storage/sqlite/SQLiteAdapter.ts +10 -13
  45. package/src/storage/sqlite/{SqliteQueryContext.ts → SQLiteQueryContext.ts} +1 -1
  46. package/src/storage/sqlite/drivers/{BetterSqlite3Driver.ts → BetterSQLite3Driver.ts} +8 -8
  47. package/src/storage/sqlite/drivers/CapacitorSQLiteDriver.ts +2 -2
  48. package/src/storage/sqlite/drivers/{WaSqliteDriver.ts → WaSQLiteDriver.ts} +20 -20
  49. package/src/storage/sqlite/index.ts +1 -1
  50. package/src/storage/sqlite/types.ts +2 -12
  51. package/src/storage/types.ts +2 -2
  52. package/src/wa-sqlite.ts +1 -1
@@ -1,4 +1,4 @@
1
- import { S as SQLiteDatabaseDriver, a as SQLiteRunResult, b as SQLiteQueryResult } from './types-CSbIAfu2.js';
1
+ import { S as SQLiteDatabaseDriver, a as SQLiteRunResult, b as SQLiteQueryResult } from './types-6-NyRQ0D.js';
2
2
 
3
3
  /**
4
4
  * Virtual File System (VFS) options for wa-sqlite.
@@ -6,7 +6,7 @@ import { S as SQLiteDatabaseDriver, a as SQLiteRunResult, b as SQLiteQueryResult
6
6
  *
7
7
  * @see https://github.com/rhashimoto/wa-sqlite/tree/master/src/examples#vfs-comparison
8
8
  */
9
- type WaSqliteVfsType =
9
+ type WaSQLiteVfsType =
10
10
  /**
11
11
  * IDBBatchAtomicVFS - IndexedDB-backed storage
12
12
  * - Works on ALL contexts (Window, Worker, SharedWorker, service worker)
@@ -44,14 +44,14 @@ type WaSqliteVfsType =
44
44
  */
45
45
  | 'AccessHandlePoolVFS';
46
46
  /**
47
- * Options for configuring the WaSqliteDriver.
47
+ * Options for configuring the WaSQLiteDriver.
48
48
  */
49
- interface WaSqliteDriverOptions {
49
+ interface WaSQLiteDriverOptions {
50
50
  /**
51
51
  * Virtual File System to use for storage.
52
52
  * @default 'IDBBatchAtomicVFS'
53
53
  */
54
- vfs?: WaSqliteVfsType;
54
+ vfs?: WaSQLiteVfsType;
55
55
  /**
56
56
  * Directory path for the database in OPFS VFS modes.
57
57
  * Only used with OPFS-based VFS types.
@@ -113,20 +113,20 @@ interface WaSqliteDriverOptions {
113
113
  *
114
114
  * @example
115
115
  * ```ts
116
- * import { WaSqliteDriver } from '@anfenn/dync/wa-sqlite';
116
+ * import { WaSQLiteDriver } from '@anfenn/dync/wa-sqlite';
117
117
  * import { SQLiteAdapter } from '@anfenn/dync';
118
118
  *
119
119
  * // Default: IDBBatchAtomicVFS (works in main thread, multi-tab safe)
120
- * const driver = new WaSqliteDriver('myapp.db');
120
+ * const driver = new WaSQLiteDriver('myapp.db');
121
121
  *
122
122
  * // For OPFS (faster, requires Worker, filesystem transparent)
123
- * const opfsDriver = new WaSqliteDriver('myapp.db', { vfs: 'OPFSCoopSyncVFS' });
123
+ * const opfsDriver = new WaSQLiteDriver('myapp.db', { vfs: 'OPFSCoopSyncVFS' });
124
124
  *
125
125
  * const adapter = new SQLiteAdapter('myapp', driver);
126
126
  * ```
127
127
  */
128
- declare class WaSqliteDriver implements SQLiteDatabaseDriver {
129
- readonly type = "WaSqliteDriver";
128
+ declare class WaSQLiteDriver implements SQLiteDatabaseDriver {
129
+ readonly type = "WaSQLiteDriver";
130
130
  private db;
131
131
  private sqlite3;
132
132
  private readonly options;
@@ -134,7 +134,7 @@ declare class WaSqliteDriver implements SQLiteDatabaseDriver {
134
134
  private openPromise;
135
135
  private executionLock;
136
136
  readonly name: string;
137
- constructor(databaseName: string, options?: WaSqliteDriverOptions);
137
+ constructor(databaseName: string, options?: WaSQLiteDriverOptions);
138
138
  /**
139
139
  * Execute a callback with exclusive database access.
140
140
  * This prevents concurrent operations which can corrupt the database.
@@ -162,7 +162,7 @@ declare class WaSqliteDriver implements SQLiteDatabaseDriver {
162
162
  /**
163
163
  * Get the VFS type being used by this driver.
164
164
  */
165
- getVfsType(): WaSqliteVfsType;
165
+ getVfsType(): WaSQLiteVfsType;
166
166
  /**
167
167
  * Delete the database.
168
168
  * This will close the database if open and remove all persisted data.
@@ -172,4 +172,4 @@ declare class WaSqliteDriver implements SQLiteDatabaseDriver {
172
172
  delete(): Promise<void>;
173
173
  }
174
174
 
175
- export { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult, WaSqliteDriver, type WaSqliteDriverOptions, type WaSqliteVfsType };
175
+ export { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult, WaSQLiteDriver, type WaSQLiteDriverOptions, type WaSQLiteVfsType };
package/dist/wa-sqlite.js CHANGED
@@ -1,10 +1,10 @@
1
- // src/storage/sqlite/drivers/WaSqliteDriver.ts
1
+ // src/storage/sqlite/drivers/WaSQLiteDriver.ts
2
2
  var cachedModuleFactory = null;
3
3
  var cachedModule = null;
4
- var cachedSqlite3 = null;
4
+ var cachedSQLite3 = null;
5
5
  var registeredVFS = /* @__PURE__ */ new Map();
6
- var WaSqliteDriver = class {
7
- type = "WaSqliteDriver";
6
+ var WaSQLiteDriver = class {
7
+ type = "WaSQLiteDriver";
8
8
  db = null;
9
9
  sqlite3 = null;
10
10
  options;
@@ -54,11 +54,11 @@ var WaSqliteDriver = class {
54
54
  }
55
55
  async _open() {
56
56
  const module = await this.loadWasmModule();
57
- if (!cachedSqlite3) {
57
+ if (!cachedSQLite3) {
58
58
  const { Factory } = await import("@journeyapps/wa-sqlite");
59
- cachedSqlite3 = Factory(module);
59
+ cachedSQLite3 = Factory(module);
60
60
  }
61
- this.sqlite3 = cachedSqlite3;
61
+ this.sqlite3 = cachedSQLite3;
62
62
  const vfsName = `dync_${this.options.vfs}_${this.name}`.replace(/[^a-zA-Z0-9_-]/g, "_");
63
63
  let existingVfs = registeredVFS.get(vfsName);
64
64
  if (!existingVfs) {
@@ -276,6 +276,6 @@ var WaSqliteDriver = class {
276
276
  }
277
277
  };
278
278
  export {
279
- WaSqliteDriver
279
+ WaSQLiteDriver
280
280
  };
281
281
  //# sourceMappingURL=wa-sqlite.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/storage/sqlite/drivers/WaSqliteDriver.ts"],"sourcesContent":["import type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from '../types';\n\n/**\n * Virtual File System (VFS) options for wa-sqlite.\n * Each VFS has different trade-offs for performance, durability, and compatibility.\n *\n * @see https://github.com/rhashimoto/wa-sqlite/tree/master/src/examples#vfs-comparison\n */\nexport type WaSqliteVfsType =\n /**\n * IDBBatchAtomicVFS - IndexedDB-backed storage\n * - Works on ALL contexts (Window, Worker, SharedWorker, service worker)\n * - Supports multiple connections\n * - Full durability with batch atomic writes\n * - Good general-purpose choice for maximum compatibility\n * @recommended For apps that need to work in main thread and don't need OPFS\n */\n | 'IDBBatchAtomicVFS'\n /**\n * IDBMirrorVFS - IndexedDB with in-memory mirror\n * - Works on ALL contexts\n * - Supports multiple connections\n * - Much faster than IDBBatchAtomicVFS\n * - Database must fit in available memory\n * @recommended For small databases where performance is critical\n */\n | 'IDBMirrorVFS'\n /**\n * OPFSCoopSyncVFS - OPFS with cooperative synchronous access\n * - Requires Worker context\n * - Supports multiple connections\n * - Filesystem transparent (can import/export files)\n * - Good balance of performance and compatibility\n * @recommended For apps needing OPFS with multi-connection support\n */\n | 'OPFSCoopSyncVFS'\n /**\n * AccessHandlePoolVFS - OPFS-backed storage (fastest single connection)\n * - Requires Worker context\n * - Single connection only (no multi-tab support)\n * - Best performance, supports WAL mode\n * - NOT filesystem transparent\n * @recommended For single-tab apps where performance is critical\n */\n | 'AccessHandlePoolVFS';\n\n/**\n * Options for configuring the WaSqliteDriver.\n */\nexport interface WaSqliteDriverOptions {\n /**\n * Virtual File System to use for storage.\n * @default 'IDBBatchAtomicVFS'\n */\n vfs?: WaSqliteVfsType;\n\n /**\n * Directory path for the database in OPFS VFS modes.\n * Only used with OPFS-based VFS types.\n * @default '/'\n */\n directory?: string;\n\n /**\n * SQLite page size in bytes.\n * Larger pages can improve read performance for large BLOBs.\n * Cannot be changed after database creation for IDBBatchAtomicVFS/IDBMirrorVFS.\n * @default 4096\n */\n pageSize?: number;\n\n /**\n * SQLite cache size in pages (negative = KB, positive = pages).\n * Larger cache improves performance but uses more memory.\n * For IDBBatchAtomicVFS, must be large enough to hold journal for batch atomic mode.\n * @default -2000 (2MB)\n */\n cacheSize?: number;\n\n /**\n * Enable WAL (Write-Ahead Logging) mode.\n * Only supported with AccessHandlePoolVFS (with locking_mode=exclusive).\n * For other VFS types, this is ignored.\n * @default false\n */\n wal?: boolean;\n\n /**\n * Set synchronous pragma for durability vs performance trade-off.\n * - 'full': Maximum durability (default)\n * - 'normal': Relaxed durability, better performance (supported by IDBBatchAtomicVFS, IDBMirrorVFS, OPFSPermutedVFS)\n * - 'off': No sync, fastest but risks data loss on crash\n * @default 'full'\n */\n synchronous?: 'full' | 'normal' | 'off';\n}\n\n// Internal VFS interface for lifecycle management\ninterface WaSqliteVFS {\n close(): Promise<void>;\n name: string;\n}\n\n// VFS class type with static create method\ninterface VFSClass {\n create(name: string, module: any, options?: any): Promise<WaSqliteVFS>;\n}\n\n// Cached module factory and instance\nlet cachedModuleFactory: (() => Promise<any>) | null = null;\nlet cachedModule: any = null;\nlet cachedSqlite3: any = null;\n// Track VFS instances by name to avoid re-registering\nconst registeredVFS = new Map<string, WaSqliteVFS>();\n\n/**\n * SQLite driver for web browsers using wa-sqlite with IndexedDB or OPFS persistence.\n * Provides robust, persistent SQLite storage in the browser that prevents data loss.\n *\n * ## Data Safety Features\n *\n * - **IDBBatchAtomicVFS** (default): Uses IndexedDB batch atomic writes to ensure transactions\n * are either fully committed or not at all. Multi-tab safe.\n * - **IDBMirrorVFS**: IndexedDB with in-memory mirror. Much faster, database must fit in RAM.\n * - **OPFSCoopSyncVFS**: OPFS with cooperative sync. Multi-connection, filesystem transparent.\n * - **AccessHandlePoolVFS**: Uses OPFS Access Handles for high performance. Single-tab only.\n * - Full durability by default (`PRAGMA synchronous=full`)\n * - Automatic journal mode configuration for each VFS type\n *\n * ## VFS Selection Guide\n *\n * | VFS | Best For | Multi-Tab | Speed |\n * |-----|----------|-----------|-------|\n * | IDBBatchAtomicVFS | General use, main thread | ✅ | Good |\n * | IDBMirrorVFS | Small DBs, main thread | ✅ | Fast |\n * | OPFSCoopSyncVFS | Web Workers, file export | ✅ | Good |\n * | AccessHandlePoolVFS | Single-tab performance | ❌ | Fastest |\n *\n * @example\n * ```ts\n * import { WaSqliteDriver } from '@anfenn/dync/wa-sqlite';\n * import { SQLiteAdapter } from '@anfenn/dync';\n *\n * // Default: IDBBatchAtomicVFS (works in main thread, multi-tab safe)\n * const driver = new WaSqliteDriver('myapp.db');\n *\n * // For OPFS (faster, requires Worker, filesystem transparent)\n * const opfsDriver = new WaSqliteDriver('myapp.db', { vfs: 'OPFSCoopSyncVFS' });\n *\n * const adapter = new SQLiteAdapter('myapp', driver);\n * ```\n */\nexport class WaSqliteDriver implements SQLiteDatabaseDriver {\n readonly type = 'WaSqliteDriver';\n private db: number | null = null;\n private sqlite3: any = null;\n private readonly options: Required<WaSqliteDriverOptions>;\n private opened = false;\n private openPromise: Promise<void> | null = null;\n // Mutex to prevent concurrent database operations (critical for wa-sqlite)\n private executionLock: Promise<void> = Promise.resolve();\n readonly name: string;\n\n constructor(databaseName: string, options: WaSqliteDriverOptions = {}) {\n this.name = databaseName;\n this.options = {\n vfs: 'IDBBatchAtomicVFS',\n directory: '/',\n pageSize: 4096,\n cacheSize: -2000,\n wal: false,\n synchronous: 'full',\n ...options,\n };\n }\n\n /**\n * Execute a callback with exclusive database access.\n * This prevents concurrent operations which can corrupt the database.\n */\n private async withLock<T>(fn: () => Promise<T>): Promise<T> {\n // Chain onto the existing lock\n const previousLock = this.executionLock;\n let releaseLock: () => void;\n this.executionLock = new Promise<void>((resolve) => {\n releaseLock = resolve;\n });\n\n try {\n // Wait for previous operation to complete\n await previousLock;\n // Execute our operation\n return await fn();\n } finally {\n // Release the lock\n releaseLock!();\n }\n }\n\n async open(): Promise<void> {\n if (this.opened) return;\n if (this.openPromise) return this.openPromise;\n\n this.openPromise = this._open();\n\n try {\n await this.openPromise;\n } finally {\n this.openPromise = null;\n }\n }\n\n private async _open(): Promise<void> {\n // Load wa-sqlite module (asyncify build for async VFS support)\n const module = await this.loadWasmModule();\n\n // Create SQLite API from module (cached - must only create once per module)\n if (!cachedSqlite3) {\n const { Factory } = await import('@journeyapps/wa-sqlite');\n cachedSqlite3 = Factory(module);\n }\n this.sqlite3 = cachedSqlite3;\n\n // For IDB-based VFS, the VFS name is also used as the IndexedDB database name\n // Use a unique name based on database name to avoid conflicts\n const vfsName = `dync_${this.options.vfs}_${this.name}`.replace(/[^a-zA-Z0-9_-]/g, '_');\n\n // Reuse existing VFS instance or create and register a new one\n let existingVfs = registeredVFS.get(vfsName);\n if (!existingVfs) {\n existingVfs = await this.createVFS(module, vfsName);\n // Register VFS with SQLite as default (like PowerSync does)\n this.sqlite3.vfs_register(existingVfs, true);\n registeredVFS.set(vfsName, existingVfs);\n }\n\n // Build database path - for IDB VFS, this is the \"file\" path within the VFS\n const dbPath = this.buildDatabasePath();\n\n // Open database (VFS is registered as default)\n this.db = await this.sqlite3.open_v2(dbPath);\n\n // Configure database pragmas for performance and durability\n await this.configurePragmas();\n\n this.opened = true;\n }\n\n private async loadWasmModule(): Promise<any> {\n if (!cachedModule) {\n if (!cachedModuleFactory) {\n // Dynamically import the asyncify build for async VFS support\n const wasmModule = await import('@journeyapps/wa-sqlite/dist/wa-sqlite-async.mjs');\n cachedModuleFactory = wasmModule.default;\n }\n // Cache the module instance - all VFS and sqlite3 APIs must share the same module\n cachedModule = await cachedModuleFactory();\n }\n return cachedModule;\n }\n\n private async createVFS(module: any, vfsName: string): Promise<WaSqliteVFS> {\n const vfsType = this.options.vfs;\n let VFSClass: VFSClass;\n let vfsOptions: any = undefined;\n\n // Dynamically import VFS implementation\n // Note: We cast to unknown first because the package types don't include the static create method\n switch (vfsType) {\n case 'IDBBatchAtomicVFS': {\n const mod = await import('@journeyapps/wa-sqlite/src/examples/IDBBatchAtomicVFS.js');\n VFSClass = mod.IDBBatchAtomicVFS as unknown as VFSClass;\n // Use exclusive lock policy like PowerSync does\n vfsOptions = { lockPolicy: 'exclusive' };\n break;\n }\n case 'IDBMirrorVFS': {\n const mod = await import('@journeyapps/wa-sqlite/src/examples/IDBMirrorVFS.js');\n VFSClass = mod.IDBMirrorVFS as unknown as VFSClass;\n break;\n }\n case 'OPFSCoopSyncVFS': {\n const mod = await import('@journeyapps/wa-sqlite/src/examples/OPFSCoopSyncVFS.js');\n VFSClass = mod.OPFSCoopSyncVFS as unknown as VFSClass;\n break;\n }\n case 'AccessHandlePoolVFS': {\n const mod = await import('@journeyapps/wa-sqlite/src/examples/AccessHandlePoolVFS.js');\n VFSClass = mod.AccessHandlePoolVFS as unknown as VFSClass;\n break;\n }\n default:\n throw new Error(`Unsupported VFS type: ${vfsType}`);\n }\n\n return VFSClass.create(vfsName, module, vfsOptions);\n }\n\n private buildDatabasePath(): string {\n const vfsType = this.options.vfs;\n\n // For IDB-based VFS, use database name directly\n if (vfsType === 'IDBBatchAtomicVFS' || vfsType === 'IDBMirrorVFS') {\n return this.name;\n }\n\n // For OPFS-based VFS, build full path\n const directory = this.options.directory.replace(/\\/$/, '');\n return `${directory}/${this.name}`;\n }\n\n private async configurePragmas(): Promise<void> {\n if (!this.db || !this.sqlite3) return;\n\n // Page size can only be set on new/empty databases\n // For IDBBatchAtomicVFS, it cannot be changed after creation\n // Try to set it, but ignore errors if the database already exists\n try {\n await this.sqlite3.exec(this.db, `PRAGMA page_size = ${this.options.pageSize}`);\n } catch {\n // Page size already set, ignore\n }\n\n // Cache size for performance\n await this.sqlite3.exec(this.db, `PRAGMA cache_size = ${this.options.cacheSize}`);\n\n // WAL mode only for AccessHandlePoolVFS with exclusive locking\n if (this.options.wal && this.options.vfs === 'AccessHandlePoolVFS') {\n await this.sqlite3.exec(this.db, 'PRAGMA locking_mode = exclusive');\n await this.sqlite3.exec(this.db, 'PRAGMA journal_mode = WAL');\n }\n // Note: For IDB-based VFS, we don't set journal_mode - let the VFS handle it\n }\n\n async close(): Promise<void> {\n if (!this.opened || !this.db || !this.sqlite3) return;\n\n // Wait for any pending operations to complete by acquiring the lock\n await this.withLock(async () => {\n // Ensure all data is flushed before closing\n // This is critical for IDB-based VFS which batch writes\n try {\n await this.sqlite3!.exec(this.db!, 'PRAGMA wal_checkpoint(TRUNCATE)');\n } catch {\n // Ignore if WAL mode not enabled\n }\n\n await this.sqlite3!.close(this.db!);\n\n // Don't close the shared VFS - it may be used by other connections\n // The VFS will be cleaned up when all references are gone\n this.db = null;\n this.opened = false;\n });\n }\n\n async execute(statement: string): Promise<void> {\n await this.open();\n\n if (!this.db || !this.sqlite3) {\n throw new Error('Database not initialized');\n }\n\n await this.withLock(async () => {\n await this.sqlite3.exec(this.db, statement);\n });\n }\n\n async run(statement: string, values: unknown[] = []): Promise<SQLiteRunResult> {\n await this.open();\n\n if (!this.db || !this.sqlite3) {\n throw new Error('Database not initialized');\n }\n\n return this.withLock(async () => {\n // Convert values for SQLite (booleans -> integers)\n const convertedValues = this.convertValues(values);\n\n // Use statements() generator with proper binding\n for await (const stmt of this.sqlite3.statements(this.db, statement)) {\n if (stmt === null) {\n break;\n }\n\n // Reset statement before binding (critical for wa-sqlite)\n this.sqlite3.reset(stmt);\n\n // Bind parameters if any\n if (convertedValues.length > 0) {\n this.sqlite3.bind_collection(stmt, convertedValues);\n }\n\n // Execute the statement\n await this.sqlite3.step(stmt);\n }\n\n return {\n changes: this.sqlite3.changes(this.db),\n lastId: Number(this.sqlite3.last_insert_id(this.db)),\n };\n });\n }\n\n /**\n * Convert values for SQLite compatibility.\n * - Booleans must be converted to integers (SQLite has no boolean type)\n */\n private convertValues(values: unknown[]): unknown[] {\n return values.map((value) => {\n if (typeof value === 'boolean') {\n return value ? 1 : 0;\n }\n return value;\n });\n }\n\n async query(statement: string, values: unknown[] = []): Promise<SQLiteQueryResult> {\n await this.open();\n\n if (!this.db || !this.sqlite3) {\n throw new Error('Database not initialized');\n }\n\n return this.withLock(async () => {\n const { SQLITE_ROW } = await import('@journeyapps/wa-sqlite');\n const allRows: unknown[][] = [];\n let columns: string[] = [];\n\n // Convert values for SQLite (booleans -> integers)\n const convertedValues = this.convertValues(values);\n\n // Use statements() generator with proper binding\n for await (const stmt of this.sqlite3.statements(this.db, statement)) {\n if (stmt === null) {\n break;\n }\n\n // Reset statement before binding (critical for wa-sqlite)\n this.sqlite3.reset(stmt);\n\n // Bind parameters if any\n if (convertedValues.length > 0) {\n this.sqlite3.bind_collection(stmt, convertedValues);\n }\n\n // Get column names\n if (columns.length === 0) {\n columns = this.sqlite3.column_names(stmt);\n }\n\n // Fetch all rows\n while ((await this.sqlite3.step(stmt)) === SQLITE_ROW) {\n const row = this.sqlite3.row(stmt);\n allRows.push(row);\n }\n }\n\n return { columns, values: allRows };\n });\n }\n\n /**\n * Check if the database is currently open.\n */\n isOpen(): boolean {\n return this.opened;\n }\n\n /**\n * Get the VFS type being used by this driver.\n */\n getVfsType(): WaSqliteVfsType {\n return this.options.vfs;\n }\n\n /**\n * Delete the database.\n * This will close the database if open and remove all persisted data.\n * For IndexedDB-based VFS, this deletes the IndexedDB database.\n * For OPFS-based VFS, this removes the files from OPFS.\n */\n async delete(): Promise<void> {\n // Close if open\n if (this.opened) {\n await this.close();\n }\n\n const vfsType = this.options.vfs;\n\n if (vfsType === 'IDBBatchAtomicVFS' || vfsType === 'IDBMirrorVFS') {\n // Delete IndexedDB database\n await new Promise<void>((resolve, reject) => {\n const request = indexedDB.deleteDatabase(this.name);\n request.onsuccess = () => resolve();\n request.onerror = () => reject(request.error);\n request.onblocked = () => {\n console.warn(`Database deletion blocked for ${this.name}. Close all connections and try again.`);\n };\n });\n } else {\n // For OPFS-based VFS, remove the directory\n const dbPath = this.buildDatabasePath();\n const root = await navigator.storage.getDirectory();\n\n try {\n // Try to remove the file/directory\n const pathParts = dbPath.split('/').filter(Boolean);\n let current = root;\n\n // Navigate to parent directory\n for (let i = 0; i < pathParts.length - 1; i++) {\n current = await current.getDirectoryHandle(pathParts[i]!);\n }\n\n // Remove the database file\n const filename = pathParts[pathParts.length - 1]!;\n await current.removeEntry(filename, { recursive: true });\n\n // Also try to remove associated journal/wal files\n const associatedFiles = [`${filename}-journal`, `${filename}-wal`, `${filename}-shm`];\n for (const file of associatedFiles) {\n try {\n await current.removeEntry(file, { recursive: false });\n } catch {\n // Ignore if file doesn't exist\n }\n }\n } catch (error) {\n // Ignore if directory doesn't exist\n if ((error as Error).name !== 'NotFoundError') {\n throw error;\n }\n }\n }\n }\n}\n"],"mappings":";AA6GA,IAAI,sBAAmD;AACvD,IAAI,eAAoB;AACxB,IAAI,gBAAqB;AAEzB,IAAM,gBAAgB,oBAAI,IAAyB;AAuC5C,IAAM,iBAAN,MAAqD;AAAA,EAC/C,OAAO;AAAA,EACR,KAAoB;AAAA,EACpB,UAAe;AAAA,EACN;AAAA,EACT,SAAS;AAAA,EACT,cAAoC;AAAA;AAAA,EAEpC,gBAA+B,QAAQ,QAAQ;AAAA,EAC9C;AAAA,EAET,YAAY,cAAsB,UAAiC,CAAC,GAAG;AACnE,SAAK,OAAO;AACZ,SAAK,UAAU;AAAA,MACX,KAAK;AAAA,MACL,WAAW;AAAA,MACX,UAAU;AAAA,MACV,WAAW;AAAA,MACX,KAAK;AAAA,MACL,aAAa;AAAA,MACb,GAAG;AAAA,IACP;AAAA,EACJ;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAc,SAAY,IAAkC;AAExD,UAAM,eAAe,KAAK;AAC1B,QAAI;AACJ,SAAK,gBAAgB,IAAI,QAAc,CAAC,YAAY;AAChD,oBAAc;AAAA,IAClB,CAAC;AAED,QAAI;AAEA,YAAM;AAEN,aAAO,MAAM,GAAG;A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;","names":[]}
1
+ {"version":3,"sources":["../src/storage/sqlite/drivers/WaSQLiteDriver.ts"],"sourcesContent":["import type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from '../types';\n\n/**\n * Virtual File System (VFS) options for wa-sqlite.\n * Each VFS has different trade-offs for performance, durability, and compatibility.\n *\n * @see https://github.com/rhashimoto/wa-sqlite/tree/master/src/examples#vfs-comparison\n */\nexport type WaSQLiteVfsType =\n /**\n * IDBBatchAtomicVFS - IndexedDB-backed storage\n * - Works on ALL contexts (Window, Worker, SharedWorker, service worker)\n * - Supports multiple connections\n * - Full durability with batch atomic writes\n * - Good general-purpose choice for maximum compatibility\n * @recommended For apps that need to work in main thread and don't need OPFS\n */\n | 'IDBBatchAtomicVFS'\n /**\n * IDBMirrorVFS - IndexedDB with in-memory mirror\n * - Works on ALL contexts\n * - Supports multiple connections\n * - Much faster than IDBBatchAtomicVFS\n * - Database must fit in available memory\n * @recommended For small databases where performance is critical\n */\n | 'IDBMirrorVFS'\n /**\n * OPFSCoopSyncVFS - OPFS with cooperative synchronous access\n * - Requires Worker context\n * - Supports multiple connections\n * - Filesystem transparent (can import/export files)\n * - Good balance of performance and compatibility\n * @recommended For apps needing OPFS with multi-connection support\n */\n | 'OPFSCoopSyncVFS'\n /**\n * AccessHandlePoolVFS - OPFS-backed storage (fastest single connection)\n * - Requires Worker context\n * - Single connection only (no multi-tab support)\n * - Best performance, supports WAL mode\n * - NOT filesystem transparent\n * @recommended For single-tab apps where performance is critical\n */\n | 'AccessHandlePoolVFS';\n\n/**\n * Options for configuring the WaSQLiteDriver.\n */\nexport interface WaSQLiteDriverOptions {\n /**\n * Virtual File System to use for storage.\n * @default 'IDBBatchAtomicVFS'\n */\n vfs?: WaSQLiteVfsType;\n\n /**\n * Directory path for the database in OPFS VFS modes.\n * Only used with OPFS-based VFS types.\n * @default '/'\n */\n directory?: string;\n\n /**\n * SQLite page size in bytes.\n * Larger pages can improve read performance for large BLOBs.\n * Cannot be changed after database creation for IDBBatchAtomicVFS/IDBMirrorVFS.\n * @default 4096\n */\n pageSize?: number;\n\n /**\n * SQLite cache size in pages (negative = KB, positive = pages).\n * Larger cache improves performance but uses more memory.\n * For IDBBatchAtomicVFS, must be large enough to hold journal for batch atomic mode.\n * @default -2000 (2MB)\n */\n cacheSize?: number;\n\n /**\n * Enable WAL (Write-Ahead Logging) mode.\n * Only supported with AccessHandlePoolVFS (with locking_mode=exclusive).\n * For other VFS types, this is ignored.\n * @default false\n */\n wal?: boolean;\n\n /**\n * Set synchronous pragma for durability vs performance trade-off.\n * - 'full': Maximum durability (default)\n * - 'normal': Relaxed durability, better performance (supported by IDBBatchAtomicVFS, IDBMirrorVFS, OPFSPermutedVFS)\n * - 'off': No sync, fastest but risks data loss on crash\n * @default 'full'\n */\n synchronous?: 'full' | 'normal' | 'off';\n}\n\n// Internal VFS interface for lifecycle management\ninterface WaSQLiteVFS {\n close(): Promise<void>;\n name: string;\n}\n\n// VFS class type with static create method\ninterface VFSClass {\n create(name: string, module: any, options?: any): Promise<WaSQLiteVFS>;\n}\n\n// Cached module factory and instance\nlet cachedModuleFactory: (() => Promise<any>) | null = null;\nlet cachedModule: any = null;\nlet cachedSQLite3: any = null;\n// Track VFS instances by name to avoid re-registering\nconst registeredVFS = new Map<string, WaSQLiteVFS>();\n\n/**\n * SQLite driver for web browsers using wa-sqlite with IndexedDB or OPFS persistence.\n * Provides robust, persistent SQLite storage in the browser that prevents data loss.\n *\n * ## Data Safety Features\n *\n * - **IDBBatchAtomicVFS** (default): Uses IndexedDB batch atomic writes to ensure transactions\n * are either fully committed or not at all. Multi-tab safe.\n * - **IDBMirrorVFS**: IndexedDB with in-memory mirror. Much faster, database must fit in RAM.\n * - **OPFSCoopSyncVFS**: OPFS with cooperative sync. Multi-connection, filesystem transparent.\n * - **AccessHandlePoolVFS**: Uses OPFS Access Handles for high performance. Single-tab only.\n * - Full durability by default (`PRAGMA synchronous=full`)\n * - Automatic journal mode configuration for each VFS type\n *\n * ## VFS Selection Guide\n *\n * | VFS | Best For | Multi-Tab | Speed |\n * |-----|----------|-----------|-------|\n * | IDBBatchAtomicVFS | General use, main thread | ✅ | Good |\n * | IDBMirrorVFS | Small DBs, main thread | ✅ | Fast |\n * | OPFSCoopSyncVFS | Web Workers, file export | ✅ | Good |\n * | AccessHandlePoolVFS | Single-tab performance | ❌ | Fastest |\n *\n * @example\n * ```ts\n * import { WaSQLiteDriver } from '@anfenn/dync/wa-sqlite';\n * import { SQLiteAdapter } from '@anfenn/dync';\n *\n * // Default: IDBBatchAtomicVFS (works in main thread, multi-tab safe)\n * const driver = new WaSQLiteDriver('myapp.db');\n *\n * // For OPFS (faster, requires Worker, filesystem transparent)\n * const opfsDriver = new WaSQLiteDriver('myapp.db', { vfs: 'OPFSCoopSyncVFS' });\n *\n * const adapter = new SQLiteAdapter('myapp', driver);\n * ```\n */\nexport class WaSQLiteDriver implements SQLiteDatabaseDriver {\n readonly type = 'WaSQLiteDriver';\n private db: number | null = null;\n private sqlite3: any = null;\n private readonly options: Required<WaSQLiteDriverOptions>;\n private opened = false;\n private openPromise: Promise<void> | null = null;\n // Mutex to prevent concurrent database operations (critical for wa-sqlite)\n private executionLock: Promise<void> = Promise.resolve();\n readonly name: string;\n\n constructor(databaseName: string, options: WaSQLiteDriverOptions = {}) {\n this.name = databaseName;\n this.options = {\n vfs: 'IDBBatchAtomicVFS',\n directory: '/',\n pageSize: 4096,\n cacheSize: -2000,\n wal: false,\n synchronous: 'full',\n ...options,\n };\n }\n\n /**\n * Execute a callback with exclusive database access.\n * This prevents concurrent operations which can corrupt the database.\n */\n private async withLock<T>(fn: () => Promise<T>): Promise<T> {\n // Chain onto the existing lock\n const previousLock = this.executionLock;\n let releaseLock: () => void;\n this.executionLock = new Promise<void>((resolve) => {\n releaseLock = resolve;\n });\n\n try {\n // Wait for previous operation to complete\n await previousLock;\n // Execute our operation\n return await fn();\n } finally {\n // Release the lock\n releaseLock!();\n }\n }\n\n async open(): Promise<void> {\n if (this.opened) return;\n if (this.openPromise) return this.openPromise;\n\n this.openPromise = this._open();\n\n try {\n await this.openPromise;\n } finally {\n this.openPromise = null;\n }\n }\n\n private async _open(): Promise<void> {\n // Load wa-sqlite module (asyncify build for async VFS support)\n const module = await this.loadWasmModule();\n\n // Create SQLite API from module (cached - must only create once per module)\n if (!cachedSQLite3) {\n const { Factory } = await import('@journeyapps/wa-sqlite');\n cachedSQLite3 = Factory(module);\n }\n this.sqlite3 = cachedSQLite3;\n\n // For IDB-based VFS, the VFS name is also used as the IndexedDB database name\n // Use a unique name based on database name to avoid conflicts\n const vfsName = `dync_${this.options.vfs}_${this.name}`.replace(/[^a-zA-Z0-9_-]/g, '_');\n\n // Reuse existing VFS instance or create and register a new one\n let existingVfs = registeredVFS.get(vfsName);\n if (!existingVfs) {\n existingVfs = await this.createVFS(module, vfsName);\n // Register VFS with SQLite as default (like PowerSync does)\n this.sqlite3.vfs_register(existingVfs, true);\n registeredVFS.set(vfsName, existingVfs);\n }\n\n // Build database path - for IDB VFS, this is the \"file\" path within the VFS\n const dbPath = this.buildDatabasePath();\n\n // Open database (VFS is registered as default)\n this.db = await this.sqlite3.open_v2(dbPath);\n\n // Configure database pragmas for performance and durability\n await this.configurePragmas();\n\n this.opened = true;\n }\n\n private async loadWasmModule(): Promise<any> {\n if (!cachedModule) {\n if (!cachedModuleFactory) {\n // Dynamically import the asyncify build for async VFS support\n const wasmModule = await import('@journeyapps/wa-sqlite/dist/wa-sqlite-async.mjs');\n cachedModuleFactory = wasmModule.default;\n }\n // Cache the module instance - all VFS and sqlite3 APIs must share the same module\n cachedModule = await cachedModuleFactory();\n }\n return cachedModule;\n }\n\n private async createVFS(module: any, vfsName: string): Promise<WaSQLiteVFS> {\n const vfsType = this.options.vfs;\n let VFSClass: VFSClass;\n let vfsOptions: any = undefined;\n\n // Dynamically import VFS implementation\n // Note: We cast to unknown first because the package types don't include the static create method\n switch (vfsType) {\n case 'IDBBatchAtomicVFS': {\n const mod = await import('@journeyapps/wa-sqlite/src/examples/IDBBatchAtomicVFS.js');\n VFSClass = mod.IDBBatchAtomicVFS as unknown as VFSClass;\n // Use exclusive lock policy like PowerSync does\n vfsOptions = { lockPolicy: 'exclusive' };\n break;\n }\n case 'IDBMirrorVFS': {\n const mod = await import('@journeyapps/wa-sqlite/src/examples/IDBMirrorVFS.js');\n VFSClass = mod.IDBMirrorVFS as unknown as VFSClass;\n break;\n }\n case 'OPFSCoopSyncVFS': {\n const mod = await import('@journeyapps/wa-sqlite/src/examples/OPFSCoopSyncVFS.js');\n VFSClass = mod.OPFSCoopSyncVFS as unknown as VFSClass;\n break;\n }\n case 'AccessHandlePoolVFS': {\n const mod = await import('@journeyapps/wa-sqlite/src/examples/AccessHandlePoolVFS.js');\n VFSClass = mod.AccessHandlePoolVFS as unknown as VFSClass;\n break;\n }\n default:\n throw new Error(`Unsupported VFS type: ${vfsType}`);\n }\n\n return VFSClass.create(vfsName, module, vfsOptions);\n }\n\n private buildDatabasePath(): string {\n const vfsType = this.options.vfs;\n\n // For IDB-based VFS, use database name directly\n if (vfsType === 'IDBBatchAtomicVFS' || vfsType === 'IDBMirrorVFS') {\n return this.name;\n }\n\n // For OPFS-based VFS, build full path\n const directory = this.options.directory.replace(/\\/$/, '');\n return `${directory}/${this.name}`;\n }\n\n private async configurePragmas(): Promise<void> {\n if (!this.db || !this.sqlite3) return;\n\n // Page size can only be set on new/empty databases\n // For IDBBatchAtomicVFS, it cannot be changed after creation\n // Try to set it, but ignore errors if the database already exists\n try {\n await this.sqlite3.exec(this.db, `PRAGMA page_size = ${this.options.pageSize}`);\n } catch {\n // Page size already set, ignore\n }\n\n // Cache size for performance\n await this.sqlite3.exec(this.db, `PRAGMA cache_size = ${this.options.cacheSize}`);\n\n // WAL mode only for AccessHandlePoolVFS with exclusive locking\n if (this.options.wal && this.options.vfs === 'AccessHandlePoolVFS') {\n await this.sqlite3.exec(this.db, 'PRAGMA locking_mode = exclusive');\n await this.sqlite3.exec(this.db, 'PRAGMA journal_mode = WAL');\n }\n // Note: For IDB-based VFS, we don't set journal_mode - let the VFS handle it\n }\n\n async close(): Promise<void> {\n if (!this.opened || !this.db || !this.sqlite3) return;\n\n // Wait for any pending operations to complete by acquiring the lock\n await this.withLock(async () => {\n // Ensure all data is flushed before closing\n // This is critical for IDB-based VFS which batch writes\n try {\n await this.sqlite3!.exec(this.db!, 'PRAGMA wal_checkpoint(TRUNCATE)');\n } catch {\n // Ignore if WAL mode not enabled\n }\n\n await this.sqlite3!.close(this.db!);\n\n // Don't close the shared VFS - it may be used by other connections\n // The VFS will be cleaned up when all references are gone\n this.db = null;\n this.opened = false;\n });\n }\n\n async execute(statement: string): Promise<void> {\n await this.open();\n\n if (!this.db || !this.sqlite3) {\n throw new Error('Database not initialized');\n }\n\n await this.withLock(async () => {\n await this.sqlite3.exec(this.db, statement);\n });\n }\n\n async run(statement: string, values: unknown[] = []): Promise<SQLiteRunResult> {\n await this.open();\n\n if (!this.db || !this.sqlite3) {\n throw new Error('Database not initialized');\n }\n\n return this.withLock(async () => {\n // Convert values for SQLite (booleans -> integers)\n const convertedValues = this.convertValues(values);\n\n // Use statements() generator with proper binding\n for await (const stmt of this.sqlite3.statements(this.db, statement)) {\n if (stmt === null) {\n break;\n }\n\n // Reset statement before binding (critical for wa-sqlite)\n this.sqlite3.reset(stmt);\n\n // Bind parameters if any\n if (convertedValues.length > 0) {\n this.sqlite3.bind_collection(stmt, convertedValues);\n }\n\n // Execute the statement\n await this.sqlite3.step(stmt);\n }\n\n return {\n changes: this.sqlite3.changes(this.db),\n lastId: Number(this.sqlite3.last_insert_id(this.db)),\n };\n });\n }\n\n /**\n * Convert values for SQLite compatibility.\n * - Booleans must be converted to integers (SQLite has no boolean type)\n */\n private convertValues(values: unknown[]): unknown[] {\n return values.map((value) => {\n if (typeof value === 'boolean') {\n return value ? 1 : 0;\n }\n return value;\n });\n }\n\n async query(statement: string, values: unknown[] = []): Promise<SQLiteQueryResult> {\n await this.open();\n\n if (!this.db || !this.sqlite3) {\n throw new Error('Database not initialized');\n }\n\n return this.withLock(async () => {\n const { SQLITE_ROW } = await import('@journeyapps/wa-sqlite');\n const allRows: unknown[][] = [];\n let columns: string[] = [];\n\n // Convert values for SQLite (booleans -> integers)\n const convertedValues = this.convertValues(values);\n\n // Use statements() generator with proper binding\n for await (const stmt of this.sqlite3.statements(this.db, statement)) {\n if (stmt === null) {\n break;\n }\n\n // Reset statement before binding (critical for wa-sqlite)\n this.sqlite3.reset(stmt);\n\n // Bind parameters if any\n if (convertedValues.length > 0) {\n this.sqlite3.bind_collection(stmt, convertedValues);\n }\n\n // Get column names\n if (columns.length === 0) {\n columns = this.sqlite3.column_names(stmt);\n }\n\n // Fetch all rows\n while ((await this.sqlite3.step(stmt)) === SQLITE_ROW) {\n const row = this.sqlite3.row(stmt);\n allRows.push(row);\n }\n }\n\n return { columns, values: allRows };\n });\n }\n\n /**\n * Check if the database is currently open.\n */\n isOpen(): boolean {\n return this.opened;\n }\n\n /**\n * Get the VFS type being used by this driver.\n */\n getVfsType(): WaSQLiteVfsType {\n return this.options.vfs;\n }\n\n /**\n * Delete the database.\n * This will close the database if open and remove all persisted data.\n * For IndexedDB-based VFS, this deletes the IndexedDB database.\n * For OPFS-based VFS, this removes the files from OPFS.\n */\n async delete(): Promise<void> {\n // Close if open\n if (this.opened) {\n await this.close();\n }\n\n const vfsType = this.options.vfs;\n\n if (vfsType === 'IDBBatchAtomicVFS' || vfsType === 'IDBMirrorVFS') {\n // Delete IndexedDB database\n await new Promise<void>((resolve, reject) => {\n const request = indexedDB.deleteDatabase(this.name);\n request.onsuccess = () => resolve();\n request.onerror = () => reject(request.error);\n request.onblocked = () => {\n console.warn(`Database deletion blocked for ${this.name}. Close all connections and try again.`);\n };\n });\n } else {\n // For OPFS-based VFS, remove the directory\n const dbPath = this.buildDatabasePath();\n const root = await navigator.storage.getDirectory();\n\n try {\n // Try to remove the file/directory\n const pathParts = dbPath.split('/').filter(Boolean);\n let current = root;\n\n // Navigate to parent directory\n for (let i = 0; i < pathParts.length - 1; i++) {\n current = await current.getDirectoryHandle(pathParts[i]!);\n }\n\n // Remove the database file\n const filename = pathParts[pathParts.length - 1]!;\n await current.removeEntry(filename, { recursive: true });\n\n // Also try to remove associated journal/wal files\n const associatedFiles = [`${filename}-journal`, `${filename}-wal`, `${filename}-shm`];\n for (const file of associatedFiles) {\n try {\n await current.removeEntry(file, { recursive: false });\n } catch {\n // Ignore if file doesn't exist\n }\n }\n } catch (error) {\n // Ignore if directory doesn't exist\n if ((error as Error).name !== 'NotFoundError') {\n throw error;\n }\n }\n }\n }\n}\n"],"mappings":";AA6GA,IAAI,sBAAmD;AACvD,IAAI,eAAoB;AACxB,IAAI,gBAAqB;AAEzB,IAAM,gBAAgB,oBAAI,IAAyB;AAuC5C,IAAM,iBAAN,MAAqD;AAAA,EAC/C,OAAO;AAAA,EACR,KAAoB;AAAA,EACpB,UAAe;AAAA,EACN;AAAA,EACT,SAAS;AAAA,EACT,cAAoC;AAAA;AAAA,EAEpC,gBAA+B,QAAQ,QAAQ;AAAA,EAC9C;AAAA,EAET,YAAY,cAAsB,UAAiC,CAAC,GAAG;AACnE,SAAK,OAAO;AACZ,SAAK,UAAU;AAAA,MACX,KAAK;AAAA,MACL,WAAW;AAAA,MACX,UAAU;AAAA,MACV,WAAW;AAAA,MACX,KAAK;AAAA,MACL,aAAa;AAAA,MACb,GAAG;AAAA,IACP;AAAA,EACJ;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAc,SAAY,IAAkC;AAExD,UAAM,eAAe,KAAK;AAC1B,QAAI;AACJ,SAAK,gBAAgB,IAAI,QAAc,CAAC,YAAY;AAChD,oBAAc;AAAA,IAClB,CAAC;AAED,QAAI;AAEA,YAAM;AAEN,aAAO,MAAM,GAAG;AAAA,IACpB,UAAE;AAEE,kBAAa;AAAA,IACjB;AAAA,EACJ;AAAA,EAEA,MAAM,OAAsB;AACxB,QAAI,KAAK,OAAQ;AACjB,QAAI,KAAK,YAAa,QAAO,K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once, run IndexedDB & SQLite with sync anywhere - React, React Native, Expo, Capacitor, Electron & Node.js",
6
6
  "keywords": [
@@ -144,7 +144,7 @@
144
144
  "test:browser:full": "pnpm --filter tests test:browser:full",
145
145
  "reinstall": "find . -name node_modules -type d -prune -exec rm -rf {} + && pnpm install",
146
146
  "push": "bash -c '[ -n \"$1\" ] && git add . && git commit -m \"$1\" && git push' --",
147
- "release": "bash -c '[ -n \"$1\" ] || { echo \"Usage: pnpm release <commit-message>\"; exit 1; } && pnpm build:all && pnpm test:all && pnpm test:browser:full && git add . && git commit -m \"$1\" && npm version patch && git push && git push --tags && npm publish' --"
147
+ "release": "bash -c '[ -n \"$1\" ] || { echo \"Usage: pnpm release <commit-message>\"; exit 1; } && pnpm build:all && pnpm test:all && pnpm test:browser:full && git add . && git commit -m \"$1\" && npm version patch && git push && npm login && npm publish' --"
148
148
  },
149
149
  "devDependencies": {
150
150
  "@capacitor-community/sqlite": "7.0.2",
@@ -2,7 +2,7 @@ export * from './index.shared';
2
2
  export { MemoryAdapter } from './storage/memory';
3
3
  export { SQLiteAdapter } from './storage/sqlite';
4
4
  export { MemoryQueryContext } from './storage/memory';
5
- export { SqliteQueryContext } from './storage/sqlite';
5
+ export { SQLiteQueryContext } from './storage/sqlite';
6
6
  export type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from './storage/sqlite/types';
7
7
  export type { StorageAdapter } from './storage/types';
8
8
  export { SyncAction } from './types';
@@ -19,7 +19,7 @@ import {
19
19
  } from './types';
20
20
  import { addVisibilityChangeListener } from './addVisibilityChangeListener';
21
21
  import { type StorageAdapter, type StorageTable, type TransactionMode } from './storage/types';
22
- import type { StorageSchemaDefinitionOptions, SQLiteVersionConfigurator } from './storage/sqlite/types';
22
+ import type { StorageSchemaDefinitionOptions, SQLiteVersionMigration } from './storage/sqlite/types';
23
23
  import type { TableSchemaDefinition, SQLiteTableDefinition } from './storage/sqlite/schema';
24
24
  import { enhanceSyncTable, setupEnhancedTables as setupEnhancedTablesHelper, wrapWithMutationEmitter } from './core/tableEnhancers';
25
25
  import { pullAll as runPullAll, pullAllBatch as runPullAllBatch } from './core/pullOperations';
@@ -28,7 +28,7 @@ import { startFirstLoad as runFirstLoad, startFirstLoadBatch as runFirstLoadBatc
28
28
  import type { FirstLoadProgressCallback, VisibilitySubscription } from './types';
29
29
  import { StateManager, DYNC_STATE_TABLE, type StateHelpers } from './core/StateManager';
30
30
  import type { MemoryQueryContext } from './storage/memory/MemoryQueryContext';
31
- import type { SqliteQueryContext } from './storage/sqlite/SqliteQueryContext';
31
+ import type { SQLiteQueryContext } from './storage/sqlite/SQLiteQueryContext';
32
32
  import type { DexieQueryContext } from './storage/dexie/DexieQueryContext';
33
33
 
34
34
  const DEFAULT_SYNC_INTERVAL_MILLIS = 2000;
@@ -205,21 +205,12 @@ class DyncBase<_TStoreMap extends Record<string, any> = Record<string, any>> {
205
205
 
206
206
  return builder;
207
207
  },
208
- sqlite(configure: (builder: SQLiteVersionConfigurator) => void) {
208
+ sqlite(migrations: SQLiteVersionMigration) {
209
209
  if (!storesDefined) {
210
210
  throw new Error('Call stores() before registering sqlite migrations');
211
211
  }
212
212
  const sqliteOptions = (schemaOptions.sqlite ??= {});
213
- const migrations = (sqliteOptions.migrations ??= {});
214
- const configurator: SQLiteVersionConfigurator = {
215
- upgrade(handler) {
216
- migrations.upgrade = handler;
217
- },
218
- downgrade(handler) {
219
- migrations.downgrade = handler;
220
- },
221
- };
222
- configure(configurator);
213
+ sqliteOptions.migrations = migrations;
223
214
  return builder;
224
215
  },
225
216
  };
@@ -268,7 +259,7 @@ class DyncBase<_TStoreMap extends Record<string, any> = Record<string, any>> {
268
259
  this.syncEnhancedTables.clear();
269
260
  }
270
261
 
271
- async query<R>(callback: (ctx: DexieQueryContext | SqliteQueryContext | MemoryQueryContext) => Promise<R>): Promise<R> {
262
+ async query<R>(callback: (ctx: DexieQueryContext | SQLiteQueryContext | MemoryQueryContext) => Promise<R>): Promise<R> {
272
263
  return this.adapter.query(callback as any);
273
264
  }
274
265
 
package/src/index.ts CHANGED
@@ -2,7 +2,7 @@ export * from './index.shared';
2
2
  export { MemoryAdapter } from './storage/memory';
3
3
  export { SQLiteAdapter } from './storage/sqlite';
4
4
  export { MemoryQueryContext } from './storage/memory';
5
- export { SqliteQueryContext } from './storage/sqlite';
5
+ export { SQLiteQueryContext } from './storage/sqlite';
6
6
  export type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from './storage/sqlite/types';
7
7
  export type { StorageAdapter } from './storage/types';
8
8
  export { SyncAction } from './types';
package/src/node.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  // Node.js SQLite Driver using better-sqlite3
2
2
  // Works in Node.js apps and Electron main process
3
- export { BetterSqlite3Driver, type BetterSqlite3DriverOptions } from './storage/sqlite/drivers/BetterSqlite3Driver';
3
+ export { BetterSQLite3Driver, type BetterSQLite3DriverOptions } from './storage/sqlite/drivers/BetterSQLite3Driver';
4
4
  export type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from './storage/sqlite/types';
@@ -7,14 +7,14 @@ import type {
7
7
  SQLiteDefaultValue,
8
8
  SQLiteForeignKeyReference,
9
9
  } from './schema';
10
- import type { StorageSchemaDefinitionOptions, SQLiteMigrationContext, SQLiteMigrationDirection, SQLiteMigrationHandler } from './types';
10
+ import type { StorageSchemaDefinitionOptions, SQLiteMigrationContext, SQLiteMigrationHandler } from './types';
11
11
  import type { SQLiteDatabaseDriver, SQLiteRunResult, SQLiteQueryResult } from './types';
12
12
  import { LOCAL_PK } from '../../types';
13
13
  import type { SQLiteAdapterOptions, SQLiteTableSchemaMetadata, SQLiteColumnSchema } from './types';
14
14
  import { DYNC_STATE_TABLE } from '../../core/StateManager';
15
15
  import { SQLITE_SCHEMA_VERSION_STATE_KEY, quoteIdentifier } from './helpers';
16
16
  import { SQLiteTable } from './SQLiteTable';
17
- import { SqliteQueryContext } from './SqliteQueryContext';
17
+ import { SQLiteQueryContext } from './SQLiteQueryContext';
18
18
 
19
19
  export class SQLiteAdapter implements StorageAdapter {
20
20
  readonly type = 'SQLiteAdapter';
@@ -199,12 +199,12 @@ export class SQLiteAdapter implements StorageAdapter {
199
199
  return driver.run(statement, params);
200
200
  }
201
201
 
202
- async query<R>(callback: (ctx: SqliteQueryContext) => Promise<R>): Promise<R>;
202
+ async query<R>(callback: (ctx: SQLiteQueryContext) => Promise<R>): Promise<R>;
203
203
  async query(statement: string, values?: any[]): Promise<SQLiteQueryResult>;
204
- async query<R>(arg1: string | ((ctx: SqliteQueryContext) => Promise<R>), arg2?: any[]): Promise<R | SQLiteQueryResult> {
204
+ async query<R>(arg1: string | ((ctx: SQLiteQueryContext) => Promise<R>), arg2?: any[]): Promise<R | SQLiteQueryResult> {
205
205
  if (typeof arg1 === 'function') {
206
206
  const driver = await this.getDriver();
207
- return arg1(new SqliteQueryContext(driver, this));
207
+ return arg1(new SQLiteQueryContext(driver, this));
208
208
  }
209
209
  const statement = arg1;
210
210
  const values = arg2;
@@ -336,27 +336,24 @@ export class SQLiteAdapter implements StorageAdapter {
336
336
 
337
337
  if (currentVersion < targetVersion) {
338
338
  for (let version = currentVersion + 1; version <= targetVersion; version += 1) {
339
- await this.runMigrationStep(version, 'upgrade', version - 1, version);
339
+ await this.runMigrationStep(version, 'up');
340
340
  await this.setStoredSchemaVersion(version);
341
341
  }
342
342
  return;
343
343
  }
344
344
 
345
345
  for (let version = currentVersion; version > targetVersion; version -= 1) {
346
- await this.runMigrationStep(version, 'downgrade', version, version - 1);
346
+ await this.runMigrationStep(version, 'down');
347
347
  await this.setStoredSchemaVersion(version - 1);
348
348
  }
349
349
  }
350
350
 
351
- private async runMigrationStep(version: number, direction: SQLiteMigrationDirection, fromVersion: number, toVersion: number): Promise<void> {
351
+ private async runMigrationStep(version: number, direction: 'up' | 'down'): Promise<void> {
352
352
  const handler = this.getMigrationHandler(version, direction);
353
353
  if (!handler) {
354
354
  return;
355
355
  }
356
356
  const context: SQLiteMigrationContext = {
357
- direction,
358
- fromVersion,
359
- toVersion,
360
357
  execute: (statement: string) => this.internalExecute(statement),
361
358
  run: (statement: string, values?: any[]) => this.internalRun(statement, values),
362
359
  query: (statement: string, values?: any[]) => this.internalQuery(statement, values),
@@ -364,13 +361,13 @@ export class SQLiteAdapter implements StorageAdapter {
364
361
  await handler(context);
365
362
  }
366
363
 
367
- private getMigrationHandler(version: number, direction: SQLiteMigrationDirection): SQLiteMigrationHandler | undefined {
364
+ private getMigrationHandler(version: number, direction: 'up' | 'down'): SQLiteMigrationHandler | undefined {
368
365
  const options = this.versionOptions.get(version);
369
366
  const migrations = options?.sqlite?.migrations;
370
367
  if (!migrations) {
371
368
  return undefined;
372
369
  }
373
- return direction === 'upgrade' ? migrations.upgrade : migrations.downgrade;
370
+ return direction === 'up' ? migrations.up : migrations.down;
374
371
  }
375
372
 
376
373
  private async applySchema(): Promise<void> {
@@ -2,7 +2,7 @@ import type { StorageTable, StorageTransactionContext, TransactionMode } from '.
2
2
  import type { SQLiteDatabaseDriver, SQLiteRunResult } from './types';
3
3
  import { SQLiteAdapter } from './SQLiteAdapter';
4
4
 
5
- export class SqliteQueryContext {
5
+ export class SQLiteQueryContext {
6
6
  constructor(
7
7
  private readonly driver: SQLiteDatabaseDriver,
8
8
  private readonly adapter: SQLiteAdapter,
@@ -1,9 +1,9 @@
1
1
  import type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from '../types';
2
2
 
3
3
  /**
4
- * Options for configuring the BetterSqlite3Driver.
4
+ * Options for configuring the BetterSQLite3Driver.
5
5
  */
6
- export interface BetterSqlite3DriverOptions {
6
+ export interface BetterSQLite3DriverOptions {
7
7
  /**
8
8
  * Open the database in readonly mode.
9
9
  * @default false
@@ -42,21 +42,21 @@ export interface BetterSqlite3DriverOptions {
42
42
  *
43
43
  * @example
44
44
  * ```ts
45
- * import { BetterSqlite3Driver } from '@anfenn/dync/node';
45
+ * import { BetterSQLite3Driver } from '@anfenn/dync/node';
46
46
  * import { SQLiteAdapter } from '@anfenn/dync';
47
47
  *
48
- * const driver = new BetterSqlite3Driver('myapp.db', { wal: true });
48
+ * const driver = new BetterSQLite3Driver('myapp.db', { wal: true });
49
49
  * const adapter = new SQLiteAdapter('myapp', driver);
50
50
  * ```
51
51
  */
52
- export class BetterSqlite3Driver implements SQLiteDatabaseDriver {
53
- readonly type = 'BetterSqlite3Driver';
52
+ export class BetterSQLite3Driver implements SQLiteDatabaseDriver {
53
+ readonly type = 'BetterSQLite3Driver';
54
54
  private db: import('better-sqlite3').Database | null = null;
55
- private readonly options: BetterSqlite3DriverOptions;
55
+ private readonly options: BetterSQLite3DriverOptions;
56
56
  private opened = false;
57
57
  readonly name: string;
58
58
 
59
- constructor(databasePath: string, options: BetterSqlite3DriverOptions = {}) {
59
+ constructor(databasePath: string, options: BetterSQLite3DriverOptions = {}) {
60
60
  this.name = databasePath;
61
61
  this.options = {
62
62
  wal: true,
@@ -4,7 +4,7 @@ import type { SQLiteDatabaseDriver, SQLiteRunResult, SQLiteQueryResult } from '.
4
4
  // Lazily loaded module cache to avoid top-level imports that break web bundlers
5
5
  let sqliteModuleCache: typeof import('@capacitor-community/sqlite') | null = null;
6
6
 
7
- async function getSqliteModule(): Promise<typeof import('@capacitor-community/sqlite')> {
7
+ async function getSQLiteModule(): Promise<typeof import('@capacitor-community/sqlite')> {
8
8
  if (!sqliteModuleCache) {
9
9
  sqliteModuleCache = await import('@capacitor-community/sqlite');
10
10
  }
@@ -35,7 +35,7 @@ export class CapacitorSQLiteDriver implements SQLiteDatabaseDriver {
35
35
 
36
36
  private async getConnectionFactory(): Promise<SQLiteConnection> {
37
37
  if (!this.connectionFactory) {
38
- const { CapacitorSQLite, SQLiteConnection } = await getSqliteModule();
38
+ const { CapacitorSQLite, SQLiteConnection } = await getSQLiteModule();
39
39
  this.connectionFactory = new SQLiteConnection(CapacitorSQLite);
40
40
  }
41
41
  return this.connectionFactory;
@@ -6,7 +6,7 @@ import type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from '.
6
6
  *
7
7
  * @see https://github.com/rhashimoto/wa-sqlite/tree/master/src/examples#vfs-comparison
8
8
  */
9
- export type WaSqliteVfsType =
9
+ export type WaSQLiteVfsType =
10
10
  /**
11
11
  * IDBBatchAtomicVFS - IndexedDB-backed storage
12
12
  * - Works on ALL contexts (Window, Worker, SharedWorker, service worker)
@@ -45,14 +45,14 @@ export type WaSqliteVfsType =
45
45
  | 'AccessHandlePoolVFS';
46
46
 
47
47
  /**
48
- * Options for configuring the WaSqliteDriver.
48
+ * Options for configuring the WaSQLiteDriver.
49
49
  */
50
- export interface WaSqliteDriverOptions {
50
+ export interface WaSQLiteDriverOptions {
51
51
  /**
52
52
  * Virtual File System to use for storage.
53
53
  * @default 'IDBBatchAtomicVFS'
54
54
  */
55
- vfs?: WaSqliteVfsType;
55
+ vfs?: WaSQLiteVfsType;
56
56
 
57
57
  /**
58
58
  * Directory path for the database in OPFS VFS modes.
@@ -96,22 +96,22 @@ export interface WaSqliteDriverOptions {
96
96
  }
97
97
 
98
98
  // Internal VFS interface for lifecycle management
99
- interface WaSqliteVFS {
99
+ interface WaSQLiteVFS {
100
100
  close(): Promise<void>;
101
101
  name: string;
102
102
  }
103
103
 
104
104
  // VFS class type with static create method
105
105
  interface VFSClass {
106
- create(name: string, module: any, options?: any): Promise<WaSqliteVFS>;
106
+ create(name: string, module: any, options?: any): Promise<WaSQLiteVFS>;
107
107
  }
108
108
 
109
109
  // Cached module factory and instance
110
110
  let cachedModuleFactory: (() => Promise<any>) | null = null;
111
111
  let cachedModule: any = null;
112
- let cachedSqlite3: any = null;
112
+ let cachedSQLite3: any = null;
113
113
  // Track VFS instances by name to avoid re-registering
114
- const registeredVFS = new Map<string, WaSqliteVFS>();
114
+ const registeredVFS = new Map<string, WaSQLiteVFS>();
115
115
 
116
116
  /**
117
117
  * SQLite driver for web browsers using wa-sqlite with IndexedDB or OPFS persistence.
@@ -138,30 +138,30 @@ const registeredVFS = new Map<string, WaSqliteVFS>();
138
138
  *
139
139
  * @example
140
140
  * ```ts
141
- * import { WaSqliteDriver } from '@anfenn/dync/wa-sqlite';
141
+ * import { WaSQLiteDriver } from '@anfenn/dync/wa-sqlite';
142
142
  * import { SQLiteAdapter } from '@anfenn/dync';
143
143
  *
144
144
  * // Default: IDBBatchAtomicVFS (works in main thread, multi-tab safe)
145
- * const driver = new WaSqliteDriver('myapp.db');
145
+ * const driver = new WaSQLiteDriver('myapp.db');
146
146
  *
147
147
  * // For OPFS (faster, requires Worker, filesystem transparent)
148
- * const opfsDriver = new WaSqliteDriver('myapp.db', { vfs: 'OPFSCoopSyncVFS' });
148
+ * const opfsDriver = new WaSQLiteDriver('myapp.db', { vfs: 'OPFSCoopSyncVFS' });
149
149
  *
150
150
  * const adapter = new SQLiteAdapter('myapp', driver);
151
151
  * ```
152
152
  */
153
- export class WaSqliteDriver implements SQLiteDatabaseDriver {
154
- readonly type = 'WaSqliteDriver';
153
+ export class WaSQLiteDriver implements SQLiteDatabaseDriver {
154
+ readonly type = 'WaSQLiteDriver';
155
155
  private db: number | null = null;
156
156
  private sqlite3: any = null;
157
- private readonly options: Required<WaSqliteDriverOptions>;
157
+ private readonly options: Required<WaSQLiteDriverOptions>;
158
158
  private opened = false;
159
159
  private openPromise: Promise<void> | null = null;
160
160
  // Mutex to prevent concurrent database operations (critical for wa-sqlite)
161
161
  private executionLock: Promise<void> = Promise.resolve();
162
162
  readonly name: string;
163
163
 
164
- constructor(databaseName: string, options: WaSqliteDriverOptions = {}) {
164
+ constructor(databaseName: string, options: WaSQLiteDriverOptions = {}) {
165
165
  this.name = databaseName;
166
166
  this.options = {
167
167
  vfs: 'IDBBatchAtomicVFS',
@@ -215,11 +215,11 @@ export class WaSqliteDriver implements SQLiteDatabaseDriver {
215
215
  const module = await this.loadWasmModule();
216
216
 
217
217
  // Create SQLite API from module (cached - must only create once per module)
218
- if (!cachedSqlite3) {
218
+ if (!cachedSQLite3) {
219
219
  const { Factory } = await import('@journeyapps/wa-sqlite');
220
- cachedSqlite3 = Factory(module);
220
+ cachedSQLite3 = Factory(module);
221
221
  }
222
- this.sqlite3 = cachedSqlite3;
222
+ this.sqlite3 = cachedSQLite3;
223
223
 
224
224
  // For IDB-based VFS, the VFS name is also used as the IndexedDB database name
225
225
  // Use a unique name based on database name to avoid conflicts
@@ -259,7 +259,7 @@ export class WaSqliteDriver implements SQLiteDatabaseDriver {
259
259
  return cachedModule;
260
260
  }
261
261
 
262
- private async createVFS(module: any, vfsName: string): Promise<WaSqliteVFS> {
262
+ private async createVFS(module: any, vfsName: string): Promise<WaSQLiteVFS> {
263
263
  const vfsType = this.options.vfs;
264
264
  let VFSClass: VFSClass;
265
265
  let vfsOptions: any = undefined;
@@ -470,7 +470,7 @@ export class WaSqliteDriver implements SQLiteDatabaseDriver {
470
470
  /**
471
471
  * Get the VFS type being used by this driver.
472
472
  */
473
- getVfsType(): WaSqliteVfsType {
473
+ getVfsType(): WaSQLiteVfsType {
474
474
  return this.options.vfs;
475
475
  }
476
476
 
@@ -3,7 +3,7 @@ export * from './helpers';
3
3
  export * from './SQLiteCollection';
4
4
  export * from './SQLiteWhereClause';
5
5
  export * from './SQLiteTable';
6
- export * from './SqliteQueryContext';
6
+ export * from './SQLiteQueryContext';
7
7
  export * from './SQLiteAdapter';
8
8
 
9
9
  // Drivers