@procedures/sqlite 0.60.0 → 0.61.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 +57 -0
  2. package/README.md +84 -183
  3. package/dist/engines.d.ts +31 -31
  4. package/dist/engines.d.ts.map +1 -1
  5. package/dist/engines.js +35 -78
  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 -29
  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 -132
  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 -136
  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,22 +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.
86
- * - `auto`: `node` when Node.js has `node:sqlite`, and else `sqljs`.
87
- *
88
- * The storages of one file in one process share one database, and thus one engine. The first
89
- * storage of the file sets it. A storage with another engine than `auto` refuses a database
90
- * 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`.
91
81
  * @default "auto"
92
82
  */
93
83
  engine?: SqliteEngine;
94
84
  /**
95
- * 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
96
86
  * `seconds(5)`. After it, the write fails with the error `database is locked`. The first
97
87
  * storage of a file sets it.
98
88
  * @default 5 seconds
@@ -223,7 +213,7 @@ const copySchema: CopySchema = (from, to) => {
223
213
  // The databases of the files, shared in the process
224
214
  // ---------------------------------------------------------------------------
225
215
 
226
- /** One open database and its file writes. */
216
+ /** One open database, and the storages that use it. */
227
217
  class SharedDatabase {
228
218
  readonly path: string;
229
219
  readonly open: OpenDatabase;
@@ -231,21 +221,8 @@ class SharedDatabase {
231
221
  /** The storages that use the database. */
232
222
  users = 0;
233
223
  closed = false;
234
- /**
235
- * The engine sqljs: true when the database has a change that is not in the file. The tables
236
- * that a storage makes when it opens are not a change: a storage that only reads does not make
237
- * the file.
238
- */
239
- dirty = false;
240
- /** The last file write. Its error is in the promise of the write that asked for it. */
241
- private saving: Promise<void> = Promise.resolve();
242
- /** A file write that waits for the running one. The writes that come before it starts share it. */
243
- private next: Promise<void> | undefined;
244
-
245
- constructor(
246
- path: string,
247
- open: OpenDatabase,
248
- ) {
224
+
225
+ constructor(path: string, open: OpenDatabase) {
249
226
  this.path = path;
250
227
  this.open = open;
251
228
  }
@@ -253,55 +230,10 @@ class SharedDatabase {
253
230
  get engine(): SqliteEngineName {
254
231
  return this.open.engine;
255
232
  }
256
-
257
- /** True when the engine writes the whole file after a write: sql.js with a file. */
258
- private get savesFile(): boolean {
259
- return this.open.engine === "sqljs" && this.path !== MEMORY_PATH;
260
- }
261
-
262
- /**
263
- * This method records a change, and gives the promise of the file write that holds it. The
264
- * engine node wrote the change at the commit of its transaction.
265
- */
266
- changed(): Promise<void> {
267
- if (!this.savesFile) return Promise.resolve();
268
- this.dirty = true;
269
- return this.save();
270
- }
271
-
272
- /**
273
- * This method writes the file after the running file write. It gives the promise of a file write that
274
- * starts after this call, so the file gets each change that came before the call.
275
- */
276
- save(): Promise<void> {
277
- if (!this.savesFile) return Promise.resolve();
278
- if (this.next) return this.next;
279
- const run = this.saving.then(async () => {
280
- // The file write reads the database now: a later change needs a new file write
281
- this.next = undefined;
282
- if (!this.dirty) return;
283
- this.dirty = false;
284
- try {
285
- await this.open.saveFile();
286
- } catch (error) {
287
- this.dirty = true;
288
- throw error;
289
- }
290
- });
291
- this.next = run;
292
- this.saving = run.catch(() => undefined);
293
- return run;
294
- }
295
- }
296
-
297
- /** A database of a file that opens or is open, and the engine choice of the storage that opened it. */
298
- interface FileEntry {
299
- engine: SqliteEngine;
300
- database: Promise<SharedDatabase>;
301
233
  }
302
234
 
303
235
  /** The databases of the files that open or are open, by the key of the file. */
304
- const files = new Map<string, FileEntry>();
236
+ const files = new Map<string, Promise<SharedDatabase>>();
305
237
  /** The open databases of the files, by the key of the file. */
306
238
  const opened = new Map<string, SharedDatabase>();
307
239
 
@@ -311,26 +243,25 @@ function fileKey(path: string): string {
311
243
  return process.platform === "win32" ? absolute.toLowerCase() : absolute;
312
244
  }
313
245
 
314
- async function openDatabase(path: string, engine: SqliteEngine, busyTimeoutMs: number): Promise<SharedDatabase> {
315
- 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));
316
248
  }
317
249
 
318
250
  /** This function opens the database of a path, or gives the open database of the same file. */
319
- async function acquire(path: string, engine: SqliteEngine, busyTimeoutMs: number): Promise<SharedDatabase> {
251
+ async function acquire(path: string, busyTimeoutMs: number): Promise<SharedDatabase> {
320
252
  if (path === MEMORY_PATH) {
321
- const shared = await openDatabase(path, engine, busyTimeoutMs);
253
+ const shared = await openDatabase(path, busyTimeoutMs);
322
254
  shared.users++;
323
255
  return shared;
324
256
  }
325
257
  const key = fileKey(path);
326
258
  for (;;) {
327
- let entry = files.get(key);
328
- if (!entry) {
329
- const opening = openDatabase(path, engine, busyTimeoutMs);
330
- entry = { engine, database: opening };
331
- files.set(key, entry);
332
- const own = entry;
333
- 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(
334
265
  (shared) => opened.set(key, shared),
335
266
  () => {
336
267
  // A failed open does not stay: the next storage tries again
@@ -338,41 +269,18 @@ async function acquire(path: string, engine: SqliteEngine, busyTimeoutMs: number
338
269
  },
339
270
  );
340
271
  }
341
- let shared: SharedDatabase;
342
- try {
343
- shared = await entry.database;
344
- } catch (error) {
345
- // A storage with another engine choice failed to open the file: try with this choice
346
- if (entry.engine !== engine) continue;
347
- throw error;
348
- }
272
+ const shared = await opening;
349
273
  // The last storage closed the database while this one waited: open the file again
350
274
  if (shared.closed) continue;
351
- if (engine !== "auto" && shared.engine !== engine) {
352
- throw new Error(
353
- `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}.`,
354
- );
355
- }
356
275
  shared.users++;
357
276
  return shared;
358
277
  }
359
278
  }
360
279
 
361
- /** 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. */
362
281
  async function release(shared: SharedDatabase): Promise<void> {
363
282
  shared.users--;
364
283
  if (shared.users > 0) return;
365
- let failure: unknown;
366
- try {
367
- await shared.save();
368
- } catch (error) {
369
- failure = error;
370
- }
371
- // Another storage of the file opened while the file write ran
372
- if (shared.users > 0) {
373
- if (failure !== undefined) throw failure;
374
- return;
375
- }
376
284
  shared.closed = true;
377
285
  if (shared.path !== MEMORY_PATH) {
378
286
  const key = fileKey(shared.path);
@@ -382,7 +290,6 @@ async function release(shared: SharedDatabase): Promise<void> {
382
290
  }
383
291
  }
384
292
  shared.open.close();
385
- if (failure !== undefined) throw failure;
386
293
  }
387
294
 
388
295
  // ---------------------------------------------------------------------------
@@ -410,7 +317,7 @@ interface WriteResult<R> {
410
317
  * await users.set("123", { id: "123", name: "John" });
411
318
  * const user = await users.get("123"); // { id: "123", name: "John" }
412
319
  * const changes = await users.getChangesSince(); // { version, full: true, changed, deleted: [] }
413
- * await users.getEngine(); // "node" on Node.js 22.13 or later, else "sqljs"
320
+ * await users.getEngine(); // "node"
414
321
  * await users.close();
415
322
  * ```
