@ehegnes/wa-sqlite 2.0.0-beta.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 (61) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +78 -0
  3. package/dist/wa-sqlite-async.mjs +2 -0
  4. package/dist/wa-sqlite-async.wasm +0 -0
  5. package/dist/wa-sqlite-jspi.mjs +2 -0
  6. package/dist/wa-sqlite-jspi.wasm +0 -0
  7. package/dist/wa-sqlite.mjs +2 -0
  8. package/dist/wa-sqlite.wasm +0 -0
  9. package/package.json +44 -0
  10. package/src/FacadeVFS.js +681 -0
  11. package/src/VFS.js +222 -0
  12. package/src/WebLocksMixin.js +411 -0
  13. package/src/examples/AccessHandlePoolVFS.js +458 -0
  14. package/src/examples/IDBBatchAtomicVFS.js +827 -0
  15. package/src/examples/IDBMirrorVFS.js +889 -0
  16. package/src/examples/LazyLock.js +90 -0
  17. package/src/examples/Lock.js +69 -0
  18. package/src/examples/MemoryAsyncVFS.js +100 -0
  19. package/src/examples/MemoryVFS.js +176 -0
  20. package/src/examples/OPFSAdaptiveVFS.js +437 -0
  21. package/src/examples/OPFSAnyContextVFS.js +300 -0
  22. package/src/examples/OPFSCoopSyncVFS.js +597 -0
  23. package/src/examples/OPFSPermutedVFS.js +1217 -0
  24. package/src/examples/OPFSWriteAheadVFS.js +960 -0
  25. package/src/examples/README.md +81 -0
  26. package/src/examples/WriteAhead.js +1174 -0
  27. package/src/examples/tag.js +82 -0
  28. package/src/sqlite-api.js +924 -0
  29. package/src/sqlite-constants.js +275 -0
  30. package/src/types/globals.d.ts +60 -0
  31. package/src/types/index.d.ts +1228 -0
  32. package/src/types/tsconfig.json +6 -0
  33. package/test/AccessHandlePoolVFS.test.js +27 -0
  34. package/test/IDBBatchAtomicVFS.test.js +97 -0
  35. package/test/IDBMirrorVFS.test.js +27 -0
  36. package/test/MemoryAsyncVFS.test.js +27 -0
  37. package/test/MemoryVFS.test.js +27 -0
  38. package/test/OPFSAdaptiveVFS.test.js +27 -0
  39. package/test/OPFSAnyContextVFS.test.js +27 -0
  40. package/test/OPFSCoopSyncVFS.test.js +27 -0
  41. package/test/OPFSWriteAheadVFS.test.js +27 -0
  42. package/test/TestContext.js +96 -0
  43. package/test/WebLocksMixin.test.js +521 -0
  44. package/test/api.test.js +49 -0
  45. package/test/api_exec.js +89 -0
  46. package/test/api_misc.js +63 -0
  47. package/test/api_statements.js +447 -0
  48. package/test/callbacks.test.js +581 -0
  49. package/test/data/idbv5.json +1 -0
  50. package/test/sql.test.js +64 -0
  51. package/test/sql_0001.js +49 -0
  52. package/test/sql_0002.js +52 -0
  53. package/test/sql_0003.js +83 -0
  54. package/test/sql_0004.js +81 -0
  55. package/test/sql_0005.js +76 -0
  56. package/test/test-worker.js +204 -0
  57. package/test/vfs_xAccess.js +2 -0
  58. package/test/vfs_xClose.js +52 -0
  59. package/test/vfs_xOpen.js +91 -0
  60. package/test/vfs_xRead.js +38 -0
  61. package/test/vfs_xWrite.js +36 -0
@@ -0,0 +1,82 @@
1
+ import * as SQLite from '../sqlite-api.js';
2
+
3
+ /**
4
+ * @typedef SQLiteResults
5
+ * @property {string[]} columns
6
+ * @property {SQLiteCompatibleType[][]} rows
7
+ */
8
+
9
+ /**
10
+ * Build a query function for a database.
11
+ *
12
+ * The returned function can be invoke in two ways, (1) as a template
13
+ * tag, or (2) as a regular function.
14
+ *
15
+ * When used as a template tag, multiple SQL statements are accepted and
16
+ * string interpolants can be used, e.g.
17
+ * ```
18
+ * const results = await tag`
19
+ * PRAGMA integrity_check;
20
+ * SELECT * FROM ${tblName};
21
+ * `;
22
+ * ```
23
+ *
24
+ * When called as a regular function, only one statement can be used
25
+ * and SQLite placeholder substitution is performed, e.g.
26
+ * ```
27
+ * const results = await tag('INSERT INTO tblName VALUES (?, ?)', [
28
+ * ['foo', 1],
29
+ * ['bar', 17],
30
+ * ['baz', 42]
31
+ * ]);
32
+ * ```
33
+ * @param {SQLiteAPI} sqlite3
34
+ * @param {number} db
35
+ * @returns {(sql: string|TemplateStringsArray, ...values: string[]|SQLiteCompatibleType[][][]) => Promise<SQLiteResults[]>}
36
+ */
37
+ export function createTag(sqlite3, db) {
38
+ // Helper function to execute the query.
39
+ async function execute(sql, bindings) {
40
+ const results = [];
41
+ for await (const stmt of sqlite3.statements(db, sql)) {
42
+ let columns;
43
+ for (const binding of bindings ?? [[]]) {
44
+ sqlite3.reset(stmt);
45
+ if (bindings) {
46
+ sqlite3.bind_collection(stmt, binding);
47
+ }
48
+
49
+ const rows = [];
50
+ while (await sqlite3.step(stmt) === SQLite.SQLITE_ROW) {
51
+ const row = sqlite3.row(stmt);
52
+ rows.push(row);
53
+ }
54
+
55
+ columns = columns ?? sqlite3.column_names(stmt)
56
+ if (columns.length) {
57
+ results.push({ columns, rows });
58
+ }
59
+ }
60
+
61
+ // When binding parameters, only a single statement is executed.
62
+ if (bindings) {
63
+ return results;
64
+ }
65
+ }
66
+ return results;
67
+ }
68
+
69
+ return async function(sql, ...values) {
70
+ if (Array.isArray(sql)) {
71
+ // Tag usage.
72
+ const interleaved = [];
73
+ sql.forEach((s, i) => {
74
+ interleaved.push(s, values[i]);
75
+ });
76
+ return execute(interleaved.join(''));
77
+ } else {
78
+ // Binding usage.
79
+ return execute(sql, values[0]);
80
+ }
81
+ }
82
+ }