@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/storage.ts CHANGED
@@ -5,11 +5,9 @@
5
5
  * or `:memory:`. It implements `CollectionStorage` of `@procedures/client`, so a
6
6
  * `ProcedureServer`, an `ApiStorage` server and a `HybridStorage` can use it.
7
7
  *
8
- * - The storage has two engines (the option `engine`). The engine `node` is `node:sqlite` of
9
- * Node.js: it writes only the changed pages of the file. The engine `sqljs` is sql.js (SQLite in
10
- * WebAssembly) through `@mark1russell7/docker-sqlite`: it keeps the database in memory and
11
- * writes the whole file after a write. The default `auto` uses `node` when Node.js has it.
12
- * - The two engines use the same file format: a file of one engine opens with the other.
8
+ * - The engine is `node:sqlite` of Node.js, through `@mark1russell7/sqlite`. It writes only the
9
+ * changed pages of the file. Version 0.61.0 removed the engine `sqljs`, and a file that it wrote
10
+ * opens with its items and its versions.
13
11
  * - The table of a collection has the columns `id` (the key) and `value` (the JSON text of the
14
12
  * item). A value goes through `JSON.stringify` and `JSON.parse`.
15
13
  * - A change log gives `getChangesSince()`. The table `_storage_log` has the last change of each
@@ -42,7 +40,7 @@ import {
42
40
  type StorageSelector,
43
41
  } from "@procedures/client";
44
42
  import { VersionConflictError, applyPatch, patchProblems, preconditionNotSupported, queryEntries, versionConflicts, type CurrentItem } from "@procedures/client/query";
45
- // The entry /portable imports no module of Node.js. The engine node loads node:sqlite only when a storage opens.
43
+ // The entry /portable imports no module of Node.js. The storage loads node:sqlite only when it opens.
46
44
  import { readTransaction, tableColumns, transaction } from "@mark1russell7/sqlite/portable";
