@procedures/sqlite 0.60.1 → 0.62.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 (60) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/README.md +84 -189
  3. package/dist/engines.d.ts +31 -34
  4. package/dist/engines.d.ts.map +1 -1
  5. package/dist/engines.js +35 -95
  6. package/dist/engines.js.map +1 -1
  7. package/dist/index.d.ts +3 -5
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +3 -7
  10. package/dist/index.js.map +1 -1
  11. package/dist/load.d.ts +9 -0
  12. package/dist/load.d.ts.map +1 -0
  13. package/dist/load.js +11 -0
  14. package/dist/load.js.map +1 -0
  15. package/dist/module.d.ts.map +1 -1
  16. package/dist/module.js +104 -62
  17. package/dist/module.js.map +1 -1
  18. package/dist/procedures/db/backup.d.ts +5 -7
  19. package/dist/procedures/db/backup.d.ts.map +1 -1
  20. package/dist/procedures/db/backup.js +22 -27
  21. package/dist/procedures/db/backup.js.map +1 -1
  22. package/dist/procedures/db/migrate.d.ts +1 -1
  23. package/dist/procedures/db/migrate.d.ts.map +1 -1
  24. package/dist/procedures/db/migrate.js +32 -38
  25. package/dist/procedures/db/migrate.js.map +1 -1
  26. package/dist/procedures/db/schema.d.ts.map +1 -1
  27. package/dist/procedures/db/schema.js +8 -10
  28. package/dist/procedures/db/schema.js.map +1 -1
  29. package/dist/procedures/db/shared.d.ts +75 -10
  30. package/dist/procedures/db/shared.d.ts.map +1 -1
  31. package/dist/procedures/db/shared.js +148 -53
  32. package/dist/procedures/db/shared.js.map +1 -1
  33. package/dist/procedures/db/tables.d.ts.map +1 -1
  34. package/dist/procedures/db/tables.js +5 -7
  35. package/dist/procedures/db/tables.js.map +1 -1
  36. package/dist/procedures/db/transaction.d.ts +3 -4
  37. package/dist/procedures/db/transaction.d.ts.map +1 -1
  38. package/dist/procedures/db/transaction.js +18 -26
  39. package/dist/procedures/db/transaction.js.map +1 -1
  40. package/dist/storage.d.ts +18 -30
  41. package/dist/storage.d.ts.map +1 -1
  42. package/dist/storage.js +32 -118
  43. package/dist/storage.js.map +1 -1
  44. package/dist/types.d.ts +1 -1
  45. package/dist/types.d.ts.map +1 -1
  46. package/dist/types.js +1 -1
  47. package/dist/types.js.map +1 -1
  48. package/package.json +8 -10
  49. package/src/engines.ts +50 -158
  50. package/src/index.ts +3 -31
  51. package/src/load.ts +15 -0
  52. package/src/module.ts +128 -84
  53. package/src/procedures/db/backup.ts +21 -27
  54. package/src/procedures/db/migrate.ts +35 -40
  55. package/src/procedures/db/schema.ts +10 -12
  56. package/src/procedures/db/shared.ts +178 -50
  57. package/src/procedures/db/tables.ts +5 -7
  58. package/src/procedures/db/transaction.ts +17 -24
  59. package/src/storage.ts +39 -137
  60. package/src/types.ts +2 -2
package/src/engines.ts CHANGED
@@ -1,66 +1,40 @@
1
1
  /**
2
- * The two SQLite engines of `SqliteStorage`: `node:sqlite` and sql.js. Each engine gives a
3
- * `SqlConnection` of `@mark1russell7/sqlite`, so the storage has one set of queries and uses the
4
- * transactions of that package.
2
+ * The SQLite engine of `SqliteStorage`: `openDatabase` of `@mark1russell7/sqlite`, on the module
3
+ * `node:sqlite` of Node.js. It gives a `SqlConnection` of that package, so the storage uses its
4
+ * transactions. It writes only the changed pages of the file, and SQLite locks the file for each
5
+ * write.
5
6
  *
6
- * - The engine `node` is `openDatabase` of `@mark1russell7/sqlite`, on the module `node:sqlite` of
7
- * Node.js. It writes only the changed pages of the file, and SQLite locks the file for each write.
8
- * - The engine `sqljs` is sql.js (SQLite in WebAssembly), through `@mark1russell7/docker-sqlite`
9
- * and the adapter `sqlJsConnection`. It keeps the database in memory and writes the whole file
10
- * after a write. It is deprecated: its first open in a process shows a `DeprecationWarning`.
11
- * - The two engines read and write the same file format.
7
+ * Version 0.61.0 removed the engine `sqljs` (sql.js, SQLite in WebAssembly). A file that it wrote
8
+ * opens with this engine, with its items and its versions.
12
9
  *
13
10
  * This module loads the main entry of `@mark1russell7/sqlite`, and with it `node:sqlite`, only
14
11
  * when a storage opens. An import of this package does not load them.
15
12
  */
