@procedures/sqlite 0.60.1 → 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 +33 -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/CHANGELOG.md CHANGED
@@ -1,5 +1,38 @@
1
1
  # @procedures/sqlite
2
2
 
3
+ ## 0.61.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 9f19bb5: The SQLite procedures and `SqliteStorage` use only `node:sqlite`, through the engine of `@mark1russell7/sqlite`. Version 0.61.0 removes the engine `sqljs` and the package `@mark1russell7/docker-sqlite`.
8
+
9
+ Breaking changes, with the steps to migrate:
10
+
11
+ - `sqlite.query`, `sqlite.execute`, `sqlite.transaction` and `sqlite.migrate` need Node.js 24.10 or later: they refuse with `CONFINE_UNAVAILABLE` before it. To migrate, run the host on Node.js 24.10 or later. The other procedures and `SqliteStorage` work on Node.js 22.13 or later.
12
+ - The package needs Node.js 22.13 or later. Earlier versions of Node.js have no `node:sqlite` without a flag.
13
+ - `SqliteStorage` refuses the engine `sqljs` with a `TypeError`. To migrate, use the engine `auto` or `node`. A file of the engine `sqljs` opens with the engine `node`, with its items and its versions.
14
+ - The root entry no longer exports the functions and the types of `@mark1russell7/docker-sqlite` (`withConnection`, `createConnection`, `query`, `execute`, `execMultiple`, `lastInsertRowId`, `tableExists`, `getMigrationStatus`, `runMigrations`, `rollbackMigration`, `createMigration`, `ConnectionConfig`, `ConnectionCallback`, `QueryResult`, `ExecuteResult`, `Migration`, `MigrationStatus`, `Database`). To migrate, import `openDatabase`, `transaction` and `migrate` from `@mark1russell7/sqlite`.
15
+ - A script of several statements in `sqlite.execute` runs in one transaction: all its statements take effect, or none does. A script that starts with `BEGIN`, `SAVEPOINT` or another statement that controls a transaction runs as written. In a script that does not start with such a statement, a later one is refused. So a `VACUUM` in a script fails: send it alone.
16
+ - `sqlite.execute` refuses SQL that leaves a transaction open, also `BEGIN` alone. The procedure rolls the transaction back, so the file keeps none of its changes. To migrate, end each transaction with `COMMIT` or `ROLLBACK`.
17
+ - `sqlite.execute`, `sqlite.transaction`, `sqlite.migrate` and `logs.store` make a missing file when they open it, also when the work then fails.
18
+
19
+ Other changes:
20
+
21
+ - Each procedure writes only the changed pages of the file, and it waits for the lock of another process. A file keeps its journal mode: a WAL file stays in WAL mode.
22
+ - The number of changes of `sqlite.execute` is correct for a change of the schema: `0`.
23
+ - `sqlite.backup` writes a snapshot of the file with `snapshot` of `@mark1russell7/sqlite`, checks the copy, and then renames it. The copy is consistent, has the changes in the WAL file, and is always compact. The option `vacuum` has no effect and is deprecated. A later minor version removes it.
24
+ - A file that an earlier version migrated keeps its migration table and its checksums. A log file of an earlier version keeps its entries.
25
+ - `flush()` of `SqliteStorage` does no work, because each commit writes the file. It stays as the optional method of `CollectionStorage`.
26
+
27
+ MINOR: breaking changes before 1.0.0.
28
+
29
+ ### Patch Changes
30
+
31
+ - Updated dependencies [8506442]
32
+ - Updated dependencies [f9a78f9]
33
+ - Updated dependencies [3eb4b59]
34
+ - @procedures/client@0.61.0
35
+
3
36
  ## 0.60.1
4
37
 