416
323
  */
@@ -620,7 +527,7 @@ export class SqliteStorage<T> implements CollectionStorage<T> {
620
527
  // ═══ Lifecycle & Metadata ═══
621
528
  //
622
529
 
623
- /** 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`. */
624
531
  async getEngine(): Promise<SqliteEngineName> {
625
532
  this.checkOpen();
626
533
  const shared = await this.opening;
@@ -629,19 +536,18 @@ export class SqliteStorage<T> implements CollectionStorage<T> {
629
536
  }
630
537
 
631
538
  /**
632
- * This method writes the database file. Each write ends after its file write already, so a
633
- * caller needs it only after a failed file write of the engine `sqljs`. The engine `node`
634
- * 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.
635
542
  */
636
543
  async flush(): Promise<void> {
637
544
  this.checkOpen();
638
- const shared = await this.opening;
545
+ await this.opening;
639
546
  this.checkOpen();
640
- await shared.save();
641
547
  }
642
548
 
643
549
  /**
644
- * 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
645
551
  * storage of the same file and table reads them, and the versions of this storage stay valid.
646
552
  * After `close()`, each operation rejects, and a second `close()` does nothing.
647
553
  */
@@ -674,7 +580,7 @@ export class SqliteStorage<T> implements CollectionStorage<T> {
674
580
 
675
581
  /** This method opens the database, and makes the tables of the collection and of the change log. */
676
582
  private async open(busyTimeoutMs: number): Promise<SharedDatabase> {
677
- const shared = await acquire(this.path, this.engine, busyTimeoutMs);
583
+ const shared = await acquire(this.path, busyTimeoutMs);
678
584
  try {
679
585
  prepareTable(shared.open.db, this.table, newEpoch());
680
586
  return shared;
@@ -687,7 +593,7 @@ export class SqliteStorage<T> implements CollectionStorage<T> {
687
593
  /** The open database. It throws after `close()`, so the promise of the operation rejects. */
688
594
  private async database(): Promise<EngineDatabase> {
689
595
  const shared = await this.sharedDatabase();
690
- // The engine node: another process made the file after the open
596
+ // Another process made the file after the open
691
597
  shared.open.refresh(copySchema);
692
598
  return shared.open.db;
693
599
  }
@@ -742,13 +648,12 @@ export class SqliteStorage<T> implements CollectionStorage<T> {
742
648
  }
743
649
 
744
650
  /**
745
- * One write: the change in a transaction with the change log, then the file write of the engine
746
- * `sqljs`. `next` gives the next number of the change log. A write that changes nothing writes
747
- * no file.
651
+ * One write: the change in a transaction with the change log. `next` gives the next number of
652
+ * the change log.
748
653
  *
749
- * Before the first write, the engine `node` keeps the database of a missing file in memory, with
750
- * no item. A write first tests its change there. When it changes an item, the storage makes the
751
- * 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.
752
657
  */
753
658
  private async write<R>(change: (db: EngineDatabase, next: () => number) => R): Promise<R> {
754
659
  const shared = await this.sharedDatabase();
@@ -759,9 +664,7 @@ export class SqliteStorage<T> implements CollectionStorage<T> {
759
664
  if (!test.changed) return test.value;
760
665
  open.makeFile(copySchema);
761
666
  }
762
- const { value, changed } = this.runWrite(open.db, change, true);
763
- if (changed) await shared.changed();
764
- return value;
667
+ return this.runWrite(open.db, change, true).value;
765
668
  }
766
669
 
767
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