16
13
 
17
- import { existsSync, statSync } from "node:fs";
18
- import { createConnection } from "@mark1russell7/docker-sqlite";
19
- import { sqlJsConnection, type SqlConnection, type SqlStatement } from "@mark1russell7/sqlite/portable";
14
+ import { existsSync } from "node:fs";
15
+ import type { SqlConnection, SqlStatement } from "@mark1russell7/sqlite/portable";
16
+ import { loadSqlite, type Sqlite } from "./load.js";
20
17
 
21
18
  /** The path of a database that is only in memory. */
22
19
  export const MEMORY_PATH = ":memory:";
23
20
 
24
- /**
25
- * The engine of a database: `node:sqlite` (`node`) or sql.js (`sqljs`). The engine `sqljs` is
26
- * deprecated. Version 0.61.0 removes it and needs Node.js 22.13 or later.
27
- */
28
- export type SqliteEngineName = "node" | "sqljs";
21
+ /** The engine of a database: `node:sqlite` (`node`). Version 0.61.0 removed the engine `sqljs`. */
22
+ export type SqliteEngineName = "node";
29
23
 
30
- /**
31
- * The engine choice of a `SqliteStorage`. With `auto`, the storage uses `node` when Node.js has
32
- * `node:sqlite`, and the deprecated engine `sqljs` when it does not.
33
- */
24
+ /** The engine choice of a `SqliteStorage`. `auto` and `node` both use `node:sqlite`. */
34
25
  export type SqliteEngine = "auto" | SqliteEngineName;
35
26
 
36
- /** The code of the deprecation warning of the engine `sqljs`. */
37
- const SQLJS_DEPRECATION_CODE = "PROCEDURES_SQLITE_SQLJS";
38
-
39
- let sqlJsWarned = false;
40
-
41
- /**
42
- * This function shows the deprecation warning of the engine `sqljs`, one time in a process. The
43
- * flag `--no-deprecation` of Node.js hides it.
44
- */
45
- function warnSqlJs(engine: SqliteEngine): void {
46
- if (sqlJsWarned) return;
47
- sqlJsWarned = true;
48
- const cause =
49
- engine === "sqljs"
50
- ? "The SQLite engine sqljs is deprecated"
51
- : `This Node.js (${process.version}) has no node:sqlite, so the engine auto uses the deprecated engine sqljs`;
52
- process.emitWarning(
53
- `${cause}. Version 0.61.0 of @procedures/sqlite removes it: use Node.js 22.13 or later, with the engine node or auto.`,
54
- { type: "DeprecationWarning", code: SQLJS_DEPRECATION_CODE },
55
- );
56
- }
57
-
58
- const ENGINES: ReadonlySet<string> = new Set<SqliteEngine>(["auto", "node", "sqljs"]);
27
+ const ENGINES: ReadonlySet<string> = new Set<SqliteEngine>(["auto", "node"]);
59
28
 
60
29
  /** This function gives the engine choice when it is known. Else it throws a `TypeError`. */
61
30
  export function checkEngine(engine: unknown): SqliteEngine {
31
+ if (engine === "sqljs") {
32
+ throw new TypeError(
33
+ "The SQLite engine sqljs was removed in version 0.61.0: use auto or node. A file of the engine sqljs opens with the engine node.",
34
+ );
35
+ }
62
36
  if (typeof engine !== "string" || !ENGINES.has(engine)) {
63
- throw new TypeError(`Unknown SQLite engine ${JSON.stringify(engine)}: use auto, node or sqljs`);
37
+ throw new TypeError(`Unknown SQLite engine ${JSON.stringify(engine)}: use auto or node`);
64
38
  }
65
39
  return engine as SqliteEngine;
66
40
  }
