@anfenn/dync 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +212 -0
  3. package/dist/capacitor.cjs +228 -0
  4. package/dist/capacitor.cjs.map +1 -0
  5. package/dist/capacitor.d.cts +62 -0
  6. package/dist/capacitor.d.ts +62 -0
  7. package/dist/capacitor.js +9 -0
  8. package/dist/capacitor.js.map +1 -0
  9. package/dist/chunk-LGHOZECP.js +3884 -0
  10. package/dist/chunk-LGHOZECP.js.map +1 -0
  11. package/dist/chunk-SQB6E7V2.js +191 -0
  12. package/dist/chunk-SQB6E7V2.js.map +1 -0
  13. package/dist/dexie-Bv-fV10P.d.cts +444 -0
  14. package/dist/dexie-DJFApKsM.d.ts +444 -0
  15. package/dist/dexie.cjs +381 -0
  16. package/dist/dexie.cjs.map +1 -0
  17. package/dist/dexie.d.cts +3 -0
  18. package/dist/dexie.d.ts +3 -0
  19. package/dist/dexie.js +343 -0
  20. package/dist/dexie.js.map +1 -0
  21. package/dist/expoSqlite.cjs +98 -0
  22. package/dist/expoSqlite.cjs.map +1 -0
  23. package/dist/expoSqlite.d.cts +17 -0
  24. package/dist/expoSqlite.d.ts +17 -0
  25. package/dist/expoSqlite.js +61 -0
  26. package/dist/expoSqlite.js.map +1 -0
  27. package/dist/index.cjs +3916 -0
  28. package/dist/index.cjs.map +1 -0
  29. package/dist/index.d.cts +8 -0
  30. package/dist/index.d.ts +8 -0
  31. package/dist/index.js +20 -0
  32. package/dist/index.js.map +1 -0
  33. package/dist/index.shared-CPIge2ZM.d.ts +234 -0
  34. package/dist/index.shared-YSn6c01d.d.cts +234 -0
  35. package/dist/node.cjs +126 -0
  36. package/dist/node.cjs.map +1 -0
  37. package/dist/node.d.cts +80 -0
  38. package/dist/node.d.ts +80 -0
  39. package/dist/node.js +89 -0
  40. package/dist/node.js.map +1 -0
  41. package/dist/react/index.cjs +1754 -0
  42. package/dist/react/index.cjs.map +1 -0
  43. package/dist/react/index.d.cts +40 -0
  44. package/dist/react/index.d.ts +40 -0
  45. package/dist/react/index.js +78 -0
  46. package/dist/react/index.js.map +1 -0
  47. package/dist/types-CSbIAfu2.d.cts +46 -0
  48. package/dist/types-CSbIAfu2.d.ts +46 -0
  49. package/dist/wa-sqlite.cjs +318 -0
  50. package/dist/wa-sqlite.cjs.map +1 -0
  51. package/dist/wa-sqlite.d.cts +175 -0
  52. package/dist/wa-sqlite.d.ts +175 -0
  53. package/dist/wa-sqlite.js +281 -0
  54. package/dist/wa-sqlite.js.map +1 -0
  55. package/package.json +171 -0
  56. package/src/addVisibilityChangeListener.native.ts +33 -0
  57. package/src/addVisibilityChangeListener.ts +24 -0
  58. package/src/capacitor.ts +4 -0
  59. package/src/core/StateManager.ts +272 -0
  60. package/src/core/firstLoad.ts +332 -0
  61. package/src/core/pullOperations.ts +212 -0
  62. package/src/core/pushOperations.ts +290 -0
  63. package/src/core/tableEnhancers.ts +457 -0
  64. package/src/core/types.ts +3 -0
  65. package/src/createLocalId.native.ts +8 -0
  66. package/src/createLocalId.ts +6 -0
  67. package/src/dexie.ts +2 -0
  68. package/src/expoSqlite.ts +2 -0
  69. package/src/helpers.ts +87 -0
  70. package/src/index.native.ts +28 -0
  71. package/src/index.shared.ts +613 -0
  72. package/src/index.ts +28 -0
  73. package/src/logger.ts +26 -0
  74. package/src/node.ts +4 -0
  75. package/src/react/index.ts +2 -0
  76. package/src/react/useDync.ts +156 -0
  77. package/src/storage/dexie/DexieAdapter.ts +72 -0
  78. package/src/storage/dexie/DexieQueryContext.ts +14 -0
  79. package/src/storage/dexie/DexieStorageCollection.ts +124 -0
  80. package/src/storage/dexie/DexieStorageTable.ts +123 -0
  81. package/src/storage/dexie/DexieStorageWhereClause.ts +103 -0
  82. package/src/storage/dexie/helpers.ts +1 -0
  83. package/src/storage/dexie/index.ts +7 -0
  84. package/src/storage/memory/MemoryAdapter.ts +55 -0
  85. package/src/storage/memory/MemoryCollection.ts +215 -0
  86. package/src/storage/memory/MemoryQueryContext.ts +14 -0
  87. package/src/storage/memory/MemoryTable.ts +336 -0
  88. package/src/storage/memory/MemoryWhereClause.ts +134 -0
  89. package/src/storage/memory/index.ts +7 -0
  90. package/src/storage/memory/types.ts +24 -0
  91. package/src/storage/sqlite/SQLiteAdapter.ts +564 -0
  92. package/src/storage/sqlite/SQLiteCollection.ts +294 -0
  93. package/src/storage/sqlite/SQLiteTable.ts +604 -0
  94. package/src/storage/sqlite/SQLiteWhereClause.ts +341 -0
  95. package/src/storage/sqlite/SqliteQueryContext.ts +30 -0
  96. package/src/storage/sqlite/drivers/BetterSqlite3Driver.ts +156 -0
  97. package/src/storage/sqlite/drivers/CapacitorFastSqlDriver.ts +114 -0
  98. package/src/storage/sqlite/drivers/CapacitorSQLiteDriver.ts +137 -0
  99. package/src/storage/sqlite/drivers/ExpoSQLiteDriver.native.ts +67 -0
  100. package/src/storage/sqlite/drivers/WaSqliteDriver.ts +537 -0
  101. package/src/storage/sqlite/drivers/wa-sqlite-vfs.d.ts +46 -0
  102. package/src/storage/sqlite/helpers.ts +144 -0
  103. package/src/storage/sqlite/index.ts +11 -0
  104. package/src/storage/sqlite/schema.ts +44 -0
  105. package/src/storage/sqlite/types.ts +164 -0
  106. package/src/storage/types.ts +112 -0
  107. package/src/types.ts +186 -0
  108. package/src/wa-sqlite.ts +4 -0
