@livestore/wa-sqlite 1.0.1-dev.2 → 1.0.1-dev.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@livestore/wa-sqlite",
3
- "version": "1.0.1-dev.2",
3
+ "version": "1.0.1-dev.21",
4
4
  "type": "module",
5
5
  "main": "src/sqlite-api.js",
6
6
  "types": "src/types/index.d.ts",
package/src/sqlite-api.js CHANGED
@@ -249,6 +249,58 @@ export function Factory(Module) {
249
249
  };
250
250
  })();
251
251
 
252
+ const SQLITE_SERIALIZE_NOCOPY = 0x0_01
253
+
254
+ sqlite3.serialize = (function() {
255
+ const fname = 'sqlite3_serialize';
256
+ const f = Module.cwrap(fname, ...decl('nsnn:n'));
257
+ return function(db, schema) {
258
+ verifyDatabase(db);
259
+ const piSize = tmpPtr[0];
260
+ let address = f(db, schema, piSize, 0);
261
+ if (address === 0) {
262
+ address = f(db, schema, piSize, SQLITE_SERIALIZE_NOCOPY);
263
+ const size = Module.getValue(piSize, '*');
264
+ const result = Module.HEAPU8.subarray(address, address + size);
265
+ return new Uint8Array(result.slice());
266
+ } else {
267
+ const size = Module.getValue(piSize, '*');
268
+ const result = Module.HEAPU8.subarray(address, address + size);
269
+ return result;
270
+ }
271
+ };
272
+ })();
273
+
274
+ // sqlite3.serialize = (function() {
275
+ // const fname = 'sqlite3_serialize';
276
+ // const f = Module.cwrap(fname, ...decl('nsnn:n'));
277
+ // return function(db, schema, flags) {
278
+ // verifyDatabase(db);
279
+ // const piSize = tmpPtr[0];
280
+ // const address = f(db, schema, piSize, flags);
281
+ // const size = Module.getValue(piSize, '*');
282
+ // const result = Module.HEAPU8.subarray(address, address + size);
283
+ // return result;
284
+ // };
285
+ // })();
286
+
287
+ sqlite3.backup = (function() {
288
+ const fInit = Module.cwrap('sqlite3_backup_init', ...decl('nsns:n'));
289
+ const fStep = Module.cwrap('sqlite3_backup_step', ...decl('nn:n'));
290
+ const fFinish = Module.cwrap('sqlite3_backup_finish', ...decl('n:n'));
291
+ return function(dest, destName, source, sourceName) {
292
+ verifyDatabase(dest);
293
+ verifyDatabase(source);
294
+ const backup = fInit(dest, destName, source, sourceName);
295
+ if (backup === 0) {
296
+ const errMsg = Module.ccall('sqlite3_errmsg', 'string', ['number'], [dest]);
297
+ throw new SQLiteError(`backup failed: ${errMsg}`, SQLite.SQLITE_ERROR);
298
+ }
299
+ fStep(backup, -1);
300
+ return fFinish(backup);
301
+ };
302
+ })();
303
+
252
304
  sqlite3.clear_bindings = (function() {
253
305
  const fname = 'sqlite3_clear_bindings';
254
306
  const f = Module.cwrap(fname, ...decl('n:n'));
@@ -445,7 +497,8 @@ export function Factory(Module) {
445
497
  // return SQLite.SQLITE_OK;
446
498
  // };
447
499
  sqlite3.exec = function(db, sql, callback) {
448
- for (const stmt of sqlite3.statements(db, sql)) {
500
+ const stmts = sqlite3.statements(db, sql);
501
+ for (const stmt of stmts) {
449
502
  let columns;
450
503
  while (sqlite3.step(stmt) === SQLite.SQLITE_ROW) {
451
504
  if (callback) {
@@ -455,6 +508,9 @@ export function Factory(Module) {
455
508
  }
456
509
  }
457
510
  }
511
+ for (const stmt of stmts) {
512
+ sqlite3.finalize(stmt);
513
+ }
458
514
  return SQLite.SQLITE_OK;
459
515
  };
460
516
 
@@ -510,6 +566,28 @@ export function Factory(Module) {
510
566
  })();
511
567
 
512
568
  sqlite3.open_v2 = (function() {
569
+ const fname = 'sqlite3_open_v2';
570
+ const f = Module.cwrap(fname, ...decl('snnn:n'), { async });
571
+ return async function(zFilename, flags, zVfs) {
572
+ flags = flags || SQLite.SQLITE_OPEN_CREATE | SQLite.SQLITE_OPEN_READWRITE;
573
+ zVfs = createUTF8(zVfs);
574
+ try {
575
+ // Allow retry operations.
576
+ const rc = await retry(() => f(zFilename, tmpPtr[0], flags, zVfs));
577
+
578
+ const db = Module.getValue(tmpPtr[0], '*');
579
+ databases.add(db);
580
+
581
+ Module.ccall('RegisterExtensionFunctions', 'void', ['number'], [db]);
582
+ check(fname, rc);
583
+ return db;
584
+ } finally {
585
+ Module._sqlite3_free(zVfs);
586
+ }
587
+ };
588
+ })();
589
+
590
+ sqlite3.open_v2Sync = (function() {
513
591
  const fname = 'sqlite3_open_v2';
514
592
  const f = Module.cwrap(fname, ...decl('snnn:n'), { async });
515
593
  return function(zFilename, flags, zVfs) {
@@ -532,6 +610,7 @@ export function Factory(Module) {
532
610
  };
533
611
  })();
534
612
 
613
+
535
614
  sqlite3.progress_handler = function(db, nProgressOps, handler, userData) {
536
615
  verifyDatabase(db);
537
616
  Module.progress_handler(db, nProgressOps, handler, userData);
@@ -689,10 +768,10 @@ export function Factory(Module) {
689
768
  // { async: true });
690
769
  { async: false });
691
770
 
692
- const results = [];
771
+ const stmts = [];
693
772
 
694
773
  const onFinally = [];
695
- try {
774
+ // try {
696
775
  // Encode SQL string to UTF-8.
697
776
  const utf8 = new TextEncoder().encode(sql);
698
777
 
@@ -754,16 +833,16 @@ export function Factory(Module) {
754
833
  if (stmt) {
755
834
  mapStmtToDB.set(stmt, db);
756
835
  // yield stmt;
757
- results.push(stmt);
836
+ stmts.push(stmt);
758
837
  }
759
838
  } while (stmt);
760
- } finally {
761
- while (onFinally.length) {
762
- onFinally.pop()();
763
- }
764
- }
839
+ // } finally {
840
+ // while (onFinally.length) {
841
+ // onFinally.pop()();
842
+ // }
843
+ // }
765
844
 
766
- return results;
845
+ return stmts;
767
846
  };
768
847
 
769
848
  sqlite3.step = (function() {
@@ -884,22 +963,22 @@ export function Factory(Module) {
884
963
  // This function is used to automatically retry failed calls that
885
964
  // have pending retry operations that should allow the retry to
886
965
  // succeed.
887
- // async function retry(f) {
888
- // let rc;
889
- // do {
890
- // // Wait for all pending retry operations to complete. This is
891
- // // normally empty on the first loop iteration.
892
- // if (Module.retryOps.length) {
893
- // await Promise.all(Module.retryOps);
894
- // Module.retryOps = [];
895
- // }
966
+ async function retry(f) {
967
+ let rc;
968
+ do {
969
+ // Wait for all pending retry operations to complete. This is
970
+ // normally empty on the first loop iteration.
971
+ if (Module.retryOps.length) {
972
+ await Promise.all(Module.retryOps);
973
+ Module.retryOps = [];
974
+ }
896
975
 
897
- // rc = await f();
976
+ rc = await f();
898
977
 
899
- // // Retry on failure with new pending retry operations.
900
- // } while (rc && Module.retryOps.length);
901
- // return rc;
902
- // }
978
+ // Retry on failure with new pending retry operations.
979
+ } while (rc && Module.retryOps.length);
980
+ return rc;
981
+ }
903
982
 
904
983
  return sqlite3;
905
984
  }
@@ -33,7 +33,7 @@ type SQLiteCompatibleType = number|string|Uint8Array|Array<number>|bigint|null;
33
33
  * @see https://sqlite.org/vfs.html
34
34
  * @see https://sqlite.org/c3ref/io_methods.html
35
35
  */
36
- declare interface SQLiteVFS {
36
+ interface SQLiteVFS {
37
37
  /** Maximum length of a file path in UTF-8 bytes (default 64) */
38
38
  mxPathName?: number;
39
39
 
@@ -117,11 +117,11 @@ declare interface SQLiteVFS {
117
117
  }
118
118
 
119
119
  /**
120
- * Options object argument for {@link SQLiteAPI.statements}
120
+ * Options object argument for {@link SQLiteAPI['statements']}
121
121
  */
122
122
  declare interface SQLitePrepareOptions {
123
123
  /**
124
- * Statement handles prepared and yielded by {@link SQLiteAPI.statements}
124
+ * Statement handles prepared and yielded by {@link SQLiteAPI['statements']}
125
125
  * are normally valid only within the scope of an iteration.
126
126
  * Set `unscoped` to `true` to give iterated statements an arbitrary
127
127
  * lifetime.
@@ -178,7 +178,7 @@ declare interface SQLitePrepareOptions {
178
178
  *
179
179
  * @see https://sqlite.org/c3ref/funclist.html
180
180
  */
181
- declare interface SQLiteAPI {
181
+ interface SQLiteAPI {
182
182
  /**
183
183
  * Bind a collection of values to a statement
184
184
  *
@@ -341,7 +341,6 @@ declare interface SQLiteAPI {
341
341
  * unsigned mFlags Zero or more SQLITE_DESERIALIZE_* flags
342
342
  * );
343
343
  */
344
-
345
344
  deserialize(
346
345
  db: number,
347
346
  zSchema: string,
@@ -351,6 +350,51 @@ declare interface SQLiteAPI {
351
350
  mFlags: number
352
351
  ): number;
353
352
 
353
+ /**
354
+ * unsigned char *sqlite3_serialize(
355
+ * sqlite3 *db, The database connection
356
+ * const char *zSchema, Which DB to serialize. ex: "main", "temp", ...
357
+ * sqlite3_int64 *piSize, Write size of the DB here, if not NULL
358
+ * unsigned int mFlags Zero or more SQLITE_SERIALIZE_* flags
359
+ * );
360
+ */
361
+ serialize(
362
+ db: number,
363
+ zSchema: string,
364
+ // mFlags: number
365
+ ): Uint8Array;
366
+
367
+ // sqlite3_backup *sqlite3_backup_init(
368
+ // sqlite3 *pDest, /* Destination database handle */
369
+ // const char *zDestName, /* Destination database name */
370
+ // sqlite3 *pSource, /* Source database handle */
371
+ // const char *zSourceName /* Source database name */
372
+ // );
373
+ // int sqlite3_backup_step(sqlite3_backup *p, int nPage);
374
+ // int sqlite3_backup_finish(sqlite3_backup *p);
375
+ // int sqlite3_backup_remaining(sqlite3_backup *p);
376
+ // int sqlite3_backup_pagecount(sqlite3_backup *p);
377
+
378
+ backup_init(
379
+ pDest: number,
380
+ zDestName: string,
381
+ pSource: number,
382
+ zSourceName: string
383
+ ): number;
384
+
385
+ backup_step(p: number, nPage: number): number;
386
+ backup_finish(p: number): number;
387
+ backup_remaining(p: number): number;
388
+ backup_pagecount(p: number): number;
389
+
390
+ /**
391
+ * Combines the `backup_init`, `backup_step`, and `backup_finish` functions.
392
+ * destName/sourceName is usually "main" or "temp".
393
+ *
394
+ * @returns `SQLITE_OK` (throws exception on error)
395
+ */
396
+ backup(pDest: number, zDestName: string, pSource: number, zSourceName: string): number;
397
+
354
398
  /**
355
399
  * Reset all bindings on a prepared statement.
356
400
  * @see https://www.sqlite.org/c3ref/clear_bindings.html
@@ -599,6 +643,12 @@ declare interface SQLiteAPI {
599
643
  zVfs?: string
600
644
  ): Promise<number>;
601
645
 
646
+ open_v2Sync(
647
+ zFilename: string,
648
+ iFlags?: number,
649
+ zVfs?: string
650
+ ): number;
651
+
602
652
  /**
603
653
  * Specify callback to be invoked between long-running queries
604
654
  *
@@ -617,9 +667,9 @@ declare interface SQLiteAPI {
617
667
  * Reset a prepared statement object
618
668
  * @see https://www.sqlite.org/c3ref/reset.html
619
669
  * @param stmt prepared statement pointer
620
- * @returns Promise-wrapped `SQLITE_OK` (rejects on error)
670
+ * @returns `SQLITE_OK` (rejects on error)
621
671
  */
622
- reset(stmt: number): Promise<number>;
672
+ reset(stmt: number): number;
623
673
 
624
674
  /**
625
675
  * Convenience function to call `result_*` based of the type of `value`
@@ -746,7 +796,7 @@ declare interface SQLiteAPI {
746
796
  * @param options
747
797
  */
748
798
  // statements(db: number, sql: string, options?: SQLitePrepareOptions): AsyncIterable<number>;
749
- statements(db: number, sql: string, options?: SQLitePrepareOptions): Iterable<number>;
799
+ statements(db: number, sql: string, options?: SQLitePrepareOptions): ReadonlyArray<number>;
750
800
 
751
801
  /**
752
802
  * Evaluate an SQL statement
@@ -843,6 +893,11 @@ declare interface SQLiteAPI {
843
893
  vfs_register(vfs: SQLiteVFS, makeDefault?: boolean): number;
844
894
  }
845
895
 
896
+ declare module '@livestore/wa-sqlite' {
897
+ export type SQLiteAPI_ = SQLiteAPI;
898
+ export type SQLiteVFS_ = SQLiteVFS;
899
+ }
900
+
846
901
  /** @ignore */
847
902
  declare module '@livestore/wa-sqlite/src/sqlite-constants.js' {
848
903
  export const SQLITE_OK: 0;
@@ -1115,6 +1170,7 @@ declare module '@livestore/wa-sqlite/src/VFS.js' {
1115
1170
  export * from '@livestore/wa-sqlite/src/sqlite-constants.js';
1116
1171
 
1117
1172
  export class Base {
1173
+ name: string;
1118
1174
  mxPathName: number;
1119
1175
  /**
1120
1176
  * @param {number} fileId
@@ -1226,6 +1282,28 @@ declare module '@livestore/wa-sqlite/src/VFS.js' {
1226
1282
  }
1227
1283
  }
1228
1284
 
1285
+ /** @ignore */
1286
+ declare module '@livestore/wa-sqlite/src/FacadeVFS.js' {
1287
+ export type FacadeVFS = any
1288
+ }
1289
+
1290
+
1291
+ declare module '@livestore/wa-sqlite/src/examples/AccessHandlePoolVFS.js' {
1292
+ import * as VFS from "@livestore/wa-sqlite/src/VFS.js";
1293
+ export class AccessHandlePoolVFS extends VFS.Base {
1294
+ constructor(name: string, module: any)
1295
+ static create(name: string, module: any): Promise<AccessHandlePoolVFS>;
1296
+ }
1297
+ }
1298
+
1299
+ declare module '@livestore/wa-sqlite/src/examples/OPFSCoopSyncVFS.js' {
1300
+ import * as VFS from "@livestore/wa-sqlite/src/VFS.js";
1301
+ export class OPFSCoopSyncVFS extends VFS.Base {
1302
+ constructor(name: string, module: any)
1303
+ static create(name: string, module: any): Promise<OPFSCoopSyncVFS>;
1304
+ }
1305
+ }
1306
+
1229
1307
  /** @ignore */
1230
1308
  declare module '@livestore/wa-sqlite/src/examples/IndexedDbVFS.js' {
1231
1309
  import * as VFS from "@livestore/wa-sqlite/src/VFS.js";