@livestore/wa-sqlite 1.0.1-dev.1 → 1.0.1-dev.11

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.1",
3
+ "version": "1.0.1-dev.11",
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
@@ -237,6 +237,31 @@ export function Factory(Module) {
237
237
  };
238
238
  })();
239
239
 
240
+ sqlite3.deserialize = (function() {
241
+ const fname = 'sqlite3_deserialize';
242
+ const f = Module.cwrap(fname, ...decl('nnnnnn:n'));
243
+ return function(db, schema, data, szDb, szBuf, flags) {
244
+ verifyDatabase(db);
245
+ const ptr = Module._sqlite3_malloc(szDb);
246
+ Module.HEAPU8.subarray(ptr).set(data);
247
+ const result = f(db, schema, ptr, szDb, szBuf, flags);
248
+ return result;
249
+ };
250
+ })();
251
+
252
+ sqlite3.serialize = (function() {
253
+ const fname = 'sqlite3_serialize';
254
+ const f = Module.cwrap(fname, ...decl('nsnn:n'));
255
+ return function(db, schema, piSize, flags) {
256
+ verifyDatabase(db);
257
+ // TODO alloc + free memory for result?
258
+ const address = f(db, schema, piSize, flags);
259
+ const result = Module.HEAPU8.subarray(address, address + piSize);
260
+
261
+ return result;
262
+ };
263
+ })();
264
+
240
265
  sqlite3.clear_bindings = (function() {
241
266
  const fname = 'sqlite3_clear_bindings';
242
267
  const f = Module.cwrap(fname, ...decl('n:n'));
@@ -433,7 +458,8 @@ export function Factory(Module) {
433
458
  // return SQLite.SQLITE_OK;
434
459
  // };
435
460
  sqlite3.exec = function(db, sql, callback) {
436
- for (const stmt of sqlite3.statements(db, sql)) {
461
+ const { stmts, finalize } = sqlite3.statements(db, sql);
462
+ for (const stmt of stmts) {
437
463
  let columns;
438
464
  while (sqlite3.step(stmt) === SQLite.SQLITE_ROW) {
439
465
  if (callback) {
@@ -443,6 +469,7 @@ export function Factory(Module) {
443
469
  }
444
470
  }
445
471
  }
472
+ finalize();
446
473
  return SQLite.SQLITE_OK;
447
474
  };
448
475
 
@@ -677,10 +704,10 @@ export function Factory(Module) {
677
704
  // { async: true });
678
705
  { async: false });
679
706
 
680
- const results = [];
707
+ const stmts = [];
681
708
 
682
709
  const onFinally = [];
683
- try {
710
+ // try {
684
711
  // Encode SQL string to UTF-8.
685
712
  const utf8 = new TextEncoder().encode(sql);
686
713
 
@@ -742,16 +769,22 @@ export function Factory(Module) {
742
769
  if (stmt) {
743
770
  mapStmtToDB.set(stmt, db);
744
771
  // yield stmt;
745
- results.push(stmt);
772
+ stmts.push(stmt);
746
773
  }
747
774
  } while (stmt);
748
- } finally {
775
+ // } finally {
776
+ // while (onFinally.length) {
777
+ // onFinally.pop()();
778
+ // }
779
+ // }
780
+
781
+ const finalize = () => {
749
782
  while (onFinally.length) {
750
783
  onFinally.pop()();
751
784
  }
752
785
  }
753
786
 
754
- return results;
787
+ return { stmts, finalize };
755
788
  };
756
789
 
757
790
  sqlite3.step = (function() {
@@ -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
  *
@@ -330,6 +330,41 @@ declare interface SQLiteAPI {
330
330
  */
331
331
  changes(db): number;
332
332
 
333
+ /**
334
+ * https://www.sqlite.org/c3ref/deserialize.html
335
+ * int sqlite3_deserialize(
336
+ * sqlite3 *db, The database connection
337
+ * const char *zSchema, Which DB to reopen with the deserialization
338
+ * unsigned char *pData, The serialized database content
339
+ * sqlite3_int64 szDb, Number bytes in the deserialization
340
+ * sqlite3_int64 szBuf, Total size of buffer pData[]
341
+ * unsigned mFlags Zero or more SQLITE_DESERIALIZE_* flags
342
+ * );
343
+ */
344
+ deserialize(
345
+ db: number,
346
+ zSchema: string,
347
+ pData: Uint8Array,
348
+ szDb: number,
349
+ szBuf: number,
350
+ mFlags: number
351
+ ): number;
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
+ piSize: number,
365
+ mFlags: number
366
+ ): Uint8Array;
367
+
333
368
  /**
334
369
  * Reset all bindings on a prepared statement.
335
370
  * @see https://www.sqlite.org/c3ref/clear_bindings.html
@@ -576,7 +611,7 @@ declare interface SQLiteAPI {
576
611
  zFilename: string,
577
612
  iFlags?: number,
578
613
  zVfs?: string
579
- ): Promise<number>;
614
+ ): number;
580
615
 
581
616
  /**
582
617
  * Specify callback to be invoked between long-running queries
@@ -596,9 +631,9 @@ declare interface SQLiteAPI {
596
631
  * Reset a prepared statement object
597
632
  * @see https://www.sqlite.org/c3ref/reset.html
598
633
  * @param stmt prepared statement pointer
599
- * @returns Promise-wrapped `SQLITE_OK` (rejects on error)
634
+ * @returns `SQLITE_OK` (rejects on error)
600
635
  */
601
- reset(stmt: number): Promise<number>;
636
+ reset(stmt: number): number;
602
637
 
603
638
  /**
604
639
  * Convenience function to call `result_*` based of the type of `value`
@@ -725,7 +760,7 @@ declare interface SQLiteAPI {
725
760
  * @param options
726
761
  */
727
762
  // statements(db: number, sql: string, options?: SQLitePrepareOptions): AsyncIterable<number>;
728
- statements(db: number, sql: string, options?: SQLitePrepareOptions): Iterable<number>;
763
+ statements(db: number, sql: string, options?: SQLitePrepareOptions): { stmts: ReadonlyArray<number>; finalize: () => void; };
729
764
 
730
765
  /**
731
766
  * Evaluate an SQL statement
@@ -822,6 +857,11 @@ declare interface SQLiteAPI {
822
857
  vfs_register(vfs: SQLiteVFS, makeDefault?: boolean): number;
823
858
  }
824
859
 
860
+ declare module '@livestore/wa-sqlite' {
861
+ export type SQLiteAPI_ = SQLiteAPI;
862
+ export type SQLiteVFS_ = SQLiteVFS;
863
+ }
864
+
825
865
  /** @ignore */
826
866
  declare module '@livestore/wa-sqlite/src/sqlite-constants.js' {
827
867
  export const SQLITE_OK: 0;
@@ -1094,6 +1134,7 @@ declare module '@livestore/wa-sqlite/src/VFS.js' {
1094
1134
  export * from '@livestore/wa-sqlite/src/sqlite-constants.js';
1095
1135
 
1096
1136
  export class Base {
1137
+ name: string;
1097
1138
  mxPathName: number;
1098
1139
  /**
1099
1140
  * @param {number} fileId
@@ -1205,9 +1246,16 @@ declare module '@livestore/wa-sqlite/src/VFS.js' {
1205
1246
  }
1206
1247
  }
1207
1248
 
1249
+ declare module '@livestore/wa-sqlite/src/examples/AccessHandlePoolVFS.js' {
1250
+ import * as VFS from "@livestore/wa-sqlite/src/VFS.js";
1251
+ export class AccessHandlePoolVFS extends VFS.Base {
1252
+ constructor(name: string, module: any)
1253
+ }
1254
+ }
1255
+
1208
1256
  /** @ignore */
1209
1257
  declare module '@livestore/wa-sqlite/src/examples/IndexedDbVFS.js' {
1210
- import * as VFS from "wa-sqlite/src/VFS.js";
1258
+ import * as VFS from "@livestore/wa-sqlite/src/VFS.js";
1211
1259
  export class IndexedDbVFS extends VFS.Base {
1212
1260
  /**
1213
1261
  * @param {string} idbName Name of IndexedDB database.
@@ -1261,7 +1309,7 @@ declare module '@livestore/wa-sqlite/src/examples/IndexedDbVFS.js' {
1261
1309
 
1262
1310
  /** @ignore */
1263
1311
  declare module '@livestore/wa-sqlite/src/examples/MemoryVFS.js' {
1264
- import * as VFS from "wa-sqlite/src/VFS.js";
1312
+ import * as VFS from "@livestore/wa-sqlite/src/VFS.js";
1265
1313
  /** @ignore */
1266
1314
  export class MemoryVFS extends VFS.Base {
1267
1315
  name: string;
@@ -1272,7 +1320,7 @@ declare module '@livestore/wa-sqlite/src/examples/MemoryVFS.js' {
1272
1320
 
1273
1321
  /** @ignore */
1274
1322
  declare module '@livestore/wa-sqlite/src/examples/MemoryAsyncVFS.js' {
1275
- import { MemoryVFS } from "wa-sqlite/src/examples/MemoryVFS.js";
1323
+ import { MemoryVFS } from "@livestore/wa-sqlite/src/examples/MemoryVFS.js";
1276
1324
  export class MemoryAsyncVFS extends MemoryVFS {
1277
1325
  }
1278
1326
  }