47
45
  import {
48
46
  MEMORY_PATH,
@@ -77,23 +75,14 @@ export interface SqliteStorageOptions<T = unknown> {
77
75
  */
78
76
  maxTombstones?: number;
79
77
  /**
80
- * The SQLite engine.
81
- *
82
- * - `node`: the module `node:sqlite` of Node.js 22.13 or later. It writes only the changes, and
83
- * the file is in WAL mode. Two processes can use one file.
84
- * - `sqljs`: sql.js, SQLite in WebAssembly. It keeps the database in memory, and it writes the
85
- * whole file after a write. This engine is deprecated, and version 0.61.0 removes it. Its first
86
- * open in a process shows a `DeprecationWarning` with the code `PROCEDURES_SQLITE_SQLJS`.
87
- * - `auto`: `node` when Node.js has `node:sqlite`, and else `sqljs`, with the same warning.
88
- *
89
- * The storages of one file in one process share one database, and thus one engine. The first
90
- * storage of the file sets it. A storage with another engine than `auto` refuses a database
91
- * of the other engine.
78
+ * The SQLite engine. `auto` and `node` both use the module `node:sqlite` of Node.js 22.13 or
79
+ * later. It writes only the changes, and the file is in WAL mode. Two processes can use one file.
80
+ * Version 0.61.0 removed the engine `sqljs`: the constructor refuses it with a `TypeError`.
92
81
  * @default "auto"
93
82
  */
94
83
  engine?: SqliteEngine;
95
84
  /**
96
- * The time that a write of the engine `node` waits for the lock of another process, for example
85
+ * The time that a write waits for the lock of another process, for example
97
86
  * `seconds(5)`. After it, the write fails with the error `database is locked`. The first
98
87
  * storage of a file sets it.
99
88
  * @default 5 seconds
@@ -224,7 +213,7 @@ const copySchema: CopySchema = (from, to) => {
224
213
  // The databases of the files, shared in the process
225
214
  // ---------------------------------------------------------------------------
226
215
 
227
- /** One open database and its file writes. */
216
+ /** One open database, and the storages that use it. */
228
217
  class SharedDatabase {
229
218
  readonly path: string;
230
219
  readonly open: OpenDatabase;
@@ -232,21 +221,8 @@ class SharedDatabase {
232
221
  /** The storages that use the database. */
233
222
  users = 0;
234
223
  closed = false;
235
- /**
236
- * The engine sqljs: true when the database has a change that is not in the file. The tables
237
- * that a storage makes when it opens are not a change: a storage that only reads does not make
238
- * the file.
239
- */
240
- dirty = false;
241
- /** The last file write. Its error is in the promise of the write that asked for it. */
242
- private saving: Promise<void> = Promise.resolve();
243
- /** A file write that waits for the running one. The writes that come before it starts share it. */
244
- private next: Promise<void> | undefined;
245
-
246
- constructor(
247
- path: string,
248
- open: OpenDatabase,
249
- ) {
224
+
225
+ constructor(path: string, open: OpenDatabase) {
250
226
  this.path = path;
251
227
  this.open = open;
252
228
  }
@@ -254,55 +230,10 @@ class SharedDatabase {
254
230
  get engine(): SqliteEngineName {
255
231
  return this.open.engine;
256
232
  }
257
-
258
- /** True when the engine writes the whole file after a write: sql.js with a file. */
259
- private get savesFile(): boolean {
260
- return this.open.engine === "sqljs" && this.path !== MEMORY_PATH;
261
- }
262
-
263
- /**
264
- * This method records a change, and gives the promise of the file write that holds it. The
265
- * engine node wrote the change at the commit of its transaction.
266
- */
267
- changed(): Promise<void> {
268
- if (!this.savesFile) return Promise.resolve();
269
- this.dirty = true;
270
- return this.save();
271
- }
272
-
273
- /**
274
- * This method writes the file after the running file write. It gives the promise of a file write that
275
- * starts after this call, so the file gets each change that came before the call.
276
- */
277
- save(): Promise<void> {
278
- if (!this.savesFile) return Promise.resolve();
279
- if (this.next) return this.next;
280
- const run = this.saving.then(async () => {
281
- // The file write reads the database now: a later change needs a new file write
282
- this.next = undefined;
283
- if (!this.dirty) return;
284
- this.dirty = false;
285
- try {
286
- await this.open.saveFile();
287
- } catch (error) {
288
- this.dirty = true;
289
- throw error;
290
- }
291
- });
292
- this.next = run;
293
- this.saving = run.catch(() => undefined);
294
- return run;
295
- }
296
- }
297
-
298
- /** A database of a file that opens or is open, and the engine choice of the storage that opened it. */
299
- interface FileEntry {
300
- engine: SqliteEngine;
301
- database: Promise<SharedDatabase>;
302
233
  }
303
234
 
304
235
  /** The databases of the files that open or are open, by the key of the file. */
305
- const files = new Map<string, FileEntry>();
236
+ const files = new Map<string, Promise<SharedDatabase>>();
306
237
  /** The open databases of the files, by the key of the file. */
307
238
  const opened = new Map<string, SharedDatabase>();
308
239
 
@@ -312,26 +243,25 @@ function fileKey(path: string): string {
312
243
  return process.platform === "win32" ? absolute.toLowerCase() : absolute;
313
244
  }
314
245
 
315
- async function openDatabase(path: string, engine: SqliteEngine, busyTimeoutMs: number): Promise<SharedDatabase> {
316
- return new SharedDatabase(path, await openEngine(path, engine, busyTimeoutMs));
246
+ async function openDatabase(path: string, busyTimeoutMs: number): Promise<SharedDatabase> {
247
+ return new SharedDatabase(path, await openEngine(path, busyTimeoutMs));
317
248
  }
318
249
 
319
250
  /** This function opens the database of a path, or gives the open database of the same file. */
320
- async function acquire(path: string, engine: SqliteEngine, busyTimeoutMs: number): Promise<SharedDatabase> {
251
+ async function acquire(path: string, busyTimeoutMs: number): Promise<SharedDatabase> {
321
252
  if (path === MEMORY_PATH) {
322
- const shared = await openDatabase(path, engine, busyTimeoutMs);
253
+ const shared = await openDatabase(path, busyTimeoutMs);
323
254
  shared.users++;
324
255
  return shared;
325
256
  }
326
257
  const key = fileKey(path);
327
258
  for (;;) {
328
- let entry = files.get(key);
329
- if (!entry) {
330
- const opening = openDatabase(path, engine, busyTimeoutMs);
331
- entry = { engine, database: opening };
332
- files.set(key, entry);
333
- const own = entry;
334
- opening.then(
259
+ let opening = files.get(key);
260
+ if (!opening) {
261
+ const own = openDatabase(path, busyTimeoutMs);
262
+ opening = own;
263
+ files.set(key, own);
264
+ own.then(
335
265
  (shared) => opened.set(key, shared),
336
266
  () => {
337
267
  // A failed open does not stay: the next storage tries again
@@ -339,41 +269,18 @@ async function acquire(path: string, engine: SqliteEngine, busyTimeoutMs: number
339
269
  },
340
270
  );
341
271
  }
342
- let shared: SharedDatabase;
343
- try {
344
- shared = await entry.database;
345
- } catch (error) {
346
- // A storage with another engine choice failed to open the file: try with this choice
347
- if (entry.engine !== engine) continue;
348
- throw error;
349
- }
272
+ const shared = await opening;
350
273
  // The last storage closed the database while this one waited: open the file again
351
274
  if (shared.closed) continue;
352
- if (engine !== "auto" && shared.engine !== engine) {
353
- throw new Error(
354
- `The file ${path} is open with the SQLite engine ${shared.engine} in this process: a storage with the engine ${engine} cannot share it. Give the engine auto or ${shared.engine}.`,
355
- );
356
- }
357
275
  shared.users++;
358
276
  return shared;
359
277
  }
360
278
  }
361
279
 
362
- /** This function releases a database. The last storage writes the file and closes the database. */
280
+ /** This function releases a database. The last storage closes it. */
363
281
  async function release(shared: SharedDatabase): Promise<void> {
364
282
  shared.users--;
365
283
  if (shared.users > 0) return;
366
- let failure: unknown;
367
- try {
368
- await shared.save();
369
- } catch (error) {
370
- failure = error;
371
- }
372
- // Another storage of the file opened while the file write ran
373
- if (shared.users > 0) {
374
- if (failure !== undefined) throw failure;
375
- return;
376
- }
377
284
  shared.closed = true;
378
285
  if (shared.path !== MEMORY_PATH) {
379
286
  const key = fileKey(shared.path);
@@ -383,7 +290,6 @@ async function release(shared: SharedDatabase): Promise<void> {
383
290
  }
384
291
  }
385
292
  shared.open.close();
386
- if (failure !== undefined) throw failure;
387
293
  }
388
294
 
389
295
  // ---------------------------------------------------------------------------
@@ -411,7 +317,7 @@ interface WriteResult<R> {
411
317
  * await users.set("123", { id: "123", name: "John" });
412
318
  * const user = await users.get("123"); // { id: "123", name: "John" }
413
319
  * const changes = await users.getChangesSince(); // { version, full: true, changed, deleted: [] }
414
- * await users.getEngine(); // "node" on Node.js 22.13 or later, else "sqljs"
320
+ * await users.getEngine(); // "node"
415
321
  * await users.close();
416
322
  * ```
417
323
  */
@@ -621,7 +527,7 @@ export class SqliteStorage<T> implements CollectionStorage<T> {
621
527
  // ═══ Lifecycle & Metadata ═══
622
528
  //
623
529
 
624
- /** The engine that the storage uses: `node` or `sqljs`. With the option `auto`, the open decides it. */
530
+ /** The engine that the storage uses: `node`. */
625
531
  async getEngine(): Promise<SqliteEngineName> {
626
532
  this.checkOpen();
627
533
  const shared = await this.opening;
@@ -630,19 +536,18 @@ export class SqliteStorage<T> implements CollectionStorage<T> {
630
536
  }
631
537
 
632
538
  /**
633
- * This method writes the database file. Each write ends after its file write already, so a
634
- * caller needs it only after a failed file write of the engine `sqljs`. The engine `node`
635
- * writes each change at its commit.
539
+ * This method does no work: each write is in the file at the commit of its transaction. It
540
+ * rejects after `close()`. It is the optional method `flush` of `CollectionStorage`, which the
541
+ * drain of a procedure server calls.
636
542
  */
637
543
  async flush(): Promise<void> {
638
544
  this.checkOpen();
639
- const shared = await this.opening;
545
+ await this.opening;
640
546
  this.checkOpen();
641
- await shared.save();
642
547
  }
643
548
 
644
549
  /**
645
- * This method writes the file and releases the database. The items stay in the file. A new
550
+ * This method releases the database. The items stay in the file. A new
646
551
  * storage of the same file and table reads them, and the versions of this storage stay valid.
647
552
  * After `close()`, each operation rejects, and a second `close()` does nothing.
648
553
  */
@@ -675,7 +580,7 @@ export class SqliteStorage<T> implements CollectionStorage<T> {
675
580
 
676
581
  /** This method opens the database, and makes the tables of the collection and of the change log. */
677
582
  private async open(busyTimeoutMs: number): Promise<SharedDatabase> {
678
- const shared = await acquire(this.path, this.engine, busyTimeoutMs);
583
+ const shared = await acquire(this.path, busyTimeoutMs);
679
584
  try {
680
585
  prepareTable(shared.open.db, this.table, newEpoch());
681
586
  return shared;
@@ -688,7 +593,7 @@ export class SqliteStorage<T> implements CollectionStorage<T> {
688
593
  /** The open database. It throws after `close()`, so the promise of the operation rejects. */
689
594
  private async database(): Promise<EngineDatabase> {
690
595
  const shared = await this.sharedDatabase();
691
- // The engine node: another process made the file after the open
596
+ // Another process made the file after the open
692
597
  shared.open.refresh(copySchema);
693
598
  return shared.open.db;
694
599
  }
@@ -743,13 +648,12 @@ export class SqliteStorage<T> implements CollectionStorage<T> {
743
648
  }
744
649
 
745
650
  /**
746
- * One write: the change in a transaction with the change log, then the file write of the engine
747
- * `sqljs`. `next` gives the next number of the change log. A write that changes nothing writes
748
- * no file.
651
+ * One write: the change in a transaction with the change log. `next` gives the next number of
652
+ * the change log.
749
653
  *
750
- * Before the first write, the engine `node` keeps the database of a missing file in memory, with
751
- * no item. A write first tests its change there. When it changes an item, the storage makes the
752
- * file and runs the write in it.
654
+ * Before the first write, the storage keeps the database of a missing file in memory, with no
655
+ * item. A write first tests its change there. When it changes an item, the storage makes the
656
+ * file and runs the write in it. Thus a write that changes nothing makes no file.
753
657
  */
754
658
  private async write<R>(change: (db: EngineDatabase, next: () => number) => R): Promise<R> {
755
659
  const shared = await this.sharedDatabase();
@@ -760,9 +664,7 @@ export class SqliteStorage<T> implements CollectionStorage<T> {
760
664
  if (!test.changed) return test.value;
761
665
  open.makeFile(copySchema);
762
666
  }
763
- const { value, changed } = this.runWrite(open.db, change, true);
764
- if (changed) await shared.changed();
765
- return value;
667
+ return this.runWrite(open.db, change, true).value;
766
668
  }
767
669
 
768
670
  private runWrite<R>(db: EngineDatabase, change: (db: EngineDatabase, next: () => number) => R, keep: boolean): WriteResult<R> {
package/src/types.ts CHANGED
@@ -379,7 +379,7 @@ export const DbMigrateOutputSchema: z.ZodType<DbMigrateOutput> = z.looseObject({
379
379
  export interface DbBackupInput {
380
380
  /** The new file. The procedure does not replace a file. */
381
381
  to: string;
382
- /** Also compact the copy (VACUUM): it leaves out the free pages */
382
+ /** @deprecated This option has no effect since version 0.61.0: the copy is always compact. A later minor version removes it. */
383
383
  vacuum?: boolean | undefined;
384
384
  dbPath?: string | undefined;
385
385
  }
@@ -393,7 +393,7 @@ export interface DbBackupOutput {
393
393
 
394
394
  export const DbBackupInputSchema: z.ZodType<DbBackupInput> = z.object({
395
395
  to: z.string().min(1).describe("The new file. The procedure does not replace a file."),
396
- vacuum: z.boolean().optional().describe("Also compact the copy (VACUUM): it leaves out the free pages"),
396
+ vacuum: z.boolean().optional().describe("Deprecated, with no effect: the copy is always compact. A later minor version removes it."),
397
397
  dbPath,
398
398
  });
399
399