@@ -68,7 +42,7 @@ export function checkEngine(engine: unknown): SqliteEngine {
68
42
  /** A value that a statement takes or gives. */
69
43
  export type SqlValue = string | number | null;
70
44
 
71
- /** A connection of an engine: a `SqlConnection` that closes. */
45
+ /** A connection of the engine: a `SqlConnection` that closes. */
72
46
  type EngineConnection = SqlConnection & { close(): void };
73
47
 
74
48
  /** The calls of an open database that a storage uses. Each call is synchronous. */
@@ -86,7 +60,7 @@ export interface EngineDatabase {
86
60
  close(): void;
87
61
  }
88
62
 
89
- /** A database of either engine. It keeps one prepared statement for each SQL text. */
63
+ /** A database of the engine. It keeps one prepared statement for each SQL text. */
90
64
  class ConnectionDatabase implements EngineDatabase {
91
65
  readonly connection: EngineConnection;
92
66
 
@@ -128,23 +102,16 @@ class ConnectionDatabase implements EngineDatabase {
128
102
  }
129
103
  }
130
104
 
131
- // ---------------------------------------------------------------------------
132
- // node:sqlite, through @mark1russell7/sqlite
133
- // ---------------------------------------------------------------------------
134
-
135
- /** The part of `@mark1russell7/sqlite` that the engine `node` uses. */
136
- type NodeSqlite = Pick<typeof import("@mark1russell7/sqlite"), "openDatabase">;
137
-
138
- let nodeModule: Promise<NodeSqlite | undefined> | undefined;
105
+ let nodeModule: Promise<Sqlite | undefined> | undefined;
139
106
 
140
107
  /**
141
108
  * This function loads `@mark1russell7/sqlite` one time, and opens a database in memory as a test.
142
109
  * The promise gives undefined when the import or the test fails. Node.js before 22.13 has
143
110
  * `node:sqlite` only with the flag `--experimental-sqlite`.
144
111
  */
145
- function loadNodeSqlite(): Promise<NodeSqlite | undefined> {
146
- nodeModule ??= import("@mark1russell7/sqlite")
147
- .then((module: NodeSqlite) => {
112
+ function loadNodeSqlite(): Promise<Sqlite | undefined> {
113
+ nodeModule ??= loadSqlite()
114
+ .then((module) => {
148
115
  module.openDatabase(MEMORY_PATH, { wal: false }).close();
149
116
  return module;
150
117
  })
@@ -153,8 +120,8 @@ function loadNodeSqlite(): Promise<NodeSqlite | undefined> {
153
120
  }
154
121
 
155
122
  /**
156
- * This function gives true when this Node.js has `node:sqlite` without a flag (Node.js 22.13 or
157
- * later). Then the engine `auto` uses it.
123
+ * This function gives true when this Node.js has `node:sqlite` without a flag. Node.js 22.13 and
124
+ * later have it, and the package needs Node.js 22.13 or later.
158
125
  */
159
126
  export async function isNodeSqliteAvailable(): Promise<boolean> {
160
127
  return (await loadNodeSqlite()) !== undefined;
@@ -167,7 +134,7 @@ export async function isNodeSqliteAvailable(): Promise<boolean> {
167
134
  * busy timeout makes a write wait for the lock of another process. In WAL mode, `NORMAL` keeps the
168
135
  * file consistent after a crash, with fewer disk syncs.
169
136
  */
170
- function openNodeFile(sqlite: NodeSqlite, path: string, busyTimeoutMs: number): ConnectionDatabase {
137
+ function openNodeFile(sqlite: Sqlite, path: string, busyTimeoutMs: number): ConnectionDatabase {
171
138
  return new ConnectionDatabase(
172
139
  sqlite.openDatabase(path, {
173
140
  wal: "when-writable",
@@ -179,53 +146,35 @@ function openNodeFile(sqlite: NodeSqlite, path: string, busyTimeoutMs: number):
179
146
  }
180
147
 
181
148
  /** This function opens a database in memory with `node:sqlite`. */
182
- function openNodeMemory(sqlite: NodeSqlite): ConnectionDatabase {
149
+ function openNodeMemory(sqlite: Sqlite): ConnectionDatabase {
183
150
  return new ConnectionDatabase(sqlite.openDatabase(MEMORY_PATH, { wal: false }));
184
151
  }
185
152
 
186
- // ---------------------------------------------------------------------------
187
- // An open database of a path, with the file work of its engine
188
- // ---------------------------------------------------------------------------
189
-
190
153
  /**
191
- * This function copies the schema of the storages from one database to another. The node engine
154
+ * This function copies the schema of the storages from one database to another. The engine
192
155
  * calls it when it makes the file at the first write.
193
156
  */
194
157
  export type CopySchema = (from: EngineDatabase, to: EngineDatabase) => void;
195
158
 
196
- /** An open database of a path, with the file work of its engine. */
197
- export interface OpenDatabase {
198
- readonly engine: SqliteEngineName;
199
- /** The current database. The node engine changes it when it opens the file. */
200
- readonly db: EngineDatabase;
201
- /**
202
- * True when the node engine waits for the first write to make the file. Until then, the
203
- * database is in memory, and it has the tables and no item.
204
- */
205
- readonly waiting: boolean;
206
- /** This method opens the file and copies the schema to it. The node engine uses it before the first write. */
207
- makeFile(copy: CopySchema): void;
208
- /** This method opens a file that another process made after the open, before a read. */
209
- refresh(copy: CopySchema): void;
210
- /** This method writes the database to the file. Only sql.js has a write to do. */
211
- saveFile(): Promise<void>;
212
- close(): void;
213
- }
214
-
215
159
  /**
216
- * A database of `node:sqlite`. A missing file stays missing until the first write that changes
217
- * an item: until then, the database is in memory.
160
+ * An open database of a path. A missing file stays missing until the first write that changes an
161
+ * item: until then, the database is in memory.
218
162
  */
219
- class NodeOpenDatabase implements OpenDatabase {
220
- private readonly sqlite: NodeSqlite;
163
+ export class OpenDatabase {
164
+ private readonly sqlite: Sqlite;
221
165
  private readonly path: string;
222
166
  private readonly busyTimeoutMs: number;
223
167
 
224
- readonly engine = "node";
168
+ readonly engine: SqliteEngineName = "node";
169
+ /** The current database. It changes when the storage makes the file. */
225
170
  db: EngineDatabase;
171
+ /**
172
+ * True while the storage waits for the first write to make the file. Until then, the database
173
+ * is in memory, and it has the tables and no item.
174
+ */
226
175
  waiting: boolean;
227
176
 
228
- constructor(sqlite: NodeSqlite, path: string, busyTimeoutMs: number) {
177
+ constructor(sqlite: Sqlite, path: string, busyTimeoutMs: number) {
229
178
  this.sqlite = sqlite;
230
179
  this.path = path;
231
180
  this.busyTimeoutMs = busyTimeoutMs;
@@ -241,6 +190,7 @@ class NodeOpenDatabase implements OpenDatabase {
241
190
  }
242
191
  }
243
192
 
193
+ /** This method opens the file and copies the schema to it, before the first write. */
244
194
  makeFile(copy: CopySchema): void {
245
195
  if (!this.waiting) return;
246
196
  const file = openNodeFile(this.sqlite, this.path, this.busyTimeoutMs);
@@ -255,84 +205,26 @@ class NodeOpenDatabase implements OpenDatabase {
255
205
  this.waiting = false;
256
206
  }
257
207
 
208
+ /** This method opens a file that another process made after the open, before a read. */
258
209
  refresh(copy: CopySchema): void {
259
210
  if (this.waiting && existsSync(this.path)) this.makeFile(copy);
260
211
  }
261
212
 
262
- saveFile(): Promise<void> {
263
- return Promise.resolve();
264
- }
265
-
266
- close(): void {
267
- this.db.close();
268
- }
269
- }
270
-
271
- // ---------------------------------------------------------------------------
272
- // sql.js
273
- // ---------------------------------------------------------------------------
274
-
275
- /** A database of sql.js: the database is in memory, and `saveFile()` writes the whole file. */
276
- class SqlJsOpenDatabase implements OpenDatabase {
277
- readonly db: ConnectionDatabase;
278
- private readonly save: () => Promise<void>;
279
-
280
- readonly engine = "sqljs";
281
- readonly waiting = false;
282
-
283
- constructor(db: ConnectionDatabase, save: () => Promise<void>) {
284
- this.db = db;
285
- this.save = save;
286
- }
287
-
288
- makeFile(): void {}
289
-
290
- refresh(): void {}
291
-
292
- saveFile(): Promise<void> {
293
- return this.save();
294
- }
295
-
296
213
  close(): void {
297
214
  this.db.close();
298
215
  }
299
216
  }
300
217
 
301
- /**
302
- * sql.js reads only the main file. A write-ahead log of the node engine (`<file>-wal`) can hold
303
- * changes that are not in the main file yet. A whole-file write of sql.js does not agree with the
304
- * log. Thus sql.js refuses such a file.
305
- */
306
- function checkNoWriteAheadLog(path: string): void {
307
- let size = 0;
308
- try {
309
- size = statSync(`${path}-wal`).size;
310
- } catch {
311
- return;
312
- }
313
- if (size > 0) {
314
- throw new Error(
315
- `The file ${path} has a write-ahead log (${path}-wal) of the engine node: open it with the engine node, or close the process that uses it`,
316
- );
317
- }
318
- }
319
-
320
- /** The error of the engine `node` when this Node.js has no `node:sqlite`. */
218
+ /** The error when this Node.js has no `node:sqlite` without a flag. */
321
219
  function noNodeSqlite(): Error {
322
220
  return new Error(
323
- `The SQLite engine node needs node:sqlite, and this Node.js (${process.version}) does not have it without a flag: use Node.js 22.13 or later, or the engine sqljs or auto`,
221
+ `SqliteStorage needs node:sqlite, and this Node.js (${process.version}) does not have it without a flag: use Node.js 22.13 or later`,
324
222
  );
325
223
  }
326
224
 
327
- /** This function opens the database of a path with an engine. `auto` uses `node` when it can. */
328
- export async function openEngine(path: string, engine: SqliteEngine, busyTimeoutMs: number): Promise<OpenDatabase> {
329
- if (engine !== "sqljs") {
330
- const sqlite = await loadNodeSqlite();
331
- if (sqlite) return new NodeOpenDatabase(sqlite, path, busyTimeoutMs);
332
- if (engine === "node") throw noNodeSqlite();
333
- }
334
- warnSqlJs(engine);
335
- if (path !== MEMORY_PATH) checkNoWriteAheadLog(path);
336
- const { db, save } = await createConnection({ dbPath: path });
337
- return new SqlJsOpenDatabase(new ConnectionDatabase(sqlJsConnection(db)), save);
225
+ /** This function opens the database of a path. */
226
+ export async function openEngine(path: string, busyTimeoutMs: number): Promise<OpenDatabase> {
227
+ const sqlite = await loadNodeSqlite();
228
+ if (!sqlite) throw noNodeSqlite();
229
+ return new OpenDatabase(sqlite, path, busyTimeoutMs);
338
230
  }
package/src/index.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  /**
2
- * client-sqlite
2
+ * @procedures/sqlite
3
3
  *
4
- * SQLite database operations via client procedures.
5
- * Enables database queries and logging via client.call().
4
+ * The SQLite procedures and the SQLite collection storage. Each procedure opens the file through the engine of
5
+ * `@mark1russell7/sqlite`. That package has the functions of a connection (`openDatabase`, `migrate`, `transaction`).
6
6
  *
7
7
  * @example
8
8
  * ```typescript
@@ -92,31 +92,3 @@ export {
92
92
  type SqliteEngineName,
93
93
  type SqliteStorageOptions,
94
94
  } from "./storage.js";
95
-
96
- // =============================================================================
97
- // Re-export docker-sqlite utilities for direct use
98
- // =============================================================================
99
-
100
- export {
101
- withConnection,
102
- createConnection,
103
- query,
104
- execute,
105
- execMultiple,
106
- lastInsertRowId,
107
- tableExists,
108
- getMigrationStatus,
109
- runMigrations,
110
- rollbackMigration,
111
- createMigration,
112
- } from "@mark1russell7/docker-sqlite";
113
-
114
- export type {
115
- ConnectionConfig,
116
- ConnectionCallback,
117
- QueryResult,
118
- ExecuteResult,
119
- Migration,
120
- MigrationStatus,
121
- Database,
122
- } from "@mark1russell7/docker-sqlite";
package/src/load.ts ADDED
@@ -0,0 +1,15 @@
1
+ /**
2
+ * The main entry of `@mark1russell7/sqlite`, loaded at its first use. The procedures and the storage load it, and with
3
+ * it `node:sqlite`, only when they open a database. An import of this package does not load them.
4
+ */
5
+
6
+ /** The main entry of `@mark1russell7/sqlite`. */
7
+ export type Sqlite = typeof import("@mark1russell7/sqlite");
8
+
9
+ let loading: Promise<Sqlite> | undefined;
10
+
11
+ /** This function loads the main entry of `@mark1russell7/sqlite` one time. */
12
+ export function loadSqlite(): Promise<Sqlite> {
13
+ loading ??= import("@mark1russell7/sqlite");
14
+ return loading;
15
+ }