@anfenn/dync 1.0.19 → 1.0.21
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/dist/capacitor.cjs +2 -0
- package/dist/capacitor.cjs.map +1 -1
- package/dist/capacitor.js +1 -1
- package/dist/{chunk-SQB6E7V2.js → chunk-CGCYGESK.js} +3 -1
- package/dist/chunk-CGCYGESK.js.map +1 -0
- package/dist/{chunk-WZ7V4U6Z.js → chunk-I2KQD4DD.js} +35 -23
- package/dist/chunk-I2KQD4DD.js.map +1 -0
- package/dist/expoSqlite.cjs +1 -0
- package/dist/expoSqlite.cjs.map +1 -1
- package/dist/expoSqlite.js +1 -0
- package/dist/expoSqlite.js.map +1 -1
- package/dist/index.cjs +34 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +2 -2
- package/dist/node.cjs +1 -0
- package/dist/node.cjs.map +1 -1
- package/dist/node.js +1 -0
- package/dist/node.js.map +1 -1
- package/dist/react/index.js +2 -2
- package/dist/wa-sqlite.cjs +1 -0
- package/dist/wa-sqlite.cjs.map +1 -1
- package/dist/wa-sqlite.js +1 -0
- package/dist/wa-sqlite.js.map +1 -1
- package/package.json +1 -1
- package/src/storage/sqlite/SQLiteCollection.ts +19 -13
- package/src/storage/sqlite/drivers/BetterSqlite3Driver.ts +3 -0
- package/src/storage/sqlite/drivers/CapacitorFastSqlDriver.ts +2 -0
- package/src/storage/sqlite/drivers/CapacitorSQLiteDriver.ts +2 -0
- package/src/storage/sqlite/drivers/ExpoSQLiteDriver.native.ts +2 -0
- package/src/storage/sqlite/drivers/WaSqliteDriver.ts +3 -0
- package/src/storage/sqlite/helpers.ts +21 -9
- package/src/storage/sqlite/types.ts +5 -2
- package/dist/chunk-SQB6E7V2.js.map +0 -1
- package/dist/chunk-WZ7V4U6Z.js.map +0 -1
package/dist/index.js
CHANGED
package/dist/node.cjs
CHANGED
package/dist/node.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/node.ts","../src/storage/sqlite/drivers/BetterSqlite3Driver.ts"],"sourcesContent":["// Node.js SQLite Driver using better-sqlite3\n// Works in Node.js apps and Electron main process\nexport { BetterSqlite3Driver, type BetterSqlite3DriverOptions } from './storage/sqlite/drivers/BetterSqlite3Driver';\nexport type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from './storage/sqlite/types';\n","import type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from '../types';\n\n/**\n * Options for configuring the BetterSqlite3Driver.\n */\nexport interface BetterSqlite3DriverOptions {\n /**\n * Open the database in readonly mode.\n * @default false\n */\n readonly?: boolean;\n\n /**\n * Create the database file if it doesn't exist.\n * Set to false to throw an error if the file doesn't exist.\n * @default true\n */\n fileMustExist?: boolean;\n\n /**\n * Timeout in milliseconds when waiting for the database to become unlocked.\n * @default 5000\n */\n timeout?: number;\n\n /**\n * Enable verbose mode for debugging SQL statements.\n */\n verbose?: (message?: unknown, ...additionalArgs: unknown[]) => void;\n\n /**\n * Enable WAL (Write-Ahead Logging) mode for better concurrent access.\n * Recommended for most use cases.\n * @default true\n */\n wal?: boolean;\n}\n\n/**\n * SQLite driver for Node.js using better-sqlite3.\n * This driver is synchronous but wraps operations in Promises for API compatibility.\n *\n * @example\n * ```ts\n * import { BetterSqlite3Driver } from '@anfenn/dync/node';\n * import { SQLiteAdapter } from '@anfenn/dync';\n *\n * const driver = new BetterSqlite3Driver('myapp.db', { wal: true });\n * const adapter = new SQLiteAdapter('myapp', driver);\n * ```\n */\nexport class BetterSqlite3Driver implements SQLiteDatabaseDriver {\n readonly type = 'BetterSqlite3Driver';\n private db: import('better-sqlite3').Database | null = null;\n private readonly options: BetterSqlite3DriverOptions;\n private opened = false;\n readonly name: string;\n\n constructor(databasePath: string, options: BetterSqlite3DriverOptions = {}) {\n this.name = databasePath;\n this.options = {\n wal: true,\n ...options,\n };\n }\n\n async open(): Promise<void> {\n if (this.opened) return;\n\n // Dynamic import to avoid bundling issues in non-Electron environments\n const Database = (await import('better-sqlite3')).default;\n\n this.db = new Database(this.name, {\n readonly: this.options.readonly ?? false,\n fileMustExist: this.options.fileMustExist ?? false,\n timeout: this.options.timeout ?? 5000,\n verbose: this.options.verbose,\n });\n\n // Enable WAL mode for better concurrent access (recommended)\n if (this.options.wal && !this.options.readonly) {\n this.db.pragma('journal_mode = WAL');\n }\n\n this.opened = true;\n }\n\n async close(): Promise<void> {\n if (!this.db) return;\n\n this.db.close();\n this.db = null;\n this.opened = false;\n }\n\n async execute(statement: string): Promise<void> {\n await this.open();\n this.db!.exec(statement);\n }\n\n async run(statement: string, values: unknown[] = []): Promise<SQLiteRunResult> {\n await this.open();\n\n const stmt = this.db!.prepare(statement);\n const result = stmt.run(...values);\n\n return {\n changes: result.changes,\n lastId: result.lastInsertRowid !== undefined ? Number(result.lastInsertRowid) : undefined,\n };\n }\n\n async query(statement: string, values: unknown[] = []): Promise<SQLiteQueryResult> {\n await this.open();\n\n const stmt = this.db!.prepare(statement);\n const rows = stmt.all(...values) as Record<string, unknown>[];\n\n if (rows.length === 0) {\n return { columns: [], values: [] };\n }\n\n const firstRow = rows[0]!;\n const columns = Object.keys(firstRow);\n const valuesMatrix = rows.map((row) => columns.map((col) => row[col]));\n\n return { columns, values: valuesMatrix };\n }\n\n /**\n * Access the underlying better-sqlite3 Database instance for advanced operations.\n * Returns null if the database is not open.\n */\n getDatabase(): import('better-sqlite3').Database | null {\n return this.db;\n }\n\n /**\n * Execute a function within a transaction.\n * This provides better performance when doing many writes.\n *\n * @example\n * ```ts\n * await driver.transaction(() => {\n * driver.run('INSERT INTO users (name) VALUES (?)', ['Alice']);\n * driver.run('INSERT INTO users (name) VALUES (?)', ['Bob']);\n * });\n * ```\n */\n transaction<T>(fn: () => T): T {\n if (!this.db) {\n throw new Error('Database not open. Call open() first.');\n }\n return this.db.transaction(fn)();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACmDO,IAAM,sBAAN,MAA0D;AAAA,EACpD,OAAO;AAAA,EACR,KAA+C;AAAA,EACtC;AAAA,EACT,SAAS;AAAA,EACR;AAAA,EAET,YAAY,cAAsB,UAAsC,CAAC,GAAG;AACxE,SAAK,OAAO;AACZ,SAAK,UAAU;AAAA,MACX,KAAK;AAAA,MACL,GAAG;AAAA,IACP;AAAA,EACJ;AAAA,EAEA,MAAM,OAAsB;AACxB,QAAI,KAAK,OAAQ;AAGjB,UAAM,YAAY,MAAM,OAAO,gBAAgB,GAAG;AAElD,SAAK,KAAK,IAAI,SAAS,KAAK,MAAM;AAAA,MAC9B,UAAU,KAAK,QAAQ,YAAY;AAAA,MACnC,eAAe,KAAK,QAAQ,iBAAiB;AAAA,MAC7C,SAAS,KAAK,QAAQ,WAAW;AAAA,MACjC,SAAS,KAAK,QAAQ;AAAA,IAC1B,CAAC;AAGD,QAAI,KAAK,QAAQ,OAAO,CAAC,KAAK,QAAQ,UAAU;AAC5C,WAAK,GAAG,OAAO,oBAAoB;AAAA,IACvC;
|
|
1
|
+
{"version":3,"sources":["../src/node.ts","../src/storage/sqlite/drivers/BetterSqlite3Driver.ts"],"sourcesContent":["// Node.js SQLite Driver using better-sqlite3\n// Works in Node.js apps and Electron main process\nexport { BetterSqlite3Driver, type BetterSqlite3DriverOptions } from './storage/sqlite/drivers/BetterSqlite3Driver';\nexport type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from './storage/sqlite/types';\n","import type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from '../types';\n\n/**\n * Options for configuring the BetterSqlite3Driver.\n */\nexport interface BetterSqlite3DriverOptions {\n /**\n * Open the database in readonly mode.\n * @default false\n */\n readonly?: boolean;\n\n /**\n * Create the database file if it doesn't exist.\n * Set to false to throw an error if the file doesn't exist.\n * @default true\n */\n fileMustExist?: boolean;\n\n /**\n * Timeout in milliseconds when waiting for the database to become unlocked.\n * @default 5000\n */\n timeout?: number;\n\n /**\n * Enable verbose mode for debugging SQL statements.\n */\n verbose?: (message?: unknown, ...additionalArgs: unknown[]) => void;\n\n /**\n * Enable WAL (Write-Ahead Logging) mode for better concurrent access.\n * Recommended for most use cases.\n * @default true\n */\n wal?: boolean;\n}\n\n/**\n * SQLite driver for Node.js using better-sqlite3.\n * This driver is synchronous but wraps operations in Promises for API compatibility.\n *\n * @example\n * ```ts\n * import { BetterSqlite3Driver } from '@anfenn/dync/node';\n * import { SQLiteAdapter } from '@anfenn/dync';\n *\n * const driver = new BetterSqlite3Driver('myapp.db', { wal: true });\n * const adapter = new SQLiteAdapter('myapp', driver);\n * ```\n */\nexport class BetterSqlite3Driver implements SQLiteDatabaseDriver {\n readonly type = 'BetterSqlite3Driver';\n private db: import('better-sqlite3').Database | null = null;\n private readonly options: BetterSqlite3DriverOptions;\n private opened = false;\n readonly name: string;\n\n constructor(databasePath: string, options: BetterSqlite3DriverOptions = {}) {\n this.name = databasePath;\n this.options = {\n wal: true,\n ...options,\n };\n }\n\n async open(): Promise<void> {\n if (this.opened) return;\n\n // Dynamic import to avoid bundling issues in non-Electron environments\n const Database = (await import('better-sqlite3')).default;\n\n this.db = new Database(this.name, {\n readonly: this.options.readonly ?? false,\n fileMustExist: this.options.fileMustExist ?? false,\n timeout: this.options.timeout ?? 5000,\n verbose: this.options.verbose,\n });\n\n // Enable WAL mode for better concurrent access (recommended)\n if (this.options.wal && !this.options.readonly) {\n this.db.pragma('journal_mode = WAL');\n }\n\n // Case-sensitive LIKE to match Dexie's startsWith() behavior\n this.db.pragma('case_sensitive_like = ON');\n\n this.opened = true;\n }\n\n async close(): Promise<void> {\n if (!this.db) return;\n\n this.db.close();\n this.db = null;\n this.opened = false;\n }\n\n async execute(statement: string): Promise<void> {\n await this.open();\n this.db!.exec(statement);\n }\n\n async run(statement: string, values: unknown[] = []): Promise<SQLiteRunResult> {\n await this.open();\n\n const stmt = this.db!.prepare(statement);\n const result = stmt.run(...values);\n\n return {\n changes: result.changes,\n lastId: result.lastInsertRowid !== undefined ? Number(result.lastInsertRowid) : undefined,\n };\n }\n\n async query(statement: string, values: unknown[] = []): Promise<SQLiteQueryResult> {\n await this.open();\n\n const stmt = this.db!.prepare(statement);\n const rows = stmt.all(...values) as Record<string, unknown>[];\n\n if (rows.length === 0) {\n return { columns: [], values: [] };\n }\n\n const firstRow = rows[0]!;\n const columns = Object.keys(firstRow);\n const valuesMatrix = rows.map((row) => columns.map((col) => row[col]));\n\n return { columns, values: valuesMatrix };\n }\n\n /**\n * Access the underlying better-sqlite3 Database instance for advanced operations.\n * Returns null if the database is not open.\n */\n getDatabase(): import('better-sqlite3').Database | null {\n return this.db;\n }\n\n /**\n * Execute a function within a transaction.\n * This provides better performance when doing many writes.\n *\n * @example\n * ```ts\n * await driver.transaction(() => {\n * driver.run('INSERT INTO users (name) VALUES (?)', ['Alice']);\n * driver.run('INSERT INTO users (name) VALUES (?)', ['Bob']);\n * });\n * ```\n */\n transaction<T>(fn: () => T): T {\n if (!this.db) {\n throw new Error('Database not open. Call open() first.');\n }\n return this.db.transaction(fn)();\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACmDO,IAAM,sBAAN,MAA0D;AAAA,EACpD,OAAO;AAAA,EACR,KAA+C;AAAA,EACtC;AAAA,EACT,SAAS;AAAA,EACR;AAAA,EAET,YAAY,cAAsB,UAAsC,CAAC,GAAG;AACxE,SAAK,OAAO;AACZ,SAAK,UAAU;AAAA,MACX,KAAK;AAAA,MACL,GAAG;AAAA,IACP;AAAA,EACJ;AAAA,EAEA,MAAM,OAAsB;AACxB,QAAI,KAAK,OAAQ;AAGjB,UAAM,YAAY,MAAM,OAAO,gBAAgB,GAAG;AAElD,SAAK,KAAK,IAAI,SAAS,KAAK,MAAM;AAAA,MAC9B,UAAU,KAAK,QAAQ,YAAY;AAAA,MACnC,eAAe,KAAK,QAAQ,iBAAiB;AAAA,MAC7C,S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A;AAAA;AAAA;AAAA;AAAA,EAMA,cAAwD;AACpD,WAAO,KAAK;AAAA,EAChB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAcA,YAAe,IAAgB;AAC3B,QAAI,CAAC,KAAK,IAAI;AACV,YAAM,IAAI,MAAM,uCAAuC;AAAA,IAC3D;AACA,WAAO,KAAK,GAAG,YAAY,EAAE,EAAE;AAAA,EACnC;AACJ;","names":[]}
|
package/dist/node.js
CHANGED
package/dist/node.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/storage/sqlite/drivers/BetterSqlite3Driver.ts"],"sourcesContent":["import type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from '../types';\n\n/**\n * Options for configuring the BetterSqlite3Driver.\n */\nexport interface BetterSqlite3DriverOptions {\n /**\n * Open the database in readonly mode.\n * @default false\n */\n readonly?: boolean;\n\n /**\n * Create the database file if it doesn't exist.\n * Set to false to throw an error if the file doesn't exist.\n * @default true\n */\n fileMustExist?: boolean;\n\n /**\n * Timeout in milliseconds when waiting for the database to become unlocked.\n * @default 5000\n */\n timeout?: number;\n\n /**\n * Enable verbose mode for debugging SQL statements.\n */\n verbose?: (message?: unknown, ...additionalArgs: unknown[]) => void;\n\n /**\n * Enable WAL (Write-Ahead Logging) mode for better concurrent access.\n * Recommended for most use cases.\n * @default true\n */\n wal?: boolean;\n}\n\n/**\n * SQLite driver for Node.js using better-sqlite3.\n * This driver is synchronous but wraps operations in Promises for API compatibility.\n *\n * @example\n * ```ts\n * import { BetterSqlite3Driver } from '@anfenn/dync/node';\n * import { SQLiteAdapter } from '@anfenn/dync';\n *\n * const driver = new BetterSqlite3Driver('myapp.db', { wal: true });\n * const adapter = new SQLiteAdapter('myapp', driver);\n * ```\n */\nexport class BetterSqlite3Driver implements SQLiteDatabaseDriver {\n readonly type = 'BetterSqlite3Driver';\n private db: import('better-sqlite3').Database | null = null;\n private readonly options: BetterSqlite3DriverOptions;\n private opened = false;\n readonly name: string;\n\n constructor(databasePath: string, options: BetterSqlite3DriverOptions = {}) {\n this.name = databasePath;\n this.options = {\n wal: true,\n ...options,\n };\n }\n\n async open(): Promise<void> {\n if (this.opened) return;\n\n // Dynamic import to avoid bundling issues in non-Electron environments\n const Database = (await import('better-sqlite3')).default;\n\n this.db = new Database(this.name, {\n readonly: this.options.readonly ?? false,\n fileMustExist: this.options.fileMustExist ?? false,\n timeout: this.options.timeout ?? 5000,\n verbose: this.options.verbose,\n });\n\n // Enable WAL mode for better concurrent access (recommended)\n if (this.options.wal && !this.options.readonly) {\n this.db.pragma('journal_mode = WAL');\n }\n\n this.opened = true;\n }\n\n async close(): Promise<void> {\n if (!this.db) return;\n\n this.db.close();\n this.db = null;\n this.opened = false;\n }\n\n async execute(statement: string): Promise<void> {\n await this.open();\n this.db!.exec(statement);\n }\n\n async run(statement: string, values: unknown[] = []): Promise<SQLiteRunResult> {\n await this.open();\n\n const stmt = this.db!.prepare(statement);\n const result = stmt.run(...values);\n\n return {\n changes: result.changes,\n lastId: result.lastInsertRowid !== undefined ? Number(result.lastInsertRowid) : undefined,\n };\n }\n\n async query(statement: string, values: unknown[] = []): Promise<SQLiteQueryResult> {\n await this.open();\n\n const stmt = this.db!.prepare(statement);\n const rows = stmt.all(...values) as Record<string, unknown>[];\n\n if (rows.length === 0) {\n return { columns: [], values: [] };\n }\n\n const firstRow = rows[0]!;\n const columns = Object.keys(firstRow);\n const valuesMatrix = rows.map((row) => columns.map((col) => row[col]));\n\n return { columns, values: valuesMatrix };\n }\n\n /**\n * Access the underlying better-sqlite3 Database instance for advanced operations.\n * Returns null if the database is not open.\n */\n getDatabase(): import('better-sqlite3').Database | null {\n return this.db;\n }\n\n /**\n * Execute a function within a transaction.\n * This provides better performance when doing many writes.\n *\n * @example\n * ```ts\n * await driver.transaction(() => {\n * driver.run('INSERT INTO users (name) VALUES (?)', ['Alice']);\n * driver.run('INSERT INTO users (name) VALUES (?)', ['Bob']);\n * });\n * ```\n */\n transaction<T>(fn: () => T): T {\n if (!this.db) {\n throw new Error('Database not open. Call open() first.');\n }\n return this.db.transaction(fn)();\n }\n}\n"],"mappings":";AAmDO,IAAM,sBAAN,MAA0D;AAAA,EACpD,OAAO;AAAA,EACR,KAA+C;AAAA,EACtC;AAAA,EACT,SAAS;AAAA,EACR;AAAA,EAET,YAAY,cAAsB,UAAsC,CAAC,GAAG;AACxE,SAAK,OAAO;AACZ,SAAK,UAAU;AAAA,MACX,KAAK;AAAA,MACL,GAAG;AAAA,IACP;AAAA,EACJ;AAAA,EAEA,MAAM,OAAsB;AACxB,QAAI,KAAK,OAAQ;AAGjB,UAAM,YAAY,MAAM,OAAO,gBAAgB,GAAG;AAElD,SAAK,KAAK,IAAI,SAAS,KAAK,MAAM;AAAA,MAC9B,UAAU,KAAK,QAAQ,YAAY;AAAA,MACnC,eAAe,KAAK,QAAQ,iBAAiB;AAAA,MAC7C,SAAS,KAAK,QAAQ,WAAW;AAAA,MACjC,SAAS,KAAK,QAAQ;AAAA,IAC1B,CAAC;AAGD,QAAI,KAAK,QAAQ,OAAO,CAAC,KAAK,QAAQ,UAAU;AAC5C,WAAK,GAAG,OAAO,oBAAoB;AAAA,IACvC;
|
|
1
|
+
{"version":3,"sources":["../src/storage/sqlite/drivers/BetterSqlite3Driver.ts"],"sourcesContent":["import type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from '../types';\n\n/**\n * Options for configuring the BetterSqlite3Driver.\n */\nexport interface BetterSqlite3DriverOptions {\n /**\n * Open the database in readonly mode.\n * @default false\n */\n readonly?: boolean;\n\n /**\n * Create the database file if it doesn't exist.\n * Set to false to throw an error if the file doesn't exist.\n * @default true\n */\n fileMustExist?: boolean;\n\n /**\n * Timeout in milliseconds when waiting for the database to become unlocked.\n * @default 5000\n */\n timeout?: number;\n\n /**\n * Enable verbose mode for debugging SQL statements.\n */\n verbose?: (message?: unknown, ...additionalArgs: unknown[]) => void;\n\n /**\n * Enable WAL (Write-Ahead Logging) mode for better concurrent access.\n * Recommended for most use cases.\n * @default true\n */\n wal?: boolean;\n}\n\n/**\n * SQLite driver for Node.js using better-sqlite3.\n * This driver is synchronous but wraps operations in Promises for API compatibility.\n *\n * @example\n * ```ts\n * import { BetterSqlite3Driver } from '@anfenn/dync/node';\n * import { SQLiteAdapter } from '@anfenn/dync';\n *\n * const driver = new BetterSqlite3Driver('myapp.db', { wal: true });\n * const adapter = new SQLiteAdapter('myapp', driver);\n * ```\n */\nexport class BetterSqlite3Driver implements SQLiteDatabaseDriver {\n readonly type = 'BetterSqlite3Driver';\n private db: import('better-sqlite3').Database | null = null;\n private readonly options: BetterSqlite3DriverOptions;\n private opened = false;\n readonly name: string;\n\n constructor(databasePath: string, options: BetterSqlite3DriverOptions = {}) {\n this.name = databasePath;\n this.options = {\n wal: true,\n ...options,\n };\n }\n\n async open(): Promise<void> {\n if (this.opened) return;\n\n // Dynamic import to avoid bundling issues in non-Electron environments\n const Database = (await import('better-sqlite3')).default;\n\n this.db = new Database(this.name, {\n readonly: this.options.readonly ?? false,\n fileMustExist: this.options.fileMustExist ?? false,\n timeout: this.options.timeout ?? 5000,\n verbose: this.options.verbose,\n });\n\n // Enable WAL mode for better concurrent access (recommended)\n if (this.options.wal && !this.options.readonly) {\n this.db.pragma('journal_mode = WAL');\n }\n\n // Case-sensitive LIKE to match Dexie's startsWith() behavior\n this.db.pragma('case_sensitive_like = ON');\n\n this.opened = true;\n }\n\n async close(): Promise<void> {\n if (!this.db) return;\n\n this.db.close();\n this.db = null;\n this.opened = false;\n }\n\n async execute(statement: string): Promise<void> {\n await this.open();\n this.db!.exec(statement);\n }\n\n async run(statement: string, values: unknown[] = []): Promise<SQLiteRunResult> {\n await this.open();\n\n const stmt = this.db!.prepare(statement);\n const result = stmt.run(...values);\n\n return {\n changes: result.changes,\n lastId: result.lastInsertRowid !== undefined ? Number(result.lastInsertRowid) : undefined,\n };\n }\n\n async query(statement: string, values: unknown[] = []): Promise<SQLiteQueryResult> {\n await this.open();\n\n const stmt = this.db!.prepare(statement);\n const rows = stmt.all(...values) as Record<string, unknown>[];\n\n if (rows.length === 0) {\n return { columns: [], values: [] };\n }\n\n const firstRow = rows[0]!;\n const columns = Object.keys(firstRow);\n const valuesMatrix = rows.map((row) => columns.map((col) => row[col]));\n\n return { columns, values: valuesMatrix };\n }\n\n /**\n * Access the underlying better-sqlite3 Database instance for advanced operations.\n * Returns null if the database is not open.\n */\n getDatabase(): import('better-sqlite3').Database | null {\n return this.db;\n }\n\n /**\n * Execute a function within a transaction.\n * This provides better performance when doing many writes.\n *\n * @example\n * ```ts\n * await driver.transaction(() => {\n * driver.run('INSERT INTO users (name) VALUES (?)', ['Alice']);\n * driver.run('INSERT INTO users (name) VALUES (?)', ['Bob']);\n * });\n * ```\n */\n transaction<T>(fn: () => T): T {\n if (!this.db) {\n throw new Error('Database not open. Call open() first.');\n }\n return this.db.transaction(fn)();\n }\n}\n"],"mappings":";AAmDO,IAAM,sBAAN,MAA0D;AAAA,EACpD,OAAO;AAAA,EACR,KAA+C;AAAA,EACtC;AAAA,EACT,SAAS;AAAA,EACR;AAAA,EAET,YAAY,cAAsB,UAAsC,CAAC,GAAG;AACxE,SAAK,OAAO;AACZ,SAAK,UAAU;AAAA,MACX,KAAK;AAAA,MACL,GAAG;AAAA,IACP;AAAA,EACJ;AAAA,EAEA,MAAM,OAAsB;AACxB,QAAI,KAAK,OAAQ;AAGjB,UAAM,YAAY,MAAM,OAAO,gBAAgB,GAAG;AAElD,SAAK,KAAK,IAAI,SAAS,KAAK,MAAM;AAAA,MAC9B,UAAU,KAAK,QAAQ,YAAY;AAAA,MACnC,eAAe,KAAK,QAAQ,iBAAiB;AAAA,MAC7C,SAAS,KAAK,QAAQ,WAAW;AAAA,MACjC,SAAS,KAAK,QAAQ;AAAA,IAC1B,CAAC;AAGD,QAAI,KAAK,QAAQ,OAAO,CAAC,KAAK,QAAQ,UAAU;AAC5C,WA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;AAAA,EAcA,YAAe,IAAgB;AAC3B,QAAI,CAAC,KAAK,IAAI;AACV,YAAM,IAAI,MAAM,uCAAuC;AAAA,IAC3D;AACA,WAAO,KAAK,GAAG,YAAY,EAAE,EAAE;AAAA,EACnC;AACJ;","names":[]}
|
package/dist/react/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Dync
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
3
|
+
} from "../chunk-I2KQD4DD.js";
|
|
4
|
+
import "../chunk-CGCYGESK.js";
|
|
5
5
|
|
|
6
6
|
// src/react/useDync.ts
|
|
7
7
|
import { useCallback, useEffect, useRef, useState, useSyncExternalStore } from "react";
|
package/dist/wa-sqlite.cjs
CHANGED
|
@@ -163,6 +163,7 @@ var WaSqliteDriver = class {
|
|
|
163
163
|
} catch {
|
|
164
164
|
}
|
|
165
165
|
await this.sqlite3.exec(this.db, `PRAGMA cache_size = ${this.options.cacheSize}`);
|
|
166
|
+
await this.sqlite3.exec(this.db, "PRAGMA case_sensitive_like = ON");
|
|
166
167
|
if (this.options.wal && this.options.vfs === "AccessHandlePoolVFS") {
|
|
167
168
|
await this.sqlite3.exec(this.db, "PRAGMA locking_mode = exclusive");
|
|
168
169
|
await this.sqlite3.exec(this.db, "PRAGMA journal_mode = WAL");
|
package/dist/wa-sqlite.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/wa-sqlite.ts","../src/storage/sqlite/drivers/WaSqliteDriver.ts"],"sourcesContent":["// wa-sqlite Browser SQLite Driver\n// Import this entry point for browser/web builds with SQLite support\nexport { WaSqliteDriver, type WaSqliteDriverOptions, type WaSqliteVfsType } from './storage/sqlite/drivers/WaSqliteDriver';\nexport type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from './storage/sqlite/types';\n","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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;AC6GA,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,KAAK;AAElC,SAAK,cAAc,KAAK,MAAM;AAE9B,QAAI;AACA,YAAM,KAAK;AAAA,IACf,UAAE;AACE,WAA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AM,IAAI,QAAc,CAAC,SAAS,WAAW;AACzC,cAAM,UAAU,UAAU,eAAe,KAAK,IAAI;AAClD,gBAAQ,YAAY,MAAM,QAAQ;AAClC,gBAAQ,UAAU,MAAM,OAAO,QAAQ,KAAK;AAC5C,gBAAQ,YAAY,MAAM;AACtB,kBAAQ,KAAK,iCAAiC,KAAK,IAAI,wCAAwC;AAAA,QACnG;AAAA,MACJ,CAAC;AAAA,IACL,OAAO;AAEH,YAAM,SAAS,KAAK,kBAAkB;AACtC,YAAM,OAAO,MAAM,UAAU,QAAQ,aAAa;AAElD,UAAI;AAEA,cAAM,YAAY,OAAO,MAAM,GAAG,EAAE,OAAO,OAAO;AAClD,YAAI,UAAU;AAGd,iBAAS,IAAI,GAAG,IAAI,UAAU,SAAS,GAAG,KAAK;AAC3C,oBAAU,MAAM,QAAQ,mBAAmB,UAAU,CAAC,CAAE;AAAA,QAC5D;AAGA,cAAM,WAAW,UAAU,UAAU,SAAS,CAAC;AAC/C,cAAM,QAAQ,YAAY,UAAU,EAAE,WAAW,KAAK,CAAC;AAGvD,cAAM,kBAAkB,CAAC,GAAG,QAAQ,YAAY,GAAG,QAAQ,QAAQ,GAAG,QAAQ,MAAM;AACpF,mBAAW,QAAQ,iBAAiB;AAChC,cAAI;AACA,kBAAM,QAAQ,YAAY,MAAM,EAAE,WAAW,MAAM,CAAC;AAAA,UACxD,QAAQ;AAAA,UAER;AAAA,QACJ;AAAA,MACJ,SAAS,OAAO;AAEZ,YAAK,MAAgB,SAAS,iBAAiB;AAC3C,gBAAM;AAAA,QACV;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ;AACJ;","names":["module"]}
|
|
1
|
+
{"version":3,"sources":["../src/wa-sqlite.ts","../src/storage/sqlite/drivers/WaSqliteDriver.ts"],"sourcesContent":["// wa-sqlite Browser SQLite Driver\n// Import this entry point for browser/web builds with SQLite support\nexport { WaSqliteDriver, type WaSqliteDriverOptions, type WaSqliteVfsType } from './storage/sqlite/drivers/WaSqliteDriver';\nexport type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from './storage/sqlite/types';\n","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 // Case-sensitive LIKE to match Dexie's startsWith() behavior\n await this.sqlite3.exec(this.db, 'PRAGMA case_sensitive_like = ON');\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;AC6GA,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,IACp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AA,QACV;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ;AACJ;","names":["module"]}
|
package/dist/wa-sqlite.js
CHANGED
|
@@ -127,6 +127,7 @@ var WaSqliteDriver = class {
|
|
|
127
127
|
} catch {
|
|
128
128
|
}
|
|
129
129
|
await this.sqlite3.exec(this.db, `PRAGMA cache_size = ${this.options.cacheSize}`);
|
|
130
|
+
await this.sqlite3.exec(this.db, "PRAGMA case_sensitive_like = ON");
|
|
130
131
|
if (this.options.wal && this.options.vfs === "AccessHandlePoolVFS") {
|
|
131
132
|
await this.sqlite3.exec(this.db, "PRAGMA locking_mode = exclusive");
|
|
132
133
|
await this.sqlite3.exec(this.db, "PRAGMA journal_mode = WAL");
|
package/dist/wa-sqlite.js.map
CHANGED
|
@@ -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,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B,CAAC,GAAG,QAAQ,YAAY,GAAG,QAAQ,QAAQ,GAAG,QAAQ,MAAM;AACpF,mBAAW,QAAQ,iBAAiB;AAChC,cAAI;AACA,kBAAM,QAAQ,YAAY,MAAM,EAAE,WAAW,MAAM,CAAC;AAAA,UACxD,QAAQ;AAAA,UAER;AAAA,QACJ;AAAA,MACJ,SAAS,OAAO;AAEZ,YAAK,MAAgB,SAAS,iBAAiB;AAC3C,gBAAM;AAAA,QACV;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ;AACJ;","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 // Case-sensitive LIKE to match Dexie's startsWith() behavior\n await this.sqlite3.exec(this.db, 'PRAGMA case_sensitive_like = ON');\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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,QAAQ,YAAY,GAAG,QAAQ,QAAQ,GAAG,QAAQ,MAAM;AACpF,mBAAW,QAAQ,iBAAiB;AAChC,cAAI;AACA,kBAAM,QAAQ,YAAY,MAAM,EAAE,WAAW,MAAM,CAAC;AAAA,UACxD,QAAQ;AAAA,UAER;AAAA,QACJ;AAAA,MACJ,SAAS,OAAO;AAEZ,YAAK,MAAgB,SAAS,iBAAiB;AAC3C,gBAAM;AAAA,QACV;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ;AACJ;","names":[]}
|
package/package.json
CHANGED
|
@@ -14,20 +14,21 @@ export class SQLiteCollection<T = any> implements StorageCollection<T> {
|
|
|
14
14
|
this.state = {
|
|
15
15
|
...base,
|
|
16
16
|
...state,
|
|
17
|
-
|
|
17
|
+
orGroups: state?.orGroups ?? base.orGroups,
|
|
18
18
|
jsPredicate: state?.jsPredicate,
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
getState(): SQLiteCollectionState<T> {
|
|
23
|
-
return { ...this.state,
|
|
23
|
+
return { ...this.state, orGroups: this.state.orGroups.map((g) => [...g]) };
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
// Add a SQL-expressible condition to
|
|
26
|
+
// Add a SQL-expressible condition to the current OR group
|
|
27
27
|
addSqlCondition(condition: SQLiteCondition): SQLiteCollection<T> {
|
|
28
|
+
const newGroups = this.state.orGroups.map((g, i) => (i === this.state.orGroups.length - 1 ? [...g, condition] : g));
|
|
28
29
|
return new SQLiteCollection(this.table, {
|
|
29
30
|
...this.state,
|
|
30
|
-
|
|
31
|
+
orGroups: newGroups,
|
|
31
32
|
});
|
|
32
33
|
}
|
|
33
34
|
|
|
@@ -35,7 +36,7 @@ export class SQLiteCollection<T = any> implements StorageCollection<T> {
|
|
|
35
36
|
return new SQLiteCollection(this.table, {
|
|
36
37
|
...this.state,
|
|
37
38
|
...overrides,
|
|
38
|
-
|
|
39
|
+
orGroups: overrides?.orGroups ?? this.state.orGroups,
|
|
39
40
|
jsPredicate: overrides?.jsPredicate !== undefined ? overrides.jsPredicate : this.state.jsPredicate,
|
|
40
41
|
});
|
|
41
42
|
}
|
|
@@ -73,7 +74,7 @@ export class SQLiteCollection<T = any> implements StorageCollection<T> {
|
|
|
73
74
|
orderByOverride?: { index: string | string[]; direction: 'asc' | 'desc' };
|
|
74
75
|
} = {},
|
|
75
76
|
): Promise<TableEntry<T>[]> {
|
|
76
|
-
const { whereClause, parameters } = buildWhereClause(this.state.
|
|
77
|
+
const { whereClause, parameters } = buildWhereClause(this.state.orGroups);
|
|
77
78
|
const ordering = options.orderByOverride ?? this.resolveOrdering();
|
|
78
79
|
const cloneValues = options.clone !== false;
|
|
79
80
|
const hasJsFilter = this.hasJsPredicate();
|
|
@@ -160,7 +161,7 @@ export class SQLiteCollection<T = any> implements StorageCollection<T> {
|
|
|
160
161
|
async keys(): Promise<unknown[]> {
|
|
161
162
|
// Optimization: use native SQL when no JS filtering needed
|
|
162
163
|
if (!this.hasJsPredicate()) {
|
|
163
|
-
const { whereClause, parameters } = buildWhereClause(this.state.
|
|
164
|
+
const { whereClause, parameters } = buildWhereClause(this.state.orGroups);
|
|
164
165
|
const ordering = this.resolveOrdering();
|
|
165
166
|
return this.table.queryKeysWithConditions({
|
|
166
167
|
whereClause,
|
|
@@ -183,7 +184,7 @@ export class SQLiteCollection<T = any> implements StorageCollection<T> {
|
|
|
183
184
|
async uniqueKeys(): Promise<unknown[]> {
|
|
184
185
|
// Optimization: use native SQL DISTINCT when no JS filtering needed
|
|
185
186
|
if (!this.hasJsPredicate()) {
|
|
186
|
-
const { whereClause, parameters } = buildWhereClause(this.state.
|
|
187
|
+
const { whereClause, parameters } = buildWhereClause(this.state.orGroups);
|
|
187
188
|
const ordering = this.resolveOrdering();
|
|
188
189
|
return this.table.queryKeysWithConditions({
|
|
189
190
|
whereClause,
|
|
@@ -203,8 +204,8 @@ export class SQLiteCollection<T = any> implements StorageCollection<T> {
|
|
|
203
204
|
// Optimization: use SQL COUNT when no JS filtering needed
|
|
204
205
|
if (!this.hasJsPredicate()) {
|
|
205
206
|
return this.table.countWithConditions({
|
|
206
|
-
whereClause: buildWhereClause(this.state.
|
|
207
|
-
parameters: buildWhereClause(this.state.
|
|
207
|
+
whereClause: buildWhereClause(this.state.orGroups).whereClause,
|
|
208
|
+
parameters: buildWhereClause(this.state.orGroups).parameters,
|
|
208
209
|
distinct: this.state.distinct,
|
|
209
210
|
});
|
|
210
211
|
}
|
|
@@ -229,7 +230,12 @@ export class SQLiteCollection<T = any> implements StorageCollection<T> {
|
|
|
229
230
|
}
|
|
230
231
|
|
|
231
232
|
or(index: string): StorageWhereClause<T> {
|
|
232
|
-
|
|
233
|
+
// Start a new OR group - subsequent conditions will be ORed with previous groups
|
|
234
|
+
const newCollection = new SQLiteCollection(this.table, {
|
|
235
|
+
...this.state,
|
|
236
|
+
orGroups: [...this.state.orGroups, []],
|
|
237
|
+
});
|
|
238
|
+
return this.table.createWhereClause(index, newCollection);
|
|
233
239
|
}
|
|
234
240
|
|
|
235
241
|
clone(_props?: Record<string, unknown>): StorageCollection<T> {
|
|
@@ -255,7 +261,7 @@ export class SQLiteCollection<T = any> implements StorageCollection<T> {
|
|
|
255
261
|
async delete(): Promise<number> {
|
|
256
262
|
// Optimization: use native SQL DELETE when no JS filtering needed
|
|
257
263
|
if (!this.hasJsPredicate()) {
|
|
258
|
-
const { whereClause, parameters } = buildWhereClause(this.state.
|
|
264
|
+
const { whereClause, parameters } = buildWhereClause(this.state.orGroups);
|
|
259
265
|
return this.table.deleteWithConditions({ whereClause, parameters });
|
|
260
266
|
}
|
|
261
267
|
// Fallback for JS filtering - must iterate and delete one by one
|
|
@@ -269,7 +275,7 @@ export class SQLiteCollection<T = any> implements StorageCollection<T> {
|
|
|
269
275
|
async modify(changes: Partial<T> | ((item: T) => void | Promise<void>)): Promise<number> {
|
|
270
276
|
// Optimization: use native SQL UPDATE when changes is an object and no JS filtering
|
|
271
277
|
if (typeof changes !== 'function' && !this.hasJsPredicate()) {
|
|
272
|
-
const { whereClause, parameters } = buildWhereClause(this.state.
|
|
278
|
+
const { whereClause, parameters } = buildWhereClause(this.state.orGroups);
|
|
273
279
|
return this.table.updateWithConditions({ whereClause, parameters, changes });
|
|
274
280
|
}
|
|
275
281
|
// Fallback for function-based changes or JS filtering
|
|
@@ -82,6 +82,9 @@ export class BetterSqlite3Driver implements SQLiteDatabaseDriver {
|
|
|
82
82
|
this.db.pragma('journal_mode = WAL');
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
+
// Case-sensitive LIKE to match Dexie's startsWith() behavior
|
|
86
|
+
this.db.pragma('case_sensitive_like = ON');
|
|
87
|
+
|
|
85
88
|
this.opened = true;
|
|
86
89
|
}
|
|
87
90
|
|
|
@@ -62,6 +62,8 @@ export class CapacitorFastSqlDriver implements SQLiteDatabaseDriver {
|
|
|
62
62
|
encryptionKey,
|
|
63
63
|
readOnly: this.options.readonly,
|
|
64
64
|
});
|
|
65
|
+
// Case-sensitive LIKE to match Dexie's startsWith() behavior
|
|
66
|
+
await this.db.execute('PRAGMA case_sensitive_like = ON');
|
|
65
67
|
this.opened = true;
|
|
66
68
|
}
|
|
67
69
|
})();
|
|
@@ -88,6 +88,8 @@ export class CapacitorSQLiteDriver implements SQLiteDatabaseDriver {
|
|
|
88
88
|
}
|
|
89
89
|
const db = await this.ensureDb();
|
|
90
90
|
await db.open();
|
|
91
|
+
// Case-sensitive LIKE to match Dexie's startsWith() behavior
|
|
92
|
+
await db.execute('PRAGMA case_sensitive_like = ON');
|
|
91
93
|
this.opened = true;
|
|
92
94
|
})();
|
|
93
95
|
|
|
@@ -20,6 +20,8 @@ export class ExpoSQLiteDriver implements SQLiteDatabaseDriver {
|
|
|
20
20
|
this.openPromise = (async () => {
|
|
21
21
|
if (!this.db) {
|
|
22
22
|
this.db = await SQLite.openDatabaseAsync(this.name);
|
|
23
|
+
// Case-sensitive LIKE to match Dexie's startsWith() behavior
|
|
24
|
+
await this.db.execAsync('PRAGMA case_sensitive_like = ON');
|
|
23
25
|
this.opened = true;
|
|
24
26
|
}
|
|
25
27
|
})();
|
|
@@ -324,6 +324,9 @@ export class WaSqliteDriver implements SQLiteDatabaseDriver {
|
|
|
324
324
|
// Cache size for performance
|
|
325
325
|
await this.sqlite3.exec(this.db, `PRAGMA cache_size = ${this.options.cacheSize}`);
|
|
326
326
|
|
|
327
|
+
// Case-sensitive LIKE to match Dexie's startsWith() behavior
|
|
328
|
+
await this.sqlite3.exec(this.db, 'PRAGMA case_sensitive_like = ON');
|
|
329
|
+
|
|
327
330
|
// WAL mode only for AccessHandlePoolVFS with exclusive locking
|
|
328
331
|
if (this.options.wal && this.options.vfs === 'AccessHandlePoolVFS') {
|
|
329
332
|
await this.sqlite3.exec(this.db, 'PRAGMA locking_mode = exclusive');
|