@anfenn/dync 1.0.28 → 1.0.31
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/README.md +31 -9
- package/dist/capacitor.cjs +2 -2
- package/dist/capacitor.cjs.map +1 -1
- package/dist/capacitor.d.cts +1 -1
- package/dist/capacitor.d.ts +1 -1
- package/dist/capacitor.js +1 -1
- package/dist/{chunk-SQB6E7V2.js → chunk-XAHQXK76.js} +3 -3
- package/dist/{chunk-SQB6E7V2.js.map → chunk-XAHQXK76.js.map} +1 -1
- package/dist/{dexie-BFPA0JU2.d.ts → dexie-ImxmapkS.d.ts} +5 -5
- package/dist/{dexie-T9m1mP1h.d.cts → dexie-q17SmbBw.d.cts} +5 -5
- package/dist/dexie.d.cts +2 -2
- package/dist/dexie.d.ts +2 -2
- package/dist/expoSqlite.d.cts +1 -1
- package/dist/expoSqlite.d.ts +1 -1
- package/dist/index.cjs +54 -27
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -8
- package/dist/index.d.ts +9 -8
- package/dist/index.js +54 -27
- package/dist/index.js.map +1 -1
- package/dist/node.cjs +5 -5
- package/dist/node.cjs.map +1 -1
- package/dist/node.d.cts +9 -9
- package/dist/node.d.ts +9 -9
- package/dist/node.js +4 -4
- package/dist/node.js.map +1 -1
- package/dist/react/index.d.cts +3 -3
- package/dist/react/index.d.ts +3 -3
- package/dist/{types-DLFva7gq.d.ts → types-B7xq90Kc.d.cts} +2 -2
- package/dist/{types-9I2fmDbU.d.cts → types-CfvYBKI_.d.ts} +2 -2
- package/dist/{types-CSbIAfu2.d.cts → types-DeiRXWKl.d.cts} +4 -8
- package/dist/{types-CSbIAfu2.d.ts → types-DeiRXWKl.d.ts} +4 -8
- package/dist/wa-sqlite.cjs +9 -9
- package/dist/wa-sqlite.cjs.map +1 -1
- package/dist/wa-sqlite.d.cts +13 -13
- package/dist/wa-sqlite.d.ts +13 -13
- package/dist/wa-sqlite.js +8 -8
- package/dist/wa-sqlite.js.map +1 -1
- package/package.json +6 -4
- package/src/core/tableEnhancers.ts +15 -5
- package/src/index.native.ts +1 -1
- package/src/index.shared.ts +42 -14
- package/src/index.ts +1 -1
- package/src/node.ts +1 -1
- package/src/storage/sqlite/SQLiteAdapter.ts +10 -13
- package/src/storage/sqlite/{SqliteQueryContext.ts → SQLiteQueryContext.ts} +1 -1
- package/src/storage/sqlite/drivers/{BetterSqlite3Driver.ts → BetterSQLite3Driver.ts} +8 -8
- package/src/storage/sqlite/drivers/CapacitorSQLiteDriver.ts +2 -2
- package/src/storage/sqlite/drivers/{WaSqliteDriver.ts → WaSQLiteDriver.ts} +20 -20
- package/src/storage/sqlite/index.ts +1 -1
- package/src/storage/sqlite/types.ts +4 -9
- package/src/storage/types.ts +2 -2
- package/src/types.ts +1 -1
- package/src/wa-sqlite.ts +1 -1
package/dist/wa-sqlite.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { S as SQLiteDatabaseDriver, a as SQLiteRunResult, b as SQLiteQueryResult } from './types-
|
|
1
|
+
import { S as SQLiteDatabaseDriver, a as SQLiteRunResult, b as SQLiteQueryResult } from './types-DeiRXWKl.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Virtual File System (VFS) options for wa-sqlite.
|
|
@@ -6,7 +6,7 @@ import { S as SQLiteDatabaseDriver, a as SQLiteRunResult, b as SQLiteQueryResult
|
|
|
6
6
|
*
|
|
7
7
|
* @see https://github.com/rhashimoto/wa-sqlite/tree/master/src/examples#vfs-comparison
|
|
8
8
|
*/
|
|
9
|
-
type
|
|
9
|
+
type WaSQLiteVfsType =
|
|
10
10
|
/**
|
|
11
11
|
* IDBBatchAtomicVFS - IndexedDB-backed storage
|
|
12
12
|
* - Works on ALL contexts (Window, Worker, SharedWorker, service worker)
|
|
@@ -44,14 +44,14 @@ type WaSqliteVfsType =
|
|
|
44
44
|
*/
|
|
45
45
|
| 'AccessHandlePoolVFS';
|
|
46
46
|
/**
|
|
47
|
-
* Options for configuring the
|
|
47
|
+
* Options for configuring the WaSQLiteDriver.
|
|
48
48
|
*/
|
|
49
|
-
interface
|
|
49
|
+
interface WaSQLiteDriverOptions {
|
|
50
50
|
/**
|
|
51
51
|
* Virtual File System to use for storage.
|
|
52
52
|
* @default 'IDBBatchAtomicVFS'
|
|
53
53
|
*/
|
|
54
|
-
vfs?:
|
|
54
|
+
vfs?: WaSQLiteVfsType;
|
|
55
55
|
/**
|
|
56
56
|
* Directory path for the database in OPFS VFS modes.
|
|
57
57
|
* Only used with OPFS-based VFS types.
|
|
@@ -113,20 +113,20 @@ interface WaSqliteDriverOptions {
|
|
|
113
113
|
*
|
|
114
114
|
* @example
|
|
115
115
|
* ```ts
|
|
116
|
-
* import {
|
|
116
|
+
* import { WaSQLiteDriver } from '@anfenn/dync/wa-sqlite';
|
|
117
117
|
* import { SQLiteAdapter } from '@anfenn/dync';
|
|
118
118
|
*
|
|
119
119
|
* // Default: IDBBatchAtomicVFS (works in main thread, multi-tab safe)
|
|
120
|
-
* const driver = new
|
|
120
|
+
* const driver = new WaSQLiteDriver('myapp.db');
|
|
121
121
|
*
|
|
122
122
|
* // For OPFS (faster, requires Worker, filesystem transparent)
|
|
123
|
-
* const opfsDriver = new
|
|
123
|
+
* const opfsDriver = new WaSQLiteDriver('myapp.db', { vfs: 'OPFSCoopSyncVFS' });
|
|
124
124
|
*
|
|
125
125
|
* const adapter = new SQLiteAdapter('myapp', driver);
|
|
126
126
|
* ```
|
|
127
127
|
*/
|
|
128
|
-
declare class
|
|
129
|
-
readonly type = "
|
|
128
|
+
declare class WaSQLiteDriver implements SQLiteDatabaseDriver {
|
|
129
|
+
readonly type = "WaSQLiteDriver";
|
|
130
130
|
private db;
|
|
131
131
|
private sqlite3;
|
|
132
132
|
private readonly options;
|
|
@@ -134,7 +134,7 @@ declare class WaSqliteDriver implements SQLiteDatabaseDriver {
|
|
|
134
134
|
private openPromise;
|
|
135
135
|
private executionLock;
|
|
136
136
|
readonly name: string;
|
|
137
|
-
constructor(databaseName: string, options?:
|
|
137
|
+
constructor(databaseName: string, options?: WaSQLiteDriverOptions);
|
|
138
138
|
/**
|
|
139
139
|
* Execute a callback with exclusive database access.
|
|
140
140
|
* This prevents concurrent operations which can corrupt the database.
|
|
@@ -162,7 +162,7 @@ declare class WaSqliteDriver implements SQLiteDatabaseDriver {
|
|
|
162
162
|
/**
|
|
163
163
|
* Get the VFS type being used by this driver.
|
|
164
164
|
*/
|
|
165
|
-
getVfsType():
|
|
165
|
+
getVfsType(): WaSQLiteVfsType;
|
|
166
166
|
/**
|
|
167
167
|
* Delete the database.
|
|
168
168
|
* This will close the database if open and remove all persisted data.
|
|
@@ -172,4 +172,4 @@ declare class WaSqliteDriver implements SQLiteDatabaseDriver {
|
|
|
172
172
|
delete(): Promise<void>;
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
-
export { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult,
|
|
175
|
+
export { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult, WaSQLiteDriver, type WaSQLiteDriverOptions, type WaSQLiteVfsType };
|
package/dist/wa-sqlite.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
// src/storage/sqlite/drivers/
|
|
1
|
+
// src/storage/sqlite/drivers/WaSQLiteDriver.ts
|
|
2
2
|
var cachedModuleFactory = null;
|
|
3
3
|
var cachedModule = null;
|
|
4
|
-
var
|
|
4
|
+
var cachedSQLite3 = null;
|
|
5
5
|
var registeredVFS = /* @__PURE__ */ new Map();
|
|
6
|
-
var
|
|
7
|
-
type = "
|
|
6
|
+
var WaSQLiteDriver = class {
|
|
7
|
+
type = "WaSQLiteDriver";
|
|
8
8
|
db = null;
|
|
9
9
|
sqlite3 = null;
|
|
10
10
|
options;
|
|
@@ -54,11 +54,11 @@ var WaSqliteDriver = class {
|
|
|
54
54
|
}
|
|
55
55
|
async _open() {
|
|
56
56
|
const module = await this.loadWasmModule();
|
|
57
|
-
if (!
|
|
57
|
+
if (!cachedSQLite3) {
|
|
58
58
|
const { Factory } = await import("@journeyapps/wa-sqlite");
|
|
59
|
-
|
|
59
|
+
cachedSQLite3 = Factory(module);
|
|
60
60
|
}
|
|
61
|
-
this.sqlite3 =
|
|
61
|
+
this.sqlite3 = cachedSQLite3;
|
|
62
62
|
const vfsName = `dync_${this.options.vfs}_${this.name}`.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
63
63
|
let existingVfs = registeredVFS.get(vfsName);
|
|
64
64
|
if (!existingVfs) {
|
|
@@ -276,6 +276,6 @@ var WaSqliteDriver = class {
|
|
|
276
276
|
}
|
|
277
277
|
};
|
|
278
278
|
export {
|
|
279
|
-
|
|
279
|
+
WaSQLiteDriver
|
|
280
280
|
};
|
|
281
281
|
//# sourceMappingURL=wa-sqlite.js.map
|
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,C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AC/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":[]}
|
|
1
|
+
{"version":3,"sources":["../src/storage/sqlite/drivers/WaSQLiteDriver.ts"],"sourcesContent":["import type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from '../types';\n\n/**\n * Virtual File System (VFS) options for wa-sqlite.\n * Each VFS has different trade-offs for performance, durability, and compatibility.\n *\n * @see https://github.com/rhashimoto/wa-sqlite/tree/master/src/examples#vfs-comparison\n */\nexport type WaSQLiteVfsType =\n /**\n * IDBBatchAtomicVFS - IndexedDB-backed storage\n * - Works on ALL contexts (Window, Worker, SharedWorker, service worker)\n * - Supports multiple connections\n * - Full durability with batch atomic writes\n * - Good general-purpose choice for maximum compatibility\n * @recommended For apps that need to work in main thread and don't need OPFS\n */\n | 'IDBBatchAtomicVFS'\n /**\n * IDBMirrorVFS - IndexedDB with in-memory mirror\n * - Works on ALL contexts\n * - Supports multiple connections\n * - Much faster than IDBBatchAtomicVFS\n * - Database must fit in available memory\n * @recommended For small databases where performance is critical\n */\n | 'IDBMirrorVFS'\n /**\n * OPFSCoopSyncVFS - OPFS with cooperative synchronous access\n * - Requires Worker context\n * - Supports multiple connections\n * - Filesystem transparent (can import/export files)\n * - Good balance of performance and compatibility\n * @recommended For apps needing OPFS with multi-connection support\n */\n | 'OPFSCoopSyncVFS'\n /**\n * AccessHandlePoolVFS - OPFS-backed storage (fastest single connection)\n * - Requires Worker context\n * - Single connection only (no multi-tab support)\n * - Best performance, supports WAL mode\n * - NOT filesystem transparent\n * @recommended For single-tab apps where performance is critical\n */\n | 'AccessHandlePoolVFS';\n\n/**\n * Options for configuring the WaSQLiteDriver.\n */\nexport interface WaSQLiteDriverOptions {\n /**\n * Virtual File System to use for storage.\n * @default 'IDBBatchAtomicVFS'\n */\n vfs?: WaSQLiteVfsType;\n\n /**\n * Directory path for the database in OPFS VFS modes.\n * Only used with OPFS-based VFS types.\n * @default '/'\n */\n directory?: string;\n\n /**\n * SQLite page size in bytes.\n * Larger pages can improve read performance for large BLOBs.\n * Cannot be changed after database creation for IDBBatchAtomicVFS/IDBMirrorVFS.\n * @default 4096\n */\n pageSize?: number;\n\n /**\n * SQLite cache size in pages (negative = KB, positive = pages).\n * Larger cache improves performance but uses more memory.\n * For IDBBatchAtomicVFS, must be large enough to hold journal for batch atomic mode.\n * @default -2000 (2MB)\n */\n cacheSize?: number;\n\n /**\n * Enable WAL (Write-Ahead Logging) mode.\n * Only supported with AccessHandlePoolVFS (with locking_mode=exclusive).\n * For other VFS types, this is ignored.\n * @default false\n */\n wal?: boolean;\n\n /**\n * Set synchronous pragma for durability vs performance trade-off.\n * - 'full': Maximum durability (default)\n * - 'normal': Relaxed durability, better performance (supported by IDBBatchAtomicVFS, IDBMirrorVFS, OPFSPermutedVFS)\n * - 'off': No sync, fastest but risks data loss on crash\n * @default 'full'\n */\n synchronous?: 'full' | 'normal' | 'off';\n}\n\n// Internal VFS interface for lifecycle management\ninterface WaSQLiteVFS {\n close(): Promise<void>;\n name: string;\n}\n\n// VFS class type with static create method\ninterface VFSClass {\n create(name: string, module: any, options?: any): Promise<WaSQLiteVFS>;\n}\n\n// Cached module factory and instance\nlet cachedModuleFactory: (() => Promise<any>) | null = null;\nlet cachedModule: any = null;\nlet cachedSQLite3: any = null;\n// Track VFS instances by name to avoid re-registering\nconst registeredVFS = new Map<string, WaSQLiteVFS>();\n\n/**\n * SQLite driver for web browsers using wa-sqlite with IndexedDB or OPFS persistence.\n * Provides robust, persistent SQLite storage in the browser that prevents data loss.\n *\n * ## Data Safety Features\n *\n * - **IDBBatchAtomicVFS** (default): Uses IndexedDB batch atomic writes to ensure transactions\n * are either fully committed or not at all. Multi-tab safe.\n * - **IDBMirrorVFS**: IndexedDB with in-memory mirror. Much faster, database must fit in RAM.\n * - **OPFSCoopSyncVFS**: OPFS with cooperative sync. Multi-connection, filesystem transparent.\n * - **AccessHandlePoolVFS**: Uses OPFS Access Handles for high performance. Single-tab only.\n * - Full durability by default (`PRAGMA synchronous=full`)\n * - Automatic journal mode configuration for each VFS type\n *\n * ## VFS Selection Guide\n *\n * | VFS | Best For | Multi-Tab | Speed |\n * |-----|----------|-----------|-------|\n * | IDBBatchAtomicVFS | General use, main thread | ✅ | Good |\n * | IDBMirrorVFS | Small DBs, main thread | ✅ | Fast |\n * | OPFSCoopSyncVFS | Web Workers, file export | ✅ | Good |\n * | AccessHandlePoolVFS | Single-tab performance | ❌ | Fastest |\n *\n * @example\n * ```ts\n * import { WaSQLiteDriver } from '@anfenn/dync/wa-sqlite';\n * import { SQLiteAdapter } from '@anfenn/dync';\n *\n * // Default: IDBBatchAtomicVFS (works in main thread, multi-tab safe)\n * const driver = new WaSQLiteDriver('myapp.db');\n *\n * // For OPFS (faster, requires Worker, filesystem transparent)\n * const opfsDriver = new WaSQLiteDriver('myapp.db', { vfs: 'OPFSCoopSyncVFS' });\n *\n * const adapter = new SQLiteAdapter('myapp', driver);\n * ```\n */\nexport class WaSQLiteDriver implements SQLiteDatabaseDriver {\n readonly type = 'WaSQLiteDriver';\n private db: number | null = null;\n private sqlite3: any = null;\n private readonly options: Required<WaSQLiteDriverOptions>;\n private opened = false;\n private openPromise: Promise<void> | null = null;\n // Mutex to prevent concurrent database operations (critical for wa-sqlite)\n private executionLock: Promise<void> = Promise.resolve();\n readonly name: string;\n\n constructor(databaseName: string, options: WaSQLiteDriverOptions = {}) {\n this.name = databaseName;\n this.options = {\n vfs: 'IDBBatchAtomicVFS',\n directory: '/',\n pageSize: 4096,\n cacheSize: -2000,\n wal: false,\n synchronous: 'full',\n ...options,\n };\n }\n\n /**\n * Execute a callback with exclusive database access.\n * This prevents concurrent operations which can corrupt the database.\n */\n private async withLock<T>(fn: () => Promise<T>): Promise<T> {\n // Chain onto the existing lock\n const previousLock = this.executionLock;\n let releaseLock: () => void;\n this.executionLock = new Promise<void>((resolve) => {\n releaseLock = resolve;\n });\n\n try {\n // Wait for previous operation to complete\n await previousLock;\n // Execute our operation\n return await fn();\n } finally {\n // Release the lock\n releaseLock!();\n }\n }\n\n async open(): Promise<void> {\n if (this.opened) return;\n if (this.openPromise) return this.openPromise;\n\n this.openPromise = this._open();\n\n try {\n await this.openPromise;\n } finally {\n this.openPromise = null;\n }\n }\n\n private async _open(): Promise<void> {\n // Load wa-sqlite module (asyncify build for async VFS support)\n const module = await this.loadWasmModule();\n\n // Create SQLite API from module (cached - must only create once per module)\n if (!cachedSQLite3) {\n const { Factory } = await import('@journeyapps/wa-sqlite');\n cachedSQLite3 = Factory(module);\n }\n this.sqlite3 = cachedSQLite3;\n\n // For IDB-based VFS, the VFS name is also used as the IndexedDB database name\n // Use a unique name based on database name to avoid conflicts\n const vfsName = `dync_${this.options.vfs}_${this.name}`.replace(/[^a-zA-Z0-9_-]/g, '_');\n\n // Reuse existing VFS instance or create and register a new one\n let existingVfs = registeredVFS.get(vfsName);\n if (!existingVfs) {\n existingVfs = await this.createVFS(module, vfsName);\n // Register VFS with SQLite as default (like PowerSync does)\n this.sqlite3.vfs_register(existingVfs, true);\n registeredVFS.set(vfsName, existingVfs);\n }\n\n // Build database path - for IDB VFS, this is the \"file\" path within the VFS\n const dbPath = this.buildDatabasePath();\n\n // Open database (VFS is registered as default)\n this.db = await this.sqlite3.open_v2(dbPath);\n\n // Configure database pragmas for performance and durability\n await this.configurePragmas();\n\n this.opened = true;\n }\n\n private async loadWasmModule(): Promise<any> {\n if (!cachedModule) {\n if (!cachedModuleFactory) {\n // Dynamically import the asyncify build for async VFS support\n const wasmModule = await import('@journeyapps/wa-sqlite/dist/wa-sqlite-async.mjs');\n cachedModuleFactory = wasmModule.default;\n }\n // Cache the module instance - all VFS and sqlite3 APIs must share the same module\n cachedModule = await cachedModuleFactory();\n }\n return cachedModule;\n }\n\n private async createVFS(module: any, vfsName: string): Promise<WaSQLiteVFS> {\n const vfsType = this.options.vfs;\n let VFSClass: VFSClass;\n let vfsOptions: any = undefined;\n\n // Dynamically import VFS implementation\n // Note: We cast to unknown first because the package types don't include the static create method\n switch (vfsType) {\n case 'IDBBatchAtomicVFS': {\n const mod = await import('@journeyapps/wa-sqlite/src/examples/IDBBatchAtomicVFS.js');\n VFSClass = mod.IDBBatchAtomicVFS as unknown as VFSClass;\n // Use exclusive lock policy like PowerSync does\n vfsOptions = { lockPolicy: 'exclusive' };\n break;\n }\n case 'IDBMirrorVFS': {\n const mod = await import('@journeyapps/wa-sqlite/src/examples/IDBMirrorVFS.js');\n VFSClass = mod.IDBMirrorVFS as unknown as VFSClass;\n break;\n }\n case 'OPFSCoopSyncVFS': {\n const mod = await import('@journeyapps/wa-sqlite/src/examples/OPFSCoopSyncVFS.js');\n VFSClass = mod.OPFSCoopSyncVFS as unknown as VFSClass;\n break;\n }\n case 'AccessHandlePoolVFS': {\n const mod = await import('@journeyapps/wa-sqlite/src/examples/AccessHandlePoolVFS.js');\n VFSClass = mod.AccessHandlePoolVFS as unknown as VFSClass;\n break;\n }\n default:\n throw new Error(`Unsupported VFS type: ${vfsType}`);\n }\n\n return VFSClass.create(vfsName, module, vfsOptions);\n }\n\n private buildDatabasePath(): string {\n const vfsType = this.options.vfs;\n\n // For IDB-based VFS, use database name directly\n if (vfsType === 'IDBBatchAtomicVFS' || vfsType === 'IDBMirrorVFS') {\n return this.name;\n }\n\n // For OPFS-based VFS, build full path\n const directory = this.options.directory.replace(/\\/$/, '');\n return `${directory}/${this.name}`;\n }\n\n private async configurePragmas(): Promise<void> {\n if (!this.db || !this.sqlite3) return;\n\n // Page size can only be set on new/empty databases\n // For IDBBatchAtomicVFS, it cannot be changed after creation\n // Try to set it, but ignore errors if the database already exists\n try {\n await this.sqlite3.exec(this.db, `PRAGMA page_size = ${this.options.pageSize}`);\n } catch {\n // Page size already set, ignore\n }\n\n // Cache size for performance\n await this.sqlite3.exec(this.db, `PRAGMA cache_size = ${this.options.cacheSize}`);\n\n // WAL mode only for AccessHandlePoolVFS with exclusive locking\n if (this.options.wal && this.options.vfs === 'AccessHandlePoolVFS') {\n await this.sqlite3.exec(this.db, 'PRAGMA locking_mode = exclusive');\n await this.sqlite3.exec(this.db, 'PRAGMA journal_mode = WAL');\n }\n // Note: For IDB-based VFS, we don't set journal_mode - let the VFS handle it\n }\n\n async close(): Promise<void> {\n if (!this.opened || !this.db || !this.sqlite3) return;\n\n // Wait for any pending operations to complete by acquiring the lock\n await this.withLock(async () => {\n // Ensure all data is flushed before closing\n // This is critical for IDB-based VFS which batch writes\n try {\n await this.sqlite3!.exec(this.db!, 'PRAGMA wal_checkpoint(TRUNCATE)');\n } catch {\n // Ignore if WAL mode not enabled\n }\n\n await this.sqlite3!.close(this.db!);\n\n // Don't close the shared VFS - it may be used by other connections\n // The VFS will be cleaned up when all references are gone\n this.db = null;\n this.opened = false;\n });\n }\n\n async execute(statement: string): Promise<void> {\n await this.open();\n\n if (!this.db || !this.sqlite3) {\n throw new Error('Database not initialized');\n }\n\n await this.withLock(async () => {\n await this.sqlite3.exec(this.db, statement);\n });\n }\n\n async run(statement: string, values: unknown[] = []): Promise<SQLiteRunResult> {\n await this.open();\n\n if (!this.db || !this.sqlite3) {\n throw new Error('Database not initialized');\n }\n\n return this.withLock(async () => {\n // Convert values for SQLite (booleans -> integers)\n const convertedValues = this.convertValues(values);\n\n // Use statements() generator with proper binding\n for await (const stmt of this.sqlite3.statements(this.db, statement)) {\n if (stmt === null) {\n break;\n }\n\n // Reset statement before binding (critical for wa-sqlite)\n this.sqlite3.reset(stmt);\n\n // Bind parameters if any\n if (convertedValues.length > 0) {\n this.sqlite3.bind_collection(stmt, convertedValues);\n }\n\n // Execute the statement\n await this.sqlite3.step(stmt);\n }\n\n return {\n changes: this.sqlite3.changes(this.db),\n lastId: Number(this.sqlite3.last_insert_id(this.db)),\n };\n });\n }\n\n /**\n * Convert values for SQLite compatibility.\n * - Booleans must be converted to integers (SQLite has no boolean type)\n */\n private convertValues(values: unknown[]): unknown[] {\n return values.map((value) => {\n if (typeof value === 'boolean') {\n return value ? 1 : 0;\n }\n return value;\n });\n }\n\n async query(statement: string, values: unknown[] = []): Promise<SQLiteQueryResult> {\n await this.open();\n\n if (!this.db || !this.sqlite3) {\n throw new Error('Database not initialized');\n }\n\n return this.withLock(async () => {\n const { SQLITE_ROW } = await import('@journeyapps/wa-sqlite');\n const allRows: unknown[][] = [];\n let columns: string[] = [];\n\n // Convert values for SQLite (booleans -> integers)\n const convertedValues = this.convertValues(values);\n\n // Use statements() generator with proper binding\n for await (const stmt of this.sqlite3.statements(this.db, statement)) {\n if (stmt === null) {\n break;\n }\n\n // Reset statement before binding (critical for wa-sqlite)\n this.sqlite3.reset(stmt);\n\n // Bind parameters if any\n if (convertedValues.length > 0) {\n this.sqlite3.bind_collection(stmt, convertedValues);\n }\n\n // Get column names\n if (columns.length === 0) {\n columns = this.sqlite3.column_names(stmt);\n }\n\n // Fetch all rows\n while ((await this.sqlite3.step(stmt)) === SQLITE_ROW) {\n const row = this.sqlite3.row(stmt);\n allRows.push(row);\n }\n }\n\n return { columns, values: allRows };\n });\n }\n\n /**\n * Check if the database is currently open.\n */\n isOpen(): boolean {\n return this.opened;\n }\n\n /**\n * Get the VFS type being used by this driver.\n */\n getVfsType(): WaSQLiteVfsType {\n return this.options.vfs;\n }\n\n /**\n * Delete the database.\n * This will close the database if open and remove all persisted data.\n * For IndexedDB-based VFS, this deletes the IndexedDB database.\n * For OPFS-based VFS, this removes the files from OPFS.\n */\n async delete(): Promise<void> {\n // Close if open\n if (this.opened) {\n await this.close();\n }\n\n const vfsType = this.options.vfs;\n\n if (vfsType === 'IDBBatchAtomicVFS' || vfsType === 'IDBMirrorVFS') {\n // Delete IndexedDB database\n await new Promise<void>((resolve, reject) => {\n const request = indexedDB.deleteDatabase(this.name);\n request.onsuccess = () => resolve();\n request.onerror = () => reject(request.error);\n request.onblocked = () => {\n console.warn(`Database deletion blocked for ${this.name}. Close all connections and try again.`);\n };\n });\n } else {\n // For OPFS-based VFS, remove the directory\n const dbPath = this.buildDatabasePath();\n const root = await navigator.storage.getDirectory();\n\n try {\n // Try to remove the file/directory\n const pathParts = dbPath.split('/').filter(Boolean);\n let current = root;\n\n // Navigate to parent directory\n for (let i = 0; i < pathParts.length - 1; i++) {\n current = await current.getDirectoryHandle(pathParts[i]!);\n }\n\n // Remove the database file\n const filename = pathParts[pathParts.length - 1]!;\n await current.removeEntry(filename, { recursive: true });\n\n // Also try to remove associated journal/wal files\n const associatedFiles = [`${filename}-journal`, `${filename}-wal`, `${filename}-shm`];\n for (const file of associatedFiles) {\n try {\n await current.removeEntry(file, { recursive: false });\n } catch {\n // Ignore if file doesn't exist\n }\n }\n } catch (error) {\n // Ignore if directory doesn't exist\n if ((error as Error).name !== 'NotFoundError') {\n throw error;\n }\n }\n }\n }\n}\n"],"mappings":";AA6GA,IAAI,sBAAmD;AACvD,IAAI,eAAoB;AACxB,IAAI,gBAAqB;AAEzB,IAAM,gBAAgB,oBAAI,IAAyB;AAuC5C,IAAM,iBAAN,MAAqD;AAAA,EAC/C,OAAO;AAAA,EACR,KAAoB;AAAA,EACpB,UAAe;AAAA,EACN;AAAA,EACT,SAAS;AAAA,EACT,cAAoC;AAAA;AAAA,EAEpC,gBAA+B,QAAQ,QAAQ;AAAA,EAC9C;AAAA,EAET,YAAY,cAAsB,UAAiC,CAAC,GAAG;AACnE,SAAK,OAAO;AACZ,SAAK,UAAU;AAAA,MACX,KAAK;AAAA,MACL,WAAW;AAAA,MACX,UAAU;AAAA,MACV,WAAW;AAAA,MACX,KAA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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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anfenn/dync",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.31",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Write once, run IndexedDB & SQLite with sync anywhere - React, React Native, Expo, Capacitor, Electron & Node.js",
|
|
6
6
|
"keywords": [
|
|
@@ -132,8 +132,8 @@
|
|
|
132
132
|
"LICENSE"
|
|
133
133
|
],
|
|
134
134
|
"scripts": {
|
|
135
|
-
"build": "pnpm
|
|
136
|
-
"build:all": "pnpm
|
|
135
|
+
"build": "pnpm format && pnpm lint && pnpm exec tsc --noEmit && tsup",
|
|
136
|
+
"build:all": "pnpm build && pnpm --filter react-capacitor build",
|
|
137
137
|
"dev": "tsup --watch",
|
|
138
138
|
"lint": "eslint . --fix \"src/**/*.{js,jsx,ts,tsx,json}\"",
|
|
139
139
|
"format": "prettier --write \"**/*.{ts,tsx,md}\"",
|
|
@@ -142,7 +142,9 @@
|
|
|
142
142
|
"test:all": "pnpm --filter tests test:all",
|
|
143
143
|
"test:ci": "pnpm --filter tests test:ci",
|
|
144
144
|
"test:browser:full": "pnpm --filter tests test:browser:full",
|
|
145
|
-
"reinstall": "find . -name node_modules -type d -prune -exec rm -rf {} + && pnpm install"
|
|
145
|
+
"reinstall": "find . -name node_modules -type d -prune -exec rm -rf {} + && pnpm install",
|
|
146
|
+
"push": "bash -c '[ -n \"$1\" ] && git add . && git commit -m \"$1\" && git push' --",
|
|
147
|
+
"release": "bash -c '[ -n \"$1\" ] || { echo \"Usage: pnpm release <commit-message>\"; exit 1; } && pnpm build:all && pnpm test:all && pnpm test:browser:full && git add . && git commit -m \"$1\" && npm version patch && git push && npm login && npm publish' --"
|
|
146
148
|
},
|
|
147
149
|
"devDependencies": {
|
|
148
150
|
"@capacitor-community/sqlite": "7.0.2",
|
|
@@ -6,7 +6,7 @@ import type { WithTransaction } from './types';
|
|
|
6
6
|
export type EmitMutation = (event: MutationEvent) => void;
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* Wraps a table with mutation emission for reactive updates.
|
|
9
|
+
* Wraps a table with mutation emission for reactive updates and auto-generates _localId.
|
|
10
10
|
* This allows useLiveQuery to work with any table.
|
|
11
11
|
*/
|
|
12
12
|
export function wrapWithMutationEmitter<T>(table: StorageTable<T>, tableName: string, emitMutation: EmitMutation): void {
|
|
@@ -21,13 +21,15 @@ export function wrapWithMutationEmitter<T>(table: StorageTable<T>, tableName: st
|
|
|
21
21
|
const rawClear = table.raw.clear;
|
|
22
22
|
|
|
23
23
|
table.add = async (item: AddItem<T>) => {
|
|
24
|
-
const
|
|
24
|
+
const itemWithLocalId = { ...item, _localId: (item as any)._localId || createLocalId() } as T;
|
|
25
|
+
const result = await rawAdd(itemWithLocalId);
|
|
25
26
|
emitMutation({ type: 'add', tableName, keys: [result] });
|
|
26
27
|
return result;
|
|
27
28
|
};
|
|
28
29
|
|
|
29
30
|
table.put = async (item: T) => {
|
|
30
|
-
const
|
|
31
|
+
const itemWithLocalId = { ...item, _localId: (item as any)._localId || createLocalId() } as T;
|
|
32
|
+
const result = await rawPut(itemWithLocalId);
|
|
31
33
|
emitMutation({ type: 'update', tableName, keys: [result] });
|
|
32
34
|
return result;
|
|
33
35
|
};
|
|
@@ -46,7 +48,11 @@ export function wrapWithMutationEmitter<T>(table: StorageTable<T>, tableName: st
|
|
|
46
48
|
};
|
|
47
49
|
|
|
48
50
|
table.bulkAdd = async (items: AddItem<T>[]) => {
|
|
49
|
-
const
|
|
51
|
+
const itemsWithLocalIds = items.map((item) => ({
|
|
52
|
+
...item,
|
|
53
|
+
_localId: (item as any)._localId || createLocalId(),
|
|
54
|
+
})) as T[];
|
|
55
|
+
const result = await rawBulkAdd(itemsWithLocalIds);
|
|
50
56
|
if (items.length > 0) {
|
|
51
57
|
emitMutation({ type: 'add', tableName });
|
|
52
58
|
}
|
|
@@ -54,7 +60,11 @@ export function wrapWithMutationEmitter<T>(table: StorageTable<T>, tableName: st
|
|
|
54
60
|
};
|
|
55
61
|
|
|
56
62
|
table.bulkPut = async (items: T[]) => {
|
|
57
|
-
const
|
|
63
|
+
const itemsWithLocalIds = items.map((item) => ({
|
|
64
|
+
...item,
|
|
65
|
+
_localId: (item as any)._localId || createLocalId(),
|
|
66
|
+
})) as T[];
|
|
67
|
+
const result = await rawBulkPut(itemsWithLocalIds);
|
|
58
68
|
if (items.length > 0) {
|
|
59
69
|
emitMutation({ type: 'update', tableName });
|
|
60
70
|
}
|
package/src/index.native.ts
CHANGED
|
@@ -2,7 +2,7 @@ export * from './index.shared';
|
|
|
2
2
|
export { MemoryAdapter } from './storage/memory';
|
|
3
3
|
export { SQLiteAdapter } from './storage/sqlite';
|
|
4
4
|
export { MemoryQueryContext } from './storage/memory';
|
|
5
|
-
export {
|
|
5
|
+
export { SQLiteQueryContext } from './storage/sqlite';
|
|
6
6
|
export type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from './storage/sqlite/types';
|
|
7
7
|
export type { StorageAdapter } from './storage/types';
|
|
8
8
|
export { SyncAction } from './types';
|
package/src/index.shared.ts
CHANGED
|
@@ -28,7 +28,7 @@ import { startFirstLoad as runFirstLoad, startFirstLoadBatch as runFirstLoadBatc
|
|
|
28
28
|
import type { FirstLoadProgressCallback, VisibilitySubscription } from './types';
|
|
29
29
|
import { StateManager, DYNC_STATE_TABLE, type StateHelpers } from './core/StateManager';
|
|
30
30
|
import type { MemoryQueryContext } from './storage/memory/MemoryQueryContext';
|
|
31
|
-
import type {
|
|
31
|
+
import type { SQLiteQueryContext } from './storage/sqlite/SQLiteQueryContext';
|
|
32
32
|
import type { DexieQueryContext } from './storage/dexie/DexieQueryContext';
|
|
33
33
|
|
|
34
34
|
const DEFAULT_SYNC_INTERVAL_MILLIS = 2000;
|
|
@@ -85,15 +85,17 @@ class DyncBase<_TStoreMap extends Record<string, any> = Record<string, any>> {
|
|
|
85
85
|
constructor(config: DyncOptions<_TStoreMap>) {
|
|
86
86
|
const { databaseName, storageAdapter, sync: syncConfig, options } = config;
|
|
87
87
|
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
if (syncConfig) {
|
|
89
|
+
// Detect mode based on whether sync config has batch sync shape
|
|
90
|
+
const isBatchMode = typeof syncConfig.push === 'function' && typeof syncConfig.pull === 'function';
|
|
90
91
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
92
|
+
if (isBatchMode) {
|
|
93
|
+
this.batchSync = syncConfig as BatchSync;
|
|
94
|
+
this.syncedTables = new Set(this.batchSync.syncTables);
|
|
95
|
+
} else {
|
|
96
|
+
this.syncApis = syncConfig as Record<string, ApiFunctions>;
|
|
97
|
+
this.syncedTables = new Set(Object.keys(this.syncApis));
|
|
98
|
+
}
|
|
97
99
|
}
|
|
98
100
|
|
|
99
101
|
this.adapter = storageAdapter;
|
|
@@ -158,6 +160,9 @@ class DyncBase<_TStoreMap extends Record<string, any> = Record<string, any>> {
|
|
|
158
160
|
// Auto-inject sync fields for sync tables
|
|
159
161
|
// Note: updated_at is indexed to support user queries like orderBy('updated_at')
|
|
160
162
|
fullSchema[tableName] = `${LOCAL_PK}, &${SERVER_PK}, ${tableSchema}, ${UPDATED_AT}`;
|
|
163
|
+
} else {
|
|
164
|
+
// Auto-inject _localId as primary key for non-sync tables
|
|
165
|
+
fullSchema[tableName] = `${LOCAL_PK}, ${tableSchema}`;
|
|
161
166
|
}
|
|
162
167
|
|
|
163
168
|
self.logger.debug(
|
|
@@ -167,6 +172,9 @@ class DyncBase<_TStoreMap extends Record<string, any> = Record<string, any>> {
|
|
|
167
172
|
if (isSyncTable) {
|
|
168
173
|
// Auto-inject sync columns for structured schemas
|
|
169
174
|
fullSchema[tableName] = self.injectSyncColumns(tableSchema);
|
|
175
|
+
} else {
|
|
176
|
+
// Auto-inject _localId column for non-sync structured schemas
|
|
177
|
+
fullSchema[tableName] = self.injectLocalIdColumn(tableSchema);
|
|
170
178
|
}
|
|
171
179
|
|
|
172
180
|
const schemaColumns = Object.keys((fullSchema[tableName] as SQLiteTableDefinition).columns ?? {}).join(', ');
|
|
@@ -204,11 +212,11 @@ class DyncBase<_TStoreMap extends Record<string, any> = Record<string, any>> {
|
|
|
204
212
|
const sqliteOptions = (schemaOptions.sqlite ??= {});
|
|
205
213
|
const migrations = (sqliteOptions.migrations ??= {});
|
|
206
214
|
const configurator: SQLiteVersionConfigurator = {
|
|
207
|
-
|
|
208
|
-
migrations.
|
|
215
|
+
up(handler) {
|
|
216
|
+
migrations.up = handler;
|
|
209
217
|
},
|
|
210
|
-
|
|
211
|
-
migrations.
|
|
218
|
+
down(handler) {
|
|
219
|
+
migrations.down = handler;
|
|
212
220
|
},
|
|
213
221
|
};
|
|
214
222
|
configure(configurator);
|
|
@@ -260,7 +268,7 @@ class DyncBase<_TStoreMap extends Record<string, any> = Record<string, any>> {
|
|
|
260
268
|
this.syncEnhancedTables.clear();
|
|
261
269
|
}
|
|
262
270
|
|
|
263
|
-
async query<R>(callback: (ctx: DexieQueryContext |
|
|
271
|
+
async query<R>(callback: (ctx: DexieQueryContext | SQLiteQueryContext | MemoryQueryContext) => Promise<R>): Promise<R> {
|
|
264
272
|
return this.adapter.query(callback as any);
|
|
265
273
|
}
|
|
266
274
|
|
|
@@ -350,6 +358,26 @@ class DyncBase<_TStoreMap extends Record<string, any> = Record<string, any>> {
|
|
|
350
358
|
};
|
|
351
359
|
}
|
|
352
360
|
|
|
361
|
+
private injectLocalIdColumn(schema: SQLiteTableDefinition): SQLiteTableDefinition {
|
|
362
|
+
const columns = schema.columns ?? {};
|
|
363
|
+
|
|
364
|
+
// Validate user hasn't defined reserved _localId column
|
|
365
|
+
if (columns[LOCAL_PK]) {
|
|
366
|
+
throw new Error(`Column '${LOCAL_PK}' is auto-injected and cannot be defined manually.`);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// Inject _localId column as primary key
|
|
370
|
+
const injectedColumns: Record<string, any> = {
|
|
371
|
+
...columns,
|
|
372
|
+
[LOCAL_PK]: { type: 'TEXT' },
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
return {
|
|
376
|
+
...schema,
|
|
377
|
+
columns: injectedColumns,
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
|
|
353
381
|
private enhanceSyncTable<T>(table: StorageTable<T & SyncedRecord>, tableName: string): void {
|
|
354
382
|
enhanceSyncTable({
|
|
355
383
|
table,
|
package/src/index.ts
CHANGED
|
@@ -2,7 +2,7 @@ export * from './index.shared';
|
|
|
2
2
|
export { MemoryAdapter } from './storage/memory';
|
|
3
3
|
export { SQLiteAdapter } from './storage/sqlite';
|
|
4
4
|
export { MemoryQueryContext } from './storage/memory';
|
|
5
|
-
export {
|
|
5
|
+
export { SQLiteQueryContext } from './storage/sqlite';
|
|
6
6
|
export type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from './storage/sqlite/types';
|
|
7
7
|
export type { StorageAdapter } from './storage/types';
|
|
8
8
|
export { SyncAction } from './types';
|
package/src/node.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
// Node.js SQLite Driver using better-sqlite3
|
|
2
2
|
// Works in Node.js apps and Electron main process
|
|
3
|
-
export {
|
|
3
|
+
export { BetterSQLite3Driver, type BetterSQLite3DriverOptions } from './storage/sqlite/drivers/BetterSQLite3Driver';
|
|
4
4
|
export type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from './storage/sqlite/types';
|
|
@@ -7,14 +7,14 @@ import type {
|
|
|
7
7
|
SQLiteDefaultValue,
|
|
8
8
|
SQLiteForeignKeyReference,
|
|
9
9
|
} from './schema';
|
|
10
|
-
import type { StorageSchemaDefinitionOptions, SQLiteMigrationContext,
|
|
10
|
+
import type { StorageSchemaDefinitionOptions, SQLiteMigrationContext, SQLiteMigrationHandler } from './types';
|
|
11
11
|
import type { SQLiteDatabaseDriver, SQLiteRunResult, SQLiteQueryResult } from './types';
|
|
12
12
|
import { LOCAL_PK } from '../../types';
|
|
13
13
|
import type { SQLiteAdapterOptions, SQLiteTableSchemaMetadata, SQLiteColumnSchema } from './types';
|
|
14
14
|
import { DYNC_STATE_TABLE } from '../../core/StateManager';
|
|
15
15
|
import { SQLITE_SCHEMA_VERSION_STATE_KEY, quoteIdentifier } from './helpers';
|
|
16
16
|
import { SQLiteTable } from './SQLiteTable';
|
|
17
|
-
import {
|
|
17
|
+
import { SQLiteQueryContext } from './SQLiteQueryContext';
|
|
18
18
|
|
|
19
19
|
export class SQLiteAdapter implements StorageAdapter {
|
|
20
20
|
readonly type = 'SQLiteAdapter';
|
|
@@ -199,12 +199,12 @@ export class SQLiteAdapter implements StorageAdapter {
|
|
|
199
199
|
return driver.run(statement, params);
|
|
200
200
|
}
|
|
201
201
|
|
|
202
|
-
async query<R>(callback: (ctx:
|
|
202
|
+
async query<R>(callback: (ctx: SQLiteQueryContext) => Promise<R>): Promise<R>;
|
|
203
203
|
async query(statement: string, values?: any[]): Promise<SQLiteQueryResult>;
|
|
204
|
-
async query<R>(arg1: string | ((ctx:
|
|
204
|
+
async query<R>(arg1: string | ((ctx: SQLiteQueryContext) => Promise<R>), arg2?: any[]): Promise<R | SQLiteQueryResult> {
|
|
205
205
|
if (typeof arg1 === 'function') {
|
|
206
206
|
const driver = await this.getDriver();
|
|
207
|
-
return arg1(new
|
|
207
|
+
return arg1(new SQLiteQueryContext(driver, this));
|
|
208
208
|
}
|
|
209
209
|
const statement = arg1;
|
|
210
210
|
const values = arg2;
|
|
@@ -336,27 +336,24 @@ export class SQLiteAdapter implements StorageAdapter {
|
|
|
336
336
|
|
|
337
337
|
if (currentVersion < targetVersion) {
|
|
338
338
|
for (let version = currentVersion + 1; version <= targetVersion; version += 1) {
|
|
339
|
-
await this.runMigrationStep(version, '
|
|
339
|
+
await this.runMigrationStep(version, 'up');
|
|
340
340
|
await this.setStoredSchemaVersion(version);
|
|
341
341
|
}
|
|
342
342
|
return;
|
|
343
343
|
}
|
|
344
344
|
|
|
345
345
|
for (let version = currentVersion; version > targetVersion; version -= 1) {
|
|
346
|
-
await this.runMigrationStep(version, '
|
|
346
|
+
await this.runMigrationStep(version, 'down');
|
|
347
347
|
await this.setStoredSchemaVersion(version - 1);
|
|
348
348
|
}
|
|
349
349
|
}
|
|
350
350
|
|
|
351
|
-
private async runMigrationStep(version: number, direction:
|
|
351
|
+
private async runMigrationStep(version: number, direction: 'up' | 'down'): Promise<void> {
|
|
352
352
|
const handler = this.getMigrationHandler(version, direction);
|
|
353
353
|
if (!handler) {
|
|
354
354
|
return;
|
|
355
355
|
}
|
|
356
356
|
const context: SQLiteMigrationContext = {
|
|
357
|
-
direction,
|
|
358
|
-
fromVersion,
|
|
359
|
-
toVersion,
|
|
360
357
|
execute: (statement: string) => this.internalExecute(statement),
|
|
361
358
|
run: (statement: string, values?: any[]) => this.internalRun(statement, values),
|
|
362
359
|
query: (statement: string, values?: any[]) => this.internalQuery(statement, values),
|
|
@@ -364,13 +361,13 @@ export class SQLiteAdapter implements StorageAdapter {
|
|
|
364
361
|
await handler(context);
|
|
365
362
|
}
|
|
366
363
|
|
|
367
|
-
private getMigrationHandler(version: number, direction:
|
|
364
|
+
private getMigrationHandler(version: number, direction: 'up' | 'down'): SQLiteMigrationHandler | undefined {
|
|
368
365
|
const options = this.versionOptions.get(version);
|
|
369
366
|
const migrations = options?.sqlite?.migrations;
|
|
370
367
|
if (!migrations) {
|
|
371
368
|
return undefined;
|
|
372
369
|
}
|
|
373
|
-
return direction === '
|
|
370
|
+
return direction === 'up' ? migrations.up : migrations.down;
|
|
374
371
|
}
|
|
375
372
|
|
|
376
373
|
private async applySchema(): Promise<void> {
|
|
@@ -2,7 +2,7 @@ import type { StorageTable, StorageTransactionContext, TransactionMode } from '.
|
|
|
2
2
|
import type { SQLiteDatabaseDriver, SQLiteRunResult } from './types';
|
|
3
3
|
import { SQLiteAdapter } from './SQLiteAdapter';
|
|
4
4
|
|
|
5
|
-
export class
|
|
5
|
+
export class SQLiteQueryContext {
|
|
6
6
|
constructor(
|
|
7
7
|
private readonly driver: SQLiteDatabaseDriver,
|
|
8
8
|
private readonly adapter: SQLiteAdapter,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from '../types';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* Options for configuring the
|
|
4
|
+
* Options for configuring the BetterSQLite3Driver.
|
|
5
5
|
*/
|
|
6
|
-
export interface
|
|
6
|
+
export interface BetterSQLite3DriverOptions {
|
|
7
7
|
/**
|
|
8
8
|
* Open the database in readonly mode.
|
|
9
9
|
* @default false
|
|
@@ -42,21 +42,21 @@ export interface BetterSqlite3DriverOptions {
|
|
|
42
42
|
*
|
|
43
43
|
* @example
|
|
44
44
|
* ```ts
|
|
45
|
-
* import {
|
|
45
|
+
* import { BetterSQLite3Driver } from '@anfenn/dync/node';
|
|
46
46
|
* import { SQLiteAdapter } from '@anfenn/dync';
|
|
47
47
|
*
|
|
48
|
-
* const driver = new
|
|
48
|
+
* const driver = new BetterSQLite3Driver('myapp.db', { wal: true });
|
|
49
49
|
* const adapter = new SQLiteAdapter('myapp', driver);
|
|
50
50
|
* ```
|
|
51
51
|
*/
|
|
52
|
-
export class
|
|
53
|
-
readonly type = '
|
|
52
|
+
export class BetterSQLite3Driver implements SQLiteDatabaseDriver {
|
|
53
|
+
readonly type = 'BetterSQLite3Driver';
|
|
54
54
|
private db: import('better-sqlite3').Database | null = null;
|
|
55
|
-
private readonly options:
|
|
55
|
+
private readonly options: BetterSQLite3DriverOptions;
|
|
56
56
|
private opened = false;
|
|
57
57
|
readonly name: string;
|
|
58
58
|
|
|
59
|
-
constructor(databasePath: string, options:
|
|
59
|
+
constructor(databasePath: string, options: BetterSQLite3DriverOptions = {}) {
|
|
60
60
|
this.name = databasePath;
|
|
61
61
|
this.options = {
|
|
62
62
|
wal: true,
|
|
@@ -4,7 +4,7 @@ import type { SQLiteDatabaseDriver, SQLiteRunResult, SQLiteQueryResult } from '.
|
|
|
4
4
|
// Lazily loaded module cache to avoid top-level imports that break web bundlers
|
|
5
5
|
let sqliteModuleCache: typeof import('@capacitor-community/sqlite') | null = null;
|
|
6
6
|
|
|
7
|
-
async function
|
|
7
|
+
async function getSQLiteModule(): Promise<typeof import('@capacitor-community/sqlite')> {
|
|
8
8
|
if (!sqliteModuleCache) {
|
|
9
9
|
sqliteModuleCache = await import('@capacitor-community/sqlite');
|
|
10
10
|
}
|
|
@@ -35,7 +35,7 @@ export class CapacitorSQLiteDriver implements SQLiteDatabaseDriver {
|
|
|
35
35
|
|
|
36
36
|
private async getConnectionFactory(): Promise<SQLiteConnection> {
|
|
37
37
|
if (!this.connectionFactory) {
|
|
38
|
-
const { CapacitorSQLite, SQLiteConnection } = await
|
|
38
|
+
const { CapacitorSQLite, SQLiteConnection } = await getSQLiteModule();
|
|
39
39
|
this.connectionFactory = new SQLiteConnection(CapacitorSQLite);
|
|
40
40
|
}
|
|
41
41
|
return this.connectionFactory;
|