5
38
  ### Patch Changes
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
6
  [![TypeScript](https://img.shields.io/badge/TypeScript-5.9-blue.svg)](https://www.typescriptlang.org/)
7
- [![Node.js](https://img.shields.io/badge/Node.js-20+-green.svg)](https://nodejs.org/)
7
+ [![Node.js](https://img.shields.io/badge/Node.js-22.13+-green.svg)](https://nodejs.org/)
8
8
 
9
9
  SQLite database operations via client procedures. Execute queries and store structured logs via `client.call()`.
10
10
 
@@ -47,19 +47,19 @@ graph TB
47
47
  end
48
48
 
49
49
  Migrations["Log Table Migrations<br/>(Auto-created on first use)"]
50
+ Storage["SqliteStorage<br/>(collection storage)"]
50
51
  Types["types.ts<br/>(Type Definitions)"]
51
52
  end
52
53
 
53
- subgraph "docker-sqlite Layer"
54
- DockerSqlite["@mark1russell7/docker-sqlite"]
55
- Utilities["withConnection<br/>query, execute<br/>runMigrations"]
54
+ subgraph "The engine: @mark1russell7/sqlite"
55
+ Home["openDatabase, transaction<br/>migrate, snapshot"]
56
56
  end
57
57
 
58
- subgraph "SQLite Engine"
59
- SqlJs["sql.js<br/>(SQLite WASM)"]
58
+ subgraph "SQLite"
59
+ NodeSqlite["node:sqlite<br/>(Node.js 22.13 or later)"]
60
60
  end
61
61
 
62
- subgraph "Storage"
62
+ subgraph "Files"
63
63
  LogDB["~/logs/cli/cli.db<br/>(Default log database)"]
64
64
  CustomDB["Custom databases<br/>(via dbPath parameter)"]
65
65
  end
@@ -67,15 +67,15 @@ graph TB
67
67
  Register --> Client
68
68
  Register --> Registry
69
69
 
70
- DB --> DockerSqlite
71
- Logs --> DockerSqlite
70
+ DB --> Home
71
+ Logs --> Home
72
72
  Logs --> Migrations
73
+ Storage --> Home
73
74
 
74
- DockerSqlite --> Utilities
75
- Utilities --> SqlJs
75
+ Home --> NodeSqlite
76
76
 
77
- SqlJs --> LogDB
78
- SqlJs --> CustomDB
77
+ NodeSqlite --> LogDB
78
+ NodeSqlite --> CustomDB
79
79
 
80
80
  Types -.-> DB
81
81
  Types -.-> Logs
@@ -92,18 +92,18 @@ graph LR
92
92
  App["Your Application"]
93
93
  ClientSqlite["@procedures/sqlite"]
94
94
  Client["@procedures/client"]
95
- DockerSqlite["@mark1russell7/docker-sqlite"]
96
- SqlJs["sql.js"]
95
+ Home["@mark1russell7/sqlite"]
96
+ NodeSqlite["node:sqlite"]
97
97
 
98
98
  App --> ClientSqlite
99
99
  ClientSqlite --> Client
100
- ClientSqlite --> DockerSqlite
101
- DockerSqlite --> SqlJs
100
+ ClientSqlite --> Home
101
+ Home --> NodeSqlite
102
102
 
103
103
  App -.client.call.-> Client
104
104
  Client -.executes.-> ClientSqlite
105
- ClientSqlite -.queries.-> DockerSqlite
106
- DockerSqlite -.uses.-> SqlJs
105
+ ClientSqlite -.opens the file.-> Home
106
+ Home -.uses.-> NodeSqlite
107
107
  ```
108
108
 
109
109
  ### Log Storage Flow
@@ -112,20 +112,18 @@ graph LR
112
112
  sequenceDiagram
113
113
  participant App
114
114
  participant LogsStore
115
- participant DockerSqlite
116
- participant Migration
117
- participant SQLite
115
+ participant Home as @mark1russell7/sqlite
116
+ participant SQLite as node:sqlite
118
117
 
119
118
  App->>LogsStore: logs.store({ level, message, ... })
120
- LogsStore->>Migration: ensureLogsTable()
121
- Migration->>DockerSqlite: withConnection()
122
- DockerSqlite->>SQLite: Open ~/logs/cli/cli.db
123
- Migration->>DockerSqlite: runMigrations()
124
- DockerSqlite->>SQLite: CREATE TABLE IF NOT EXISTS logs
125
- LogsStore->>DockerSqlite: execute(INSERT INTO logs ...)
126
- DockerSqlite->>SQLite: INSERT
127
- SQLite-->>DockerSqlite: lastInsertRowId
128
- DockerSqlite-->>LogsStore: { id }
119
+ LogsStore->>Home: openDatabase(~/logs/cli/cli.db)
120
+ Home->>SQLite: Open the file, set the busy timeout
121
+ LogsStore->>Home: transaction()
122
+ Home->>SQLite: BEGIN IMMEDIATE
123
+ LogsStore->>SQLite: CREATE TABLE IF NOT EXISTS logs (version 1 in _migrations)
124
+ LogsStore->>SQLite: INSERT INTO logs
125
+ SQLite-->>LogsStore: lastInsertRowid
126
+ Home->>SQLite: COMMIT
129
127
  LogsStore-->>App: { id: 123 }
130
128
  ```
131
129
 
@@ -141,6 +139,12 @@ Since 0.2.0, an import of the package registers nothing. To register its procedu
141
139
  import "@procedures/sqlite/register";
142
140
  ```
143
141
 
142
+ ### The Node.js versions
143
+
144
+ The package needs Node.js 22.13 or later. Each procedure opens the file through the engine of `@mark1russell7/sqlite`, on `node:sqlite`.
145
+
146
+ `sqlite.query`, `sqlite.execute`, `sqlite.transaction` and `sqlite.migrate` need Node.js 24.10 or later: they refuse with `CONFINE_UNAVAILABLE` before it. They refuse before they open or make the file. The other procedures and `SqliteStorage` work on Node.js 22.13 or later.
147
+
144
148
  ## Configuration
145
149
 
146
150
  ### Default Database Path
@@ -202,6 +206,9 @@ interface DbQueryOutput<T = Record<string, unknown>> {
202
206
  }
203
207
  ```
204
208
 
209
+ - The procedure runs only the first statement of `sql`.
210
+ - The procedure only reads. A missing file reads as an empty database, and the procedure does not make it.
211
+
205
212
  **Example:**
206
213
  ```typescript
207
214
  const result = await client.exec(
@@ -252,6 +259,14 @@ interface DbExecuteOutput {
252
259
  }
253
260
  ```
254
261
 
262
+ - One statement runs alone. `changes` is the number of rows that it changed. A change of the schema gives `0`.
263
+ - A script of several statements runs in one transaction: all its statements take effect, or none does. `changes` is the total of the script.
264
+ - A script that starts with `BEGIN`, `SAVEPOINT` or another statement that controls a transaction runs as written. After an error, the procedure rolls back the open transaction.
265
+ - In a script that does not start with such a statement, a later one is refused. `VACUUM` cannot run in a transaction, so send it alone.
266
+ - SQL that leaves a transaction open fails, also `BEGIN` alone. The procedure rolls the transaction back, so the file keeps none of its changes. End each transaction with `COMMIT` or `ROLLBACK`.
267
+ - `params` bind to one statement. A script with `params` is refused.
268
+ - The procedure makes a missing file.
269
+
255
270
  **Examples:**
256
271
  ```typescript
257
272
  // INSERT
@@ -333,9 +348,11 @@ const { applied, skipped, version } = await client.exec(["sqlite", "migrate"], {
333
348
 
334
349
  #### `sqlite.backup`
335
350
 
336
- The procedure copies a database to a new file (`to`). It does not replace a file, and it does not write the source. With `vacuum: true`, it compacts the copy.
351
+ The procedure copies a database to a new file (`to`). It does not replace a file, and it does not write the source. A missing source is an error (`ENOENT`).
337
352
 
338
- This package uses SQLite in WebAssembly (sql.js). The database is in the memory of the module. `VACUUM INTO` and the backup API of SQLite write into the file system of the module, not to the disk. Thus the procedure writes the image of the database (its `export()`). It takes the image in the file queue of the database, where no other call of the process changes it. It writes the image to a file next to the target, checks it with `PRAGMA integrity_check`, and then renames it.
353
+ The procedure writes a snapshot of the database (`snapshot` of `@mark1russell7/sqlite`) to a temporary file next to the target. It checks the copy with `PRAGMA integrity_check`, and then it renames the copy. The copy has the changes in the WAL file of the source, and it is always compact.
354
+
355
+ The option `vacuum` is deprecated and has no effect. A later minor version removes it.
339
356
 
340
357
  ```typescript
341
358
  const { path, bytes } = await client.exec(["sqlite", "backup"], { dbPath: "./app.db", to: "./backups/app-2026-10-16.db" });
@@ -504,48 +521,35 @@ const users = new SqliteStorage<User>({ path: "./data/app.db", table: "users" })
504
521
  await users.set("u1", { name: "Ada" });
505
522
  await users.setBatch([["u2", { name: "Bob" }], ["u3", { name: "Cy" }]]);
506
523
  const changes = await users.getChangesSince(); // { version, full: true, changed, deleted: [] }
507
- await users.getEngine(); // "node" on Node.js 22.13 or later
524
+ await users.getEngine(); // "node"
508
525
  await users.close();
509
526
  ```
510
527
 
511
528
  The entry `@procedures/sqlite/storage` has the storage alone. The package root exports it too.
512
529
 
513
- ### The engines
514
-
515
- The storage has two SQLite engines. The option `engine` selects one.
516
-
517
- | `engine` | The engine | The file write |
518
- |---|---|---|
519
- | `node` | `node:sqlite` of Node.js, without a native build | SQLite writes only the changed pages. The file is in WAL mode. |
520
- | `sqljs` (deprecated) | sql.js (SQLite in WebAssembly), through `@mark1russell7/docker-sqlite` | The storage keeps the database in memory, and it writes the whole file after a write. |
521
- | `auto` (the default) | `node` when Node.js has `node:sqlite`, and else `sqljs` | |
530
+ ### The engine
522
531
 
523
- The engine `sqljs` is deprecated. Version 0.61.0 removes it, and the package then needs Node.js 22.13 or later.
532
+ The storage uses `node:sqlite` of Node.js, through the engine of `@mark1russell7/sqlite`. SQLite writes only the changed pages, and the file is in WAL mode. The option `engine` takes `auto` (the default) or `node`. Both select `node:sqlite`.
524
533
 
525
- - The first open of the engine `sqljs` in a process shows one `DeprecationWarning` with the code `PROCEDURES_SQLITE_SQLJS`. The engine `auto` on a Node.js without `node:sqlite` shows it too.
526
- - To prepare, use Node.js 22.13 or later with the engine `node` or `auto`. A file of the engine `sqljs` opens with the engine `node`.
527
- - The flag `--no-deprecation` of Node.js hides the warning.
534
+ Version 0.61.0 removed the engine `sqljs` (sql.js, SQLite in WebAssembly). The option `engine: "sqljs"` is a `TypeError`. A file of the engine `sqljs` opens with the engine `node`, with its items and its versions.
528
535
 
529
- - Node.js 22.13 and later have `node:sqlite` without a flag. Node.js 22.5 to 22.12 have it only with the flag `--experimental-sqlite`. Node.js 22.0 to 22.4 do not have it.
530
- - Node.js 22 shows the warning `ExperimentalWarning: SQLite is an experimental feature` at the first open of the engine `node`. Node.js 24 and 26 do not show it.
536
+ - Node.js 22.13 and later have `node:sqlite` without a flag.
537
+ - Node.js 22 shows the warning `ExperimentalWarning: SQLite is an experimental feature` at the first open. Node.js 24 and 26 do not show it.
531
538
  - Node.js 26.11 renamed the class `DatabaseSync` to `Database`. The storage uses `Database` when the module has it, and else `DatabaseSync`.
532
- - With `auto`, the storage uses `sqljs` when the import of `node:sqlite` fails. With `node`, the open fails then, and each operation gives the error.
533
- - The two engines use the same file format. A file that one engine writes opens with the other engine, with its items and its versions.
539
+ - When the import of `node:sqlite` fails, the open fails, and each operation gives the error.
534
540
  - `getEngine()` gives the engine of the storage. `getMetadata()` gives it in `stats.engine`. `isNodeSqliteAvailable()` tells if this Node.js has `node:sqlite`.
535
541
 
536
542
  <!-- measured
537
- The nightly benchmarks (`bench/storage.bench.ts`) measure the two engines in a file. These are the median times of [the nightly run of {run.date}]({run.url}), on the code of commit `{run.commit}` (`@procedures/sqlite` {engine:@procedures/sqlite}), with Node.js 26 on Linux. The site takes them from the newest nightly run, and its build fails when they are more than {run.limit} days old:
543
+ The nightly benchmarks (`bench/storage.bench.ts`) measure the storage in a file. These are the median times of [the nightly run of {run.date}]({run.url}), on the code of commit `{run.commit}` (`@procedures/sqlite` {engine:@procedures/sqlite}), with Node.js 26 on Linux. The site takes them from the newest nightly run, and its build fails when they are more than {run.limit} days old:
538
544
 
539
- | The benchmark | `node` | `sqljs` |
540
- |---|---|---|
541
- | 1,000 calls of `set()`, one after the other, in a table of 1,000 items | {nightly.sqlite-node-file-write-1000} | {nightly.sqlite-sqljs-file-write-1000} |
542
- | One `getAll()` of a table of 10,000 items | {nightly.sqlite-node-file-read-10000} | {nightly.sqlite-sqljs-file-read-10000} |
545
+ | The benchmark | The time |
546
+ |---|---|
547
+ | 1,000 calls of `set()`, one after the other, in a table of 1,000 items | {nightly.sqlite-node-file-write-1000} |
548
+ | One `getAll()` of a table of 10,000 items | {nightly.sqlite-node-file-read-10000} |
543
549
  -->
544
- The nightly benchmarks (`bench/storage.bench.ts`) measure the two engines in a file. [The page of this package on the site](https://mark1russell7.github.io/client/docs/packages/client-sqlite/) shows the times of the newest nightly run, with its date and its commit.
550
+ The nightly benchmarks (`bench/storage.bench.ts`) measure the storage in a file. [The page of this package on the site](https://mark1russell7.github.io/client/docs/packages/client-sqlite/) shows the times of the newest nightly run, with its date and its commit.
545
551
  <!-- /measured -->
546
552
 
547
- The engine `sqljs` writes the whole file at each write, so its write time grows with the size of the file.
548
-
549
553
  ### The table and the values
550
554
 
551
555
  - The option `path` is a database file, or `:memory:` for a database that lives as long as the storage.
@@ -572,32 +576,21 @@ The engine `sqljs` writes the whole file at each write, so its write time grows
572
576
  - Each write runs in a transaction with the change log. A batch that fails changes nothing.
573
577
  - A write ends after its change is in the file.
574
578
  - A storage that only reads does not make the file. The storage makes the file, and its folder, at the first write that changes an item.
575
- - `close()` writes the file and releases the database. After it, each operation rejects, also `flush()` and an empty batch. A second `close()` does nothing.
579
+ - `close()` releases the database. After it, each operation rejects, also `flush()` and an empty batch. A second `close()` does nothing.
576
580
  - The storages of one file in one process share one database. Thus the storages of two tables in one file keep their items.
577
- - The first storage of a file sets the engine of the shared database. A later storage with the engine `auto` uses it. A later storage with the other engine refuses it.
578
-
579
- The engine `node`:
580
-
581
581
  - The commit of a transaction writes the change to the file. `flush()` has nothing to write.
582
582
  - The file is in WAL mode, with `synchronous = NORMAL`. After a crash, the file stays consistent. A power loss can lose the last commits.
583
583
  - A write that cannot make the file fails, and it changes nothing.
584
584
  - A read-only file opens. The reads work, and each write fails.
585
585
 
586
- The engine `sqljs`:
587
-
588
- - The storage keeps the database in memory, and it writes the whole file after each write. When several writes wait, one file write holds their changes.
589
- - A failed file write fails the write. The change stays in memory, and `flush()` or the next write writes it.
590
- - The storage refuses a file with a write-ahead log (`<file>-wal`) of the engine `node`. The log can hold changes that are not in the file. Open the file with the engine `node` one time, or close the process that has it open.
591
-
592
586
  ### Two processes on one file
593
587
 
594
- - With the engine `node` in each process, two processes can use one file at the same time.
588
+ - Two processes can use one file at the same time.
595
589
  - SQLite locks the file for each write. A write waits for the lock of another process for `busyTimeout` (5 seconds by default). After it, the write fails with `database is locked`.
596
590
  - In WAL mode, a read does not wait for a write of another process.
597
591
  - Each write reads the version counter in its transaction, so the versions of the two processes agree. `getChangesSince()` reads in one read transaction.
598
592
  - A storage that opened before the file existed opens the file when another process makes it.
599
- - The engine `sqljs` does not coordinate with another process. Its whole-file write replaces the writes of the other process. Do not use it on a file that another process writes.
600
- - The procedures `sqlite.query` and `sqlite.execute` use sql.js, and they do not share the database of the storages. Do not write the file from them while a storage has it open.
593
+ - Each procedure opens the file through the engine of `@mark1russell7/sqlite`. A procedure waits for the lock as a storage does, so the procedures and the storages can use one file at the same time.
601
594
 
602
595
  ### The durable local copy of `HybridStorage`
603
596
 
@@ -719,91 +712,26 @@ logs.forEach(log => {
719
712
  });
720
713
  ```
721
714
 
722
- ### Direct Database Access (Without RPC)
715
+ ### Direct database access (without a procedure)
723
716
 
724
- For lower-level access, use re-exported utilities from `docker-sqlite`:
717
+ Version 0.61.0 removed the functions of `@mark1russell7/docker-sqlite` from this package. For direct access, use `@mark1russell7/sqlite`:
725
718
 
726
719
  ```typescript
727
- import {
728
- withConnection,
729
- query,
730
- execute,
731
- lastInsertRowId,
732
- runMigrations,
733
- createMigration
734
- } from "@procedures/sqlite";
735
-
736
- // Managed connection with automatic cleanup
737
- await withConnection({ dbPath: "./data/app.db" }, (db) => {
738
- // Execute mutations
739
- execute(db, "INSERT INTO users (name, email) VALUES (?, ?)", [
740
- "Bob Smith",
741
- "bob@example.com"
742
- ]);
743
-
744
- const userId = lastInsertRowId(db);
745
- console.log("Inserted user ID:", userId);
746
-
747
- // Query data
748
- const result = query<{ id: number; name: string; email: string }>(
749
- db,
750
- "SELECT * FROM users WHERE id = ?",
751
- [userId]
752
- );
720
+ import { openDatabase, transaction } from "@mark1russell7/sqlite";
753
721
 
754
- console.log(result.rows);
755
- });
756
-
757
- // Connection is automatically closed after callback
722
+ const db = openDatabase("./data/app.db");
723
+ try {
724
+ const id = transaction(db, () => {
725
+ const result = db.prepare("INSERT INTO users (name, email) VALUES (?, ?)").run("Bob Smith", "bob@example.com");
726
+ return Number(result.lastInsertRowid);
727
+ });
728
+ console.log(db.prepare("SELECT * FROM users WHERE id = ?").all(id));
729
+ } finally {
730
+ db.close();
731
+ }
758
732
  ```
759
733
 
760
- ### Database Migrations
761
-
762
- ```typescript
763
- import {
764
- withConnection,
765
- runMigrations,
766
- getMigrationStatus,
767
- rollbackMigration,
768
- type Migration
769
- } from "@procedures/sqlite";
770
-
771
- const migrations: Migration[] = [
772
- {
773
- version: 1,
774
- description: "Create users table",
775
- up: `
776
- CREATE TABLE users (
777
- id INTEGER PRIMARY KEY AUTOINCREMENT,
778
- name TEXT NOT NULL,
779
- email TEXT UNIQUE NOT NULL,
780
- created_at TEXT DEFAULT (datetime('now'))
781
- );
782
- CREATE INDEX idx_users_email ON users(email);
783
- `,
784
- down: "DROP TABLE users;"
785
- },
786
- {
787
- version: 2,
788
- description: "Add users.active column",
789
- up: "ALTER TABLE users ADD COLUMN active BOOLEAN DEFAULT 1;",
790
- down: "ALTER TABLE users DROP COLUMN active;"
791
- }
792
- ];
793
-
794
- await withConnection({ dbPath: "./app.db" }, (db) => {
795
- // Run migrations
796
- runMigrations(db, migrations);
797
-
798
- // Check migration status
799
- const status = getMigrationStatus(db);
800
- console.log("Current version:", status.currentVersion);
801
- console.log("Applied migrations:", status.appliedMigrations);
802
-
803
- // Rollback last migration if needed
804
- // rollbackMigration(db, migrations);
805
- });
806
- ```
734
+ For migrations, use the procedure `sqlite.migrate`, or `migrate` of `@mark1russell7/sqlite`.
807
735
 
808
736
  ### Type-Safe Queries
809
737
 
@@ -850,7 +778,7 @@ import "@procedures/s3/register.js";
850
778
  ### Runtime Dependencies
851
779
 
852
780
  - **@procedures/client** - Core client procedure system
853
- - **@mark1russell7/docker-sqlite** - SQLite connection and query utilities
781
+ - **@mark1russell7/sqlite** - The SQLite engine on `node:sqlite`: the open of a file, transactions, migrations and snapshots
854
782
 
855
783
  ### Development Dependencies
856
784
 
@@ -858,40 +786,6 @@ import "@procedures/s3/register.js";
858
786
  - **typescript** (^5.9.3) - TypeScript compiler
859
787
  - **@types/node** (^22.0.0) - Node.js type definitions
860
788
 
861
- ## Re-exported docker-sqlite Utilities
862
-
863
- `client-sqlite` re-exports all utilities from `docker-sqlite` for direct use:
864
-
865
- ```typescript
866
- // Connection management
867
- export { withConnection, createConnection } from "@mark1russell7/docker-sqlite";
868
-
869
- // Query execution
870
- export { query, execute, execMultiple, lastInsertRowId } from "@mark1russell7/docker-sqlite";
871
-
872
- // Table utilities
873
- export { tableExists } from "@mark1russell7/docker-sqlite";
874
-
875
- // Migration utilities
876
- export {
877
- getMigrationStatus,
878
- runMigrations,
879
- rollbackMigration,
880
- createMigration
881
- } from "@mark1russell7/docker-sqlite";
882
-
883
- // Types
884
- export type {
885
- ConnectionConfig,
886
- ConnectionCallback,
887
- QueryResult,
888
- ExecuteResult,
889
- Migration,
890
- MigrationStatus,
891
- Database
892
- } from "@mark1russell7/docker-sqlite";
893
- ```
894
-
895
789
  ## Package Structure
896
790
 
897
791
  ```
@@ -901,10 +795,11 @@ client-sqlite/
901
795
  │ ├── module.ts # The procedures as data, and the sqlite.query, sqlite.execute and logs.* handlers
902
796
  │ ├── register.ts # The ./register entry: registers the module
903
797
  │ ├── storage.ts # SqliteStorage, the collection storage (the ./storage entry)
904
- │ ├── engines.ts # The engines of SqliteStorage: node:sqlite and sql.js
798
+ │ ├── engines.ts # The engine of SqliteStorage: node:sqlite through @mark1russell7/sqlite
799
+ │ ├── load.ts # The load of @mark1russell7/sqlite
905
800
  │ ├── procedures/db/ # sqlite.tables, sqlite.schema, sqlite.transaction, sqlite.migrate, sqlite.backup
906
801
  │ └── index.ts # Main entry point
907
- ├── bench/ # The benchmarks of the engines (the nightly workflow runs them)
802
+ ├── bench/ # The benchmarks of the storage (the nightly workflow runs them)
908
803
  ├── dist/ # Compiled output
909
804
  ├── package.json
910
805
  ├── tsconfig.json
package/dist/engines.d.ts CHANGED
@@ -1,30 +1,22 @@
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
- import { type SqlConnection } from "@mark1russell7/sqlite/portable";
13
+ import type { SqlConnection } from "@mark1russell7/sqlite/portable";
14
+ import { type Sqlite } from "./load.js";
17
15
  /** The path of a database that is only in memory. */
18
16
  export declare const MEMORY_PATH = ":memory:";
19
- /**
20
- * The engine of a database: `node:sqlite` (`node`) or sql.js (`sqljs`). The engine `sqljs` is
21
- * deprecated. Version 0.61.0 removes it and needs Node.js 22.13 or later.
22
- */
23
- export type SqliteEngineName = "node" | "sqljs";
24
- /**
25
- * The engine choice of a `SqliteStorage`. With `auto`, the storage uses `node` when Node.js has
26
- * `node:sqlite`, and the deprecated engine `sqljs` when it does not.
27
- */
17
+ /** The engine of a database: `node:sqlite` (`node`). Version 0.61.0 removed the engine `sqljs`. */
18
+ export type SqliteEngineName = "node";
19
+ /** The engine choice of a `SqliteStorage`. `auto` and `node` both use `node:sqlite`. */
28
20
  export type SqliteEngine = "auto" | SqliteEngineName;
29
21
  /** This function gives the engine choice when it is known. Else it throws a `TypeError`. */
30
22
  export declare function checkEngine(engine: unknown): SqliteEngine;
@@ -45,33 +37,38 @@ export interface EngineDatabase {
45
37
  close(): void;
46
38
  }
47
39
  /**
48
- * This function gives true when this Node.js has `node:sqlite` without a flag (Node.js 22.13 or
49
- * later). Then the engine `auto` uses it.
40
+ * This function gives true when this Node.js has `node:sqlite` without a flag. Node.js 22.13 and
41
+ * later have it, and the package needs Node.js 22.13 or later.
50
42
  */
51
43
  export declare function isNodeSqliteAvailable(): Promise<boolean>;
52
44
  /**
53
- * This function copies the schema of the storages from one database to another. The node engine
45
+ * This function copies the schema of the storages from one database to another. The engine
54
46
  * calls it when it makes the file at the first write.
55
47
  */
56
48
  export type CopySchema = (from: EngineDatabase, to: EngineDatabase) => void;
57
- /** An open database of a path, with the file work of its engine. */
58
- export interface OpenDatabase {
49
+ /**
50
+ * An open database of a path. A missing file stays missing until the first write that changes an
51
+ * item: until then, the database is in memory.
52
+ */
53
+ export declare class OpenDatabase {
54
+ private readonly sqlite;
55
+ private readonly path;
56
+ private readonly busyTimeoutMs;
59
57
  readonly engine: SqliteEngineName;
60
- /** The current database. The node engine changes it when it opens the file. */
61
- readonly db: EngineDatabase;
58
+ /** The current database. It changes when the storage makes the file. */
59
+ db: EngineDatabase;
62
60
  /**
63
- * True when the node engine waits for the first write to make the file. Until then, the
64
- * database is in memory, and it has the tables and no item.
61
+ * True while the storage waits for the first write to make the file. Until then, the database
62
+ * is in memory, and it has the tables and no item.
65
63
  */
66
- readonly waiting: boolean;
67
- /** This method opens the file and copies the schema to it. The node engine uses it before the first write. */
64
+ waiting: boolean;
65
+ constructor(sqlite: Sqlite, path: string, busyTimeoutMs: number);
66
+ /** This method opens the file and copies the schema to it, before the first write. */
68
67
  makeFile(copy: CopySchema): void;
69
68
  /** This method opens a file that another process made after the open, before a read. */
70
69
  refresh(copy: CopySchema): void;
71
- /** This method writes the database to the file. Only sql.js has a write to do. */
72
- saveFile(): Promise<void>;
73
70
  close(): void;
74
71
  }
75
- /** This function opens the database of a path with an engine. `auto` uses `node` when it can. */
76
- export declare function openEngine(path: string, engine: SqliteEngine, busyTimeoutMs: number): Promise<OpenDatabase>;
72
+ /** This function opens the database of a path. */
73
+ export declare function openEngine(path: string, busyTimeoutMs: number): Promise<OpenDatabase>;
77
74
  //# sourceMappingURL=engines.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"engines.d.ts","sourceRoot":"","sources":["../src/engines.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAIH,OAAO,EAAmB,KAAK,aAAa,EAAqB,MAAM,gCAAgC,CAAC;AAEx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