@@ -0,0 +1,537 @@
1
+ import type { SQLiteDatabaseDriver, SQLiteQueryResult, SQLiteRunResult } from '../types';
2
+
3
+ /**
4
+ * Virtual File System (VFS) options for wa-sqlite.
5
+ * Each VFS has different trade-offs for performance, durability, and compatibility.
6
+ *
7
+ * @see https://github.com/rhashimoto/wa-sqlite/tree/master/src/examples#vfs-comparison
8
+ */
9
+ export type WaSqliteVfsType =
10
+ /**
11
+ * IDBBatchAtomicVFS - IndexedDB-backed storage
12
+ * - Works on ALL contexts (Window, Worker, SharedWorker, service worker)
13
+ * - Supports multiple connections
14
+ * - Full durability with batch atomic writes
15
+ * - Good general-purpose choice for maximum compatibility
16
+ * @recommended For apps that need to work in main thread and don't need OPFS
17
+ */
18
+ | 'IDBBatchAtomicVFS'
19
+ /**
20
+ * IDBMirrorVFS - IndexedDB with in-memory mirror
21
+ * - Works on ALL contexts
22
+ * - Supports multiple connections
23
+ * - Much faster than IDBBatchAtomicVFS
24
+ * - Database must fit in available memory
25
+ * @recommended For small databases where performance is critical
26
+ */
27
+ | 'IDBMirrorVFS'
28
+ /**
29
+ * OPFSCoopSyncVFS - OPFS with cooperative synchronous access
30
+ * - Requires Worker context
31
+ * - Supports multiple connections
32
+ * - Filesystem transparent (can import/export files)
33
+ * - Good balance of performance and compatibility
34
+ * @recommended For apps needing OPFS with multi-connection support
35
+ */
36
+ | 'OPFSCoopSyncVFS'
37
+ /**
38
+ * AccessHandlePoolVFS - OPFS-backed storage (fastest single connection)
39
+ * - Requires Worker context
40
+ * - Single connection only (no multi-tab support)
41
+ * - Best performance, supports WAL mode
42
+ * - NOT filesystem transparent
43
+ * @recommended For single-tab apps where performance is critical
44
+ */
45
+ | 'AccessHandlePoolVFS';
46
+
47
+ /**
48
+ * Options for configuring the WaSqliteDriver.
49
+ */
50
+ export interface WaSqliteDriverOptions {
51
+ /**
52
+ * Virtual File System to use for storage.
53
+ * @default 'IDBBatchAtomicVFS'
54
+ */
55
+ vfs?: WaSqliteVfsType;
56
+
57
+ /**
58
+ * Directory path for the database in OPFS VFS modes.
59
+ * Only used with OPFS-based VFS types.
60
+ * @default '/'
61
+ */
62
+ directory?: string;
63
+
64
+ /**
65
+ * SQLite page size in bytes.
66
+ * Larger pages can improve read performance for large BLOBs.
67
+ * Cannot be changed after database creation for IDBBatchAtomicVFS/IDBMirrorVFS.
68
+ * @default 4096
69
+ */
70
+ pageSize?: number;
71
+
72
+ /**
73
+ * SQLite cache size in pages (negative = KB, positive = pages).
74
+ * Larger cache improves performance but uses more memory.
75
+ * For IDBBatchAtomicVFS, must be large enough to hold journal for batch atomic mode.
76
+ * @default -2000 (2MB)
77
+ */
78
+ cacheSize?: number;
79
+
80
+ /**
81
+ * Enable WAL (Write-Ahead Logging) mode.
82
+ * Only supported with AccessHandlePoolVFS (with locking_mode=exclusive).
83
+ * For other VFS types, this is ignored.
84
+ * @default false
85
+ */
86
+ wal?: boolean;
87
+
88
+ /**
89
+ * Set synchronous pragma for durability vs performance trade-off.
90
+ * - 'full': Maximum durability (default)
91
+ * - 'normal': Relaxed durability, better performance (supported by IDBBatchAtomicVFS, IDBMirrorVFS, OPFSPermutedVFS)
92
+ * - 'off': No sync, fastest but risks data loss on crash
93
+ * @default 'full'
94
+ */
95
+ synchronous?: 'full' | 'normal' | 'off';
96
+ }
97
+
98
+ // Internal VFS interface for lifecycle management
99
+ interface WaSqliteVFS {
100
+ close(): Promise<void>;
101
+ name: string;
102
+ }
103
+
104
+ // VFS class type with static create method
105
+ interface VFSClass {
106
+ create(name: string, module: any, options?: any): Promise<WaSqliteVFS>;
107
+ }
108
+
109
+ // Cached module factory and instance
110
+ let cachedModuleFactory: (() => Promise<any>) | null = null;
111
+ let cachedModule: any = null;
112
+ let cachedSqlite3: any = null;
113
+ // Track VFS instances by name to avoid re-registering
114
+ const registeredVFS = new Map<string, WaSqliteVFS>();
115
+
116
+ /**
117
+ * SQLite driver for web browsers using wa-sqlite with IndexedDB or OPFS persistence.
118
+ * Provides robust, persistent SQLite storage in the browser that prevents data loss.
119
+ *
120
+ * ## Data Safety Features
121
+ *
122
+ * - **IDBBatchAtomicVFS** (default): Uses IndexedDB batch atomic writes to ensure transactions
123
+ * are either fully committed or not at all. Multi-tab safe.
124
+ * - **IDBMirrorVFS**: IndexedDB with in-memory mirror. Much faster, database must fit in RAM.
125
+ * - **OPFSCoopSyncVFS**: OPFS with cooperative sync. Multi-connection, filesystem transparent.
126
+ * - **AccessHandlePoolVFS**: Uses OPFS Access Handles for high performance. Single-tab only.
127
+ * - Full durability by default (`PRAGMA synchronous=full`)
128
+ * - Automatic journal mode configuration for each VFS type
129
+ *
130
+ * ## VFS Selection Guide
131
+ *
132
+ * | VFS | Best For | Multi-Tab | Speed |
133
+ * |-----|----------|-----------|-------|
134
+ * | IDBBatchAtomicVFS | General use, main thread | ✅ | Good |
135
+ * | IDBMirrorVFS | Small DBs, main thread | ✅ | Fast |
136
+ * | OPFSCoopSyncVFS | Web Workers, file export | ✅ | Good |
137
+ * | AccessHandlePoolVFS | Single-tab performance | ❌ | Fastest |
138
+ *
139
+ * @example
140
+ * ```ts
141
+ * import { WaSqliteDriver } from '@anfenn/dync/wa-sqlite';
142
+ * import { SQLiteAdapter } from '@anfenn/dync';
143
+ *
144
+ * // Default: IDBBatchAtomicVFS (works in main thread, multi-tab safe)
145
+ * const driver = new WaSqliteDriver('myapp.db');
146
+ *
147
+ * // For OPFS (faster, requires Worker, filesystem transparent)
148
+ * const opfsDriver = new WaSqliteDriver('myapp.db', { vfs: 'OPFSCoopSyncVFS' });
149
+ *
150
+ * const adapter = new SQLiteAdapter('myapp', driver);
151
+ * ```
152
+ */
153
+ export class WaSqliteDriver implements SQLiteDatabaseDriver {
154
+ readonly type = 'WaSqliteDriver';
155
+ private db: number | null = null;
156
+ private sqlite3: any = null;
157
+ private readonly options: Required<WaSqliteDriverOptions>;
158
+ private opened = false;
159
+ private openPromise: Promise<void> | null = null;
160
+ // Mutex to prevent concurrent database operations (critical for wa-sqlite)
161
+ private executionLock: Promise<void> = Promise.resolve();
162
+ readonly name: string;
163
+
164
+ constructor(databaseName: string, options: WaSqliteDriverOptions = {}) {
165
+ this.name = databaseName;
166
+ this.options = {
167
+ vfs: 'IDBBatchAtomicVFS',
168
+ directory: '/',
169
+ pageSize: 4096,
170
+ cacheSize: -2000,
171
+ wal: false,
172
+ synchronous: 'full',
173
+ ...options,
174
+ };
175
+ }
176
+
177
+ /**
178
+ * Execute a callback with exclusive database access.
179
+ * This prevents concurrent operations which can corrupt the database.
180
+ */
181
+ private async withLock<T>(fn: () => Promise<T>): Promise<T> {
182
+ // Chain onto the existing lock
183
+ const previousLock = this.executionLock;
184
+ let releaseLock: () => void;
185
+ this.executionLock = new Promise<void>((resolve) => {
186
+ releaseLock = resolve;
187
+ });
188
+
189
+ try {
190
+ // Wait for previous operation to complete
191
+ await previousLock;
192
+ // Execute our operation
193
+ return await fn();
194
+ } finally {
195
+ // Release the lock
196
+ releaseLock!();
197
+ }
198
+ }
199
+
200
+ async open(): Promise<void> {
201
+ if (this.opened) return;
202
+ if (this.openPromise) return this.openPromise;
203
+
204
+ this.openPromise = this._open();
205
+
206
+ try {
207
+ await this.openPromise;
208
+ } finally {
209
+ this.openPromise = null;
210
+ }
211
+ }
212
+
213
+ private async _open(): Promise<void> {
214
+ // Load wa-sqlite module (asyncify build for async VFS support)
215
+ const module = await this.loadWasmModule();
216
+
217
+ // Create SQLite API from module (cached - must only create once per module)
218
+ if (!cachedSqlite3) {
219
+ const { Factory } = await import('@journeyapps/wa-sqlite');
220
+ cachedSqlite3 = Factory(module);
221
+ }
222
+ this.sqlite3 = cachedSqlite3;
223
+
224
+ // For IDB-based VFS, the VFS name is also used as the IndexedDB database name
225
+ // Use a unique name based on database name to avoid conflicts
226
+ const vfsName = `dync_${this.options.vfs}_${this.name}`.replace(/[^a-zA-Z0-9_-]/g, '_');
227
+
228
+ // Reuse existing VFS instance or create and register a new one
229
+ let existingVfs = registeredVFS.get(vfsName);
230
+ if (!existingVfs) {
231
+ existingVfs = await this.createVFS(module, vfsName);
232
+ // Register VFS with SQLite as default (like PowerSync does)
233
+ this.sqlite3.vfs_register(existingVfs, true);
234
+ registeredVFS.set(vfsName, existingVfs);
235
+ }
236
+
237
+ // Build database path - for IDB VFS, this is the "file" path within the VFS
238
+ const dbPath = this.buildDatabasePath();
239
+
240
+ // Open database (VFS is registered as default)
241
+ this.db = await this.sqlite3.open_v2(dbPath);
242
+
243
+ // Configure database pragmas for performance and durability
244
+ await this.configurePragmas();
245
+
246
+ this.opened = true;
247
+ }
248
+
249
+ private async loadWasmModule(): Promise<any> {
250
+ if (!cachedModule) {
251
+ if (!cachedModuleFactory) {
252
+ // Dynamically import the asyncify build for async VFS support
253
+ const wasmModule = await import('@journeyapps/wa-sqlite/dist/wa-sqlite-async.mjs');
254
+ cachedModuleFactory = wasmModule.default;
255
+ }
256
+ // Cache the module instance - all VFS and sqlite3 APIs must share the same module
257
+ cachedModule = await cachedModuleFactory();
258
+ }
259
+ return cachedModule;
260
+ }
261
+
262
+ private async createVFS(module: any, vfsName: string): Promise<WaSqliteVFS> {
263
+ const vfsType = this.options.vfs;
264
+ let VFSClass: VFSClass;
265
+ let vfsOptions: any = undefined;
266
+
267
+ // Dynamically import VFS implementation
268
+ // Note: We cast to unknown first because the package types don't include the static create method
269
+ switch (vfsType) {
270
+ case 'IDBBatchAtomicVFS': {
271
+ const mod = await import('@journeyapps/wa-sqlite/src/examples/IDBBatchAtomicVFS.js');
272
+ VFSClass = mod.IDBBatchAtomicVFS as unknown as VFSClass;
273
+ // Use exclusive lock policy like PowerSync does
274
+ vfsOptions = { lockPolicy: 'exclusive' };
275
+ break;
276
+ }
277
+ case 'IDBMirrorVFS': {
278
+ const mod = await import('@journeyapps/wa-sqlite/src/examples/IDBMirrorVFS.js');
279
+ VFSClass = mod.IDBMirrorVFS as unknown as VFSClass;
280
+ break;
281
+ }
282
+ case 'OPFSCoopSyncVFS': {
283
+ const mod = await import('@journeyapps/wa-sqlite/src/examples/OPFSCoopSyncVFS.js');
284
+ VFSClass = mod.OPFSCoopSyncVFS as unknown as VFSClass;
285
+ break;
286
+ }
287
+ case 'AccessHandlePoolVFS': {
288
+ const mod = await import('@journeyapps/wa-sqlite/src/examples/AccessHandlePoolVFS.js');
289
+ VFSClass = mod.AccessHandlePoolVFS as unknown as VFSClass;
290
+ break;
291
+ }
292
+ default:
293
+ throw new Error(`Unsupported VFS type: ${vfsType}`);
294
+ }
295
+
296
+ return VFSClass.create(vfsName, module, vfsOptions);
297
+ }
298
+
299
+ private buildDatabasePath(): string {
300
+ const vfsType = this.options.vfs;
301
+
302
+ // For IDB-based VFS, use database name directly
303
+ if (vfsType === 'IDBBatchAtomicVFS' || vfsType === 'IDBMirrorVFS') {
304
+ return this.name;
305
+ }
306
+
307
+ // For OPFS-based VFS, build full path
308
+ const directory = this.options.directory.replace(/\/$/, '');
309
+ return `${directory}/${this.name}`;
310
+ }
311
+
312
+ private async configurePragmas(): Promise<void> {
313
+ if (!this.db || !this.sqlite3) return;
314
+
315
+ // Page size can only be set on new/empty databases
316
+ // For IDBBatchAtomicVFS, it cannot be changed after creation
317
+ // Try to set it, but ignore errors if the database already exists
318
+ try {
319
+ await this.sqlite3.exec(this.db, `PRAGMA page_size = ${this.options.pageSize}`);
320
+ } catch {
321
+ // Page size already set, ignore
322
+ }
323
+
324
+ // Cache size for performance
325
+ await this.sqlite3.exec(this.db, `PRAGMA cache_size = ${this.options.cacheSize}`);
326
+
327
+ // WAL mode only for AccessHandlePoolVFS with exclusive locking
328
+ if (this.options.wal && this.options.vfs === 'AccessHandlePoolVFS') {
329
+ await this.sqlite3.exec(this.db, 'PRAGMA locking_mode = exclusive');
330
+ await this.sqlite3.exec(this.db, 'PRAGMA journal_mode = WAL');
331
+ }
332
+ // Note: For IDB-based VFS, we don't set journal_mode - let the VFS handle it
333
+ }
334
+
335
+ async close(): Promise<void> {
336
+ if (!this.opened || !this.db || !this.sqlite3) return;
337
+
338
+ // Wait for any pending operations to complete by acquiring the lock
339
+ await this.withLock(async () => {
340
+ // Ensure all data is flushed before closing
341
+ // This is critical for IDB-based VFS which batch writes
342
+ try {
343
+ await this.sqlite3!.exec(this.db!, 'PRAGMA wal_checkpoint(TRUNCATE)');
344
+ } catch {
345
+ // Ignore if WAL mode not enabled
346
+ }
347
+
348
+ await this.sqlite3!.close(this.db!);
349
+
350
+ // Don't close the shared VFS - it may be used by other connections
351
+ // The VFS will be cleaned up when all references are gone
352
+ this.db = null;
353
+ this.opened = false;
354
+ });
355
+ }
356
+
357
+ async execute(statement: string): Promise<void> {
358
+ await this.open();
359
+
360
+ if (!this.db || !this.sqlite3) {
361
+ throw new Error('Database not initialized');
362
+ }
363
+
364
+ await this.withLock(async () => {
365
+ await this.sqlite3.exec(this.db, statement);
366
+ });
367
+ }
368
+
369
+ async run(statement: string, values: unknown[] = []): Promise<SQLiteRunResult> {
370
+ await this.open();
371
+
372
+ if (!this.db || !this.sqlite3) {
373
+ throw new Error('Database not initialized');
374
+ }
375
+
376
+ return this.withLock(async () => {
377
+ // Convert values for SQLite (booleans -> integers)
378
+ const convertedValues = this.convertValues(values);
379
+
380
+ // Use statements() generator with proper binding
381
+ for await (const stmt of this.sqlite3.statements(this.db, statement)) {
382
+ if (stmt === null) {
383
+ break;
384
+ }
385
+
386
+ // Reset statement before binding (critical for wa-sqlite)
387
+ this.sqlite3.reset(stmt);
388
+
389
+ // Bind parameters if any
390
+ if (convertedValues.length > 0) {
391
+ this.sqlite3.bind_collection(stmt, convertedValues);
392
+ }
393
+
394
+ // Execute the statement
395
+ await this.sqlite3.step(stmt);
396
+ }
397
+
398
+ return {
399
+ changes: this.sqlite3.changes(this.db),
400
+ lastId: Number(this.sqlite3.last_insert_id(this.db)),
401
+ };
402
+ });
403
+ }
404
+
405
+ /**
406
+ * Convert values for SQLite compatibility.
407
+ * - Booleans must be converted to integers (SQLite has no boolean type)
408
+ */
409
+ private convertValues(values: unknown[]): unknown[] {
410
+ return values.map((value) => {
411
+ if (typeof value === 'boolean') {
412
+ return value ? 1 : 0;
413
+ }
414
+ return value;
415
+ });
416
+ }
417
+
418
+ async query(statement: string, values: unknown[] = []): Promise<SQLiteQueryResult> {
419
+ await this.open();
420
+
421
+ if (!this.db || !this.sqlite3) {
422
+ throw new Error('Database not initialized');
423
+ }
424
+
425
+ return this.withLock(async () => {
426
+ const { SQLITE_ROW } = await import('@journeyapps/wa-sqlite');
427
+ const allRows: unknown[][] = [];
428
+ let columns: string[] = [];
429
+
430
+ // Convert values for SQLite (booleans -> integers)
431
+ const convertedValues = this.convertValues(values);
432
+
433
+ // Use statements() generator with proper binding
434
+ for await (const stmt of this.sqlite3.statements(this.db, statement)) {
435
+ if (stmt === null) {
436
+ break;
437
+ }
438
+
439
+ // Reset statement before binding (critical for wa-sqlite)
440
+ this.sqlite3.reset(stmt);
441
+
442
+ // Bind parameters if any
443
+ if (convertedValues.length > 0) {
444
+ this.sqlite3.bind_collection(stmt, convertedValues);
445
+ }
446
+
447
+ // Get column names
448
+ if (columns.length === 0) {
449
+ columns = this.sqlite3.column_names(stmt);
450
+ }
451
+
452
+ // Fetch all rows
453
+ while ((await this.sqlite3.step(stmt)) === SQLITE_ROW) {
454
+ const row = this.sqlite3.row(stmt);
455
+ allRows.push(row);
456
+ }
457
+ }
458
+
459
+ return { columns, values: allRows };
460
+ });
461
+ }
462
+
463
+ /**
464
+ * Check if the database is currently open.
465
+ */
466
+ isOpen(): boolean {
467
+ return this.opened;
468
+ }
469
+
470
+ /**
471
+ * Get the VFS type being used by this driver.
472
+ */
473
+ getVfsType(): WaSqliteVfsType {
474
+ return this.options.vfs;
475
+ }
476
+
477
+ /**
478
+ * Delete the database.
479
+ * This will close the database if open and remove all persisted data.
480
+ * For IndexedDB-based VFS, this deletes the IndexedDB database.
481
+ * For OPFS-based VFS, this removes the files from OPFS.
482
+ */
483
+ async delete(): Promise<void> {
484
+ // Close if open
485
+ if (this.opened) {
486
+ await this.close();
487
+ }
488
+
489
+ const vfsType = this.options.vfs;
490
+
491
+ if (vfsType === 'IDBBatchAtomicVFS' || vfsType === 'IDBMirrorVFS') {
492
+ // Delete IndexedDB database
493
+ await new Promise<void>((resolve, reject) => {
494
+ const request = indexedDB.deleteDatabase(this.name);
495
+ request.onsuccess = () => resolve();
496
+ request.onerror = () => reject(request.error);
497
+ request.onblocked = () => {
498
+ console.warn(`Database deletion blocked for ${this.name}. Close all connections and try again.`);
499
+ };
500
+ });
501
+ } else {
502
+ // For OPFS-based VFS, remove the directory
503
+ const dbPath = this.buildDatabasePath();
504
+ const root = await navigator.storage.getDirectory();
505
+
506
+ try {
507
+ // Try to remove the file/directory
508
+ const pathParts = dbPath.split('/').filter(Boolean);
509
+ let current = root;
510
+
511
+ // Navigate to parent directory
512
+ for (let i = 0; i < pathParts.length - 1; i++) {
513
+ current = await current.getDirectoryHandle(pathParts[i]!);
514
+ }
515
+
516
+ // Remove the database file
517
+ const filename = pathParts[pathParts.length - 1]!;
518
+ await current.removeEntry(filename, { recursive: true });
519
+
520
+ // Also try to remove associated journal/wal files
521
+ const associatedFiles = [`${filename}-journal`, `${filename}-wal`, `${filename}-shm`];
522
+ for (const file of associatedFiles) {
523
+ try {
524
+ await current.removeEntry(file, { recursive: false });
525
+ } catch {
526
+ // Ignore if file doesn't exist
527
+ }
528
+ }
529
+ } catch (error) {
530
+ // Ignore if directory doesn't exist
531
+ if ((error as Error).name !== 'NotFoundError') {
532
+ throw error;
533
+ }
534
+ }
535
+ }
536
+ }
537
+ }
@@ -0,0 +1,46 @@
1
+ // Type declarations for @journeyapps/wa-sqlite VFS modules
2
+ // These extend the base VFS class and add static create methods
3
+
4
+ declare module '@journeyapps/wa-sqlite/src/examples/IDBBatchAtomicVFS.js' {
5
+ import * as VFS from '@journeyapps/wa-sqlite/src/VFS.js';
6
+
7
+ export class IDBBatchAtomicVFS extends VFS.Base {
8
+ name: string;
9
+
10
+ static create(name: string, module: any, options?: { durability?: string }): Promise<IDBBatchAtomicVFS>;
11
+ close(): Promise<void>;
12
+ }
13
+ }
14
+
15
+ declare module '@journeyapps/wa-sqlite/src/examples/IDBMirrorVFS.js' {
16
+ import * as VFS from '@journeyapps/wa-sqlite/src/VFS.js';
17
+
18
+ export class IDBMirrorVFS extends VFS.Base {
19
+ name: string;
20
+
21
+ static create(name: string, module: any, options?: { durability?: string }): Promise<IDBMirrorVFS>;
22
+ close(): Promise<void>;
23
+ }
24
+ }
25
+
26
+ declare module '@journeyapps/wa-sqlite/src/examples/OPFSCoopSyncVFS.js' {
27
+ import * as VFS from '@journeyapps/wa-sqlite/src/VFS.js';
28
+
29
+ export class OPFSCoopSyncVFS extends VFS.Base {
30
+ name: string;
31
+
32
+ static create(name: string, module: any): Promise<OPFSCoopSyncVFS>;
33
+ close(): Promise<void>;
34
+ }
35
+ }
36
+
37
+ declare module '@journeyapps/wa-sqlite/src/examples/AccessHandlePoolVFS.js' {
38
+ import * as VFS from '@journeyapps/wa-sqlite/src/VFS.js';
39
+
40
+ export class AccessHandlePoolVFS extends VFS.Base {
41
+ name: string;
42
+
43
+ static create(name: string, module: any): Promise<AccessHandlePoolVFS>;
44
+ close(): Promise<void>;
45
+ }
46
+ }