@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.
- package/CHANGELOG.md +40 -0
- package/README.md +84 -189
- package/dist/engines.d.ts +31 -34
- package/dist/engines.d.ts.map +1 -1
- package/dist/engines.js +35 -95
- package/dist/engines.js.map +1 -1
- package/dist/index.d.ts +3 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -7
- package/dist/index.js.map +1 -1
- package/dist/load.d.ts +9 -0
- package/dist/load.d.ts.map +1 -0
- package/dist/load.js +11 -0
- package/dist/load.js.map +1 -0
- package/dist/module.d.ts.map +1 -1
- package/dist/module.js +104 -62
- package/dist/module.js.map +1 -1
- package/dist/procedures/db/backup.d.ts +5 -7
- package/dist/procedures/db/backup.d.ts.map +1 -1
- package/dist/procedures/db/backup.js +22 -27
- package/dist/procedures/db/backup.js.map +1 -1
- package/dist/procedures/db/migrate.d.ts +1 -1
- package/dist/procedures/db/migrate.d.ts.map +1 -1
- package/dist/procedures/db/migrate.js +32 -38
- package/dist/procedures/db/migrate.js.map +1 -1
- package/dist/procedures/db/schema.d.ts.map +1 -1
- package/dist/procedures/db/schema.js +8 -10
- package/dist/procedures/db/schema.js.map +1 -1
- package/dist/procedures/db/shared.d.ts +75 -10
- package/dist/procedures/db/shared.d.ts.map +1 -1
- package/dist/procedures/db/shared.js +148 -53
- package/dist/procedures/db/shared.js.map +1 -1
- package/dist/procedures/db/tables.d.ts.map +1 -1
- package/dist/procedures/db/tables.js +5 -7
- package/dist/procedures/db/tables.js.map +1 -1
- package/dist/procedures/db/transaction.d.ts +3 -4
- package/dist/procedures/db/transaction.d.ts.map +1 -1
- package/dist/procedures/db/transaction.js +18 -26
- package/dist/procedures/db/transaction.js.map +1 -1
- package/dist/storage.d.ts +18 -30
- package/dist/storage.d.ts.map +1 -1
- package/dist/storage.js +32 -118
- package/dist/storage.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +8 -10
- package/src/engines.ts +50 -158
- package/src/index.ts +3 -31
- package/src/load.ts +15 -0
- package/src/module.ts +128 -84
- package/src/procedures/db/backup.ts +21 -27
- package/src/procedures/db/migrate.ts +35 -40
- package/src/procedures/db/schema.ts +10 -12
- package/src/procedures/db/shared.ts +178 -50
- package/src/procedures/db/tables.ts +5 -7
- package/src/procedures/db/transaction.ts +17 -24
- package/src/storage.ts +39 -137
- 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
|
|
9
|
-
*
|
|
10
|
-
*
|
|
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
|
|
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
|
-
*
|
|
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
|
|
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
|
|
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
|
-
|
|
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,
|
|
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,
|
|
316
|
-
return new SharedDatabase(path, await openEngine(path,
|
|
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,
|
|
251
|
+
async function acquire(path: string, busyTimeoutMs: number): Promise<SharedDatabase> {
|
|
321
252
|
if (path === MEMORY_PATH) {
|
|
322
|
-
const shared = await openDatabase(path,
|
|
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
|
|
329
|
-
if (!
|
|
330
|
-
const
|
|
331
|
-
|
|
332
|
-
files.set(key,
|
|
333
|
-
|
|
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
|
-
|
|
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
|
|
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"
|
|
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
|
|
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
|
|
634
|
-
*
|
|
635
|
-
*
|
|
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
|
-
|
|
545
|
+
await this.opening;
|
|
640
546
|
this.checkOpen();
|
|
641
|
-
await shared.save();
|
|
642
547
|
}
|
|
643
548
|
|
|
644
549
|
/**
|
|
645
|
-
* This method
|
|
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,
|
|
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
|
-
//
|
|
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
|
|
747
|
-
*
|
|
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
|
|
751
|
-
*
|
|
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
|
-
|
|
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
|
-
/**
|
|
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("
|
|
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
|
|