@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.
- package/CHANGELOG.md +57 -0
- package/README.md +84 -183
- package/dist/engines.d.ts +31 -31
- package/dist/engines.d.ts.map +1 -1
- package/dist/engines.js +35 -78
- 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 -29
- 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 -132
- 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 -136
- package/src/types.ts +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,62 @@
|
|
|
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
|
+
|
|
36
|
+
## 0.60.1
|
|
37
|
+
|
|
38
|
+
### Patch Changes
|
|
39
|
+
|
|
40
|
+
- f2d045b: The engine `sqljs` of `SqliteStorage` is deprecated. Version 0.61.0 removes it and needs Node.js 22.13 or later.
|
|
41
|
+
|
|
42
|
+
- 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.
|
|
43
|
+
- 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`.
|
|
44
|
+
- The flag `--no-deprecation` of Node.js hides the warning.
|
|
45
|
+
- Updated dependencies [beca782]
|
|
46
|
+
- Updated dependencies [6dbc47e]
|
|
47
|
+
- Updated dependencies [663465c]
|
|
48
|
+
- Updated dependencies [66585f5]
|
|
49
|
+
- Updated dependencies [511d820]
|
|
50
|
+
- Updated dependencies [02a7074]
|
|
51
|
+
- Updated dependencies [265f41c]
|
|
52
|
+
- Updated dependencies [a87cdda]
|
|
53
|
+
- Updated dependencies [94b5f88]
|
|
54
|
+
- Updated dependencies [ca23351]
|
|
55
|
+
- Updated dependencies [84e1b25]
|
|
56
|
+
- Updated dependencies [fe6cd23]
|
|
57
|
+
- Updated dependencies [e9b7551]
|
|
58
|
+
- @procedures/client@0.60.1
|
|
59
|
+
|
|
3
60
|
## 0.60.0
|
|
4
61
|
|
|
5
62
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://opensource.org/licenses/MIT)
|
|
6
6
|
[](https://www.typescriptlang.org/)
|
|
7
|
-
[](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 "
|
|
54
|
-
|
|
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
|
|
59
|
-
|
|
58
|
+
subgraph "SQLite"
|
|
59
|
+
NodeSqlite["node:sqlite<br/>(Node.js 22.13 or later)"]
|
|
60
60
|
end
|
|
61
61
|
|
|
62
|
-
subgraph "
|
|
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 -->
|
|
71
|
-
Logs -->
|
|
70
|
+
DB --> Home
|
|
71
|
+
Logs --> Home
|
|
72
72
|
Logs --> Migrations
|
|
73
|
+
Storage --> Home
|
|
73
74
|
|
|
74
|
-
|
|
75
|
-
Utilities --> SqlJs
|
|
75
|
+
Home --> NodeSqlite
|
|
76
76
|
|
|
77
|
-
|
|
78
|
-
|
|
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
|
-
|
|
96
|
-
|
|
95
|
+
Home["@mark1russell7/sqlite"]
|
|
96
|
+
NodeSqlite["node:sqlite"]
|
|
97
97
|
|
|
98
98
|
App --> ClientSqlite
|
|
99
99
|
ClientSqlite --> Client
|
|
100
|
-
ClientSqlite -->
|
|
101
|
-
|
|
100
|
+
ClientSqlite --> Home
|
|
101
|
+
Home --> NodeSqlite
|
|
102
102
|
|
|
103
103
|
App -.client.call.-> Client
|
|
104
104
|
Client -.executes.-> ClientSqlite
|
|
105
|
-
ClientSqlite -.
|
|
106
|
-
|
|
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
|
|
116
|
-
participant
|
|
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->>
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
LogsStore->>
|
|
126
|
-
|
|
127
|
-
SQLite
|
|
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.
|
|
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
|
-
|
|
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,42 +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"
|
|
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
|
|
530
|
+
### The engine
|
|
514
531
|
|
|
515
|
-
The storage
|
|
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`.
|
|
516
533
|
|
|
517
|
-
|
|
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` | 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` | |
|
|
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.
|
|
522
535
|
|
|
523
|
-
- Node.js 22.13 and later have `node:sqlite` without a flag.
|
|
524
|
-
- Node.js 22 shows the warning `ExperimentalWarning: SQLite is an experimental feature` at the first open
|
|
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.
|
|
525
538
|
- Node.js 26.11 renamed the class `DatabaseSync` to `Database`. The storage uses `Database` when the module has it, and else `DatabaseSync`.
|
|
526
|
-
-
|
|
527
|
-
- 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.
|
|
528
540
|
- `getEngine()` gives the engine of the storage. `getMetadata()` gives it in `stats.engine`. `isNodeSqliteAvailable()` tells if this Node.js has `node:sqlite`.
|
|
529
541
|
|
|
530
542
|
<!-- measured
|
|
531
|
-
The nightly benchmarks (`bench/storage.bench.ts`) measure the
|
|
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:
|
|
532
544
|
|
|
533
|
-
| The benchmark |
|
|
534
|
-
|
|
535
|
-
| 1,000 calls of `set()`, one after the other, in a table of 1,000 items | {nightly.sqlite-node-file-write-1000} |
|
|
536
|
-
| One `getAll()` of a table of 10,000 items | {nightly.sqlite-node-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} |
|
|
537
549
|
-->
|
|
538
|
-
The nightly benchmarks (`bench/storage.bench.ts`) measure the
|
|
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.
|
|
539
551
|
<!-- /measured -->
|
|
540
552
|
|
|
541
|
-
The engine `sqljs` writes the whole file at each write, so its write time grows with the size of the file.
|
|
542
|
-
|
|
543
553
|
### The table and the values
|
|
544
554
|
|
|
545
555
|
- The option `path` is a database file, or `:memory:` for a database that lives as long as the storage.
|
|
@@ -566,32 +576,21 @@ The engine `sqljs` writes the whole file at each write, so its write time grows
|
|
|
566
576
|
- Each write runs in a transaction with the change log. A batch that fails changes nothing.
|
|
567
577
|
- A write ends after its change is in the file.
|
|
568
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.
|
|
569
|
-
- `close()`
|
|
579
|
+
- `close()` releases the database. After it, each operation rejects, also `flush()` and an empty batch. A second `close()` does nothing.
|
|
570
580
|
- The storages of one file in one process share one database. Thus the storages of two tables in one file keep their items.
|
|
571
|
-
- 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.
|
|
572
|
-
|
|
573
|
-
The engine `node`:
|
|
574
|
-
|
|
575
581
|
- The commit of a transaction writes the change to the file. `flush()` has nothing to write.
|
|
576
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.
|
|
577
583
|
- A write that cannot make the file fails, and it changes nothing.
|
|
578
584
|
- A read-only file opens. The reads work, and each write fails.
|
|
579
585
|
|
|
580
|
-
The engine `sqljs`:
|
|
581
|
-
|
|
582
|
-
- 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.
|
|
583
|
-
- A failed file write fails the write. The change stays in memory, and `flush()` or the next write writes it.
|
|
584
|
-
- 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.
|
|
585
|
-
|
|
586
586
|
### Two processes on one file
|
|
587
587
|
|
|
588
|
-
-
|
|
588
|
+
- Two processes can use one file at the same time.
|
|
589
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`.
|
|
590
590
|
- In WAL mode, a read does not wait for a write of another process.
|
|
591
591
|
- Each write reads the version counter in its transaction, so the versions of the two processes agree. `getChangesSince()` reads in one read transaction.
|
|
592
592
|
- A storage that opened before the file existed opens the file when another process makes it.
|
|
593
|
-
-
|
|
594
|
-
- 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.
|
|
595
594
|
|
|
596
595
|
### The durable local copy of `HybridStorage`
|
|
597
596
|
|
|
@@ -713,91 +712,26 @@ logs.forEach(log => {
|
|
|
713
712
|
});
|
|
714
713
|
```
|
|
715
714
|
|
|
716
|
-
### Direct
|
|
715
|
+
### Direct database access (without a procedure)
|
|
717
716
|
|
|
718
|
-
|
|
717
|
+
Version 0.61.0 removed the functions of `@mark1russell7/docker-sqlite` from this package. For direct access, use `@mark1russell7/sqlite`:
|
|
719
718
|
|
|
720
719
|
```typescript
|
|
721
|
-
import {
|
|
722
|
-
withConnection,
|
|
723
|
-
query,
|
|
724
|
-
execute,
|
|
725
|
-
lastInsertRowId,
|
|
726
|
-
runMigrations,
|
|
727
|
-
createMigration
|
|
728
|
-
} from "@procedures/sqlite";
|
|
729
|
-
|
|
730
|
-
// Managed connection with automatic cleanup
|
|
731
|
-
await withConnection({ dbPath: "./data/app.db" }, (db) => {
|
|
732
|
-
// Execute mutations
|
|
733
|
-
execute(db, "INSERT INTO users (name, email) VALUES (?, ?)", [
|
|
734
|
-
"Bob Smith",
|
|
735
|
-
"bob@example.com"
|
|
736
|
-
]);
|
|
737
|
-
|
|
738
|
-
const userId = lastInsertRowId(db);
|
|
739
|
-
console.log("Inserted user ID:", userId);
|
|
740
|
-
|
|
741
|
-
// Query data
|
|
742
|
-
const result = query<{ id: number; name: string; email: string }>(
|
|
743
|
-
db,
|
|
744
|
-
"SELECT * FROM users WHERE id = ?",
|
|
745
|
-
[userId]
|
|
746
|
-
);
|
|
747
|
-
|
|
748
|
-
console.log(result.rows);
|
|
749
|
-
});
|
|
720
|
+
import { openDatabase, transaction } from "@mark1russell7/sqlite";
|
|
750
721
|
|
|
751
|
-
|
|
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
|
+
}
|
|
752
732
|
```
|
|
753
733
|
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
```typescript
|
|
757
|
-
import {
|
|
758
|
-
withConnection,
|
|
759
|
-
runMigrations,
|
|
760
|
-
getMigrationStatus,
|
|
761
|
-
rollbackMigration,
|
|
762
|
-
type Migration
|
|
763
|
-
} from "@procedures/sqlite";
|
|
764
|
-
|
|
765
|
-
const migrations: Migration[] = [
|
|
766
|
-
{
|
|
767
|
-
version: 1,
|
|
768
|
-
description: "Create users table",
|
|
769
|
-
up: `
|
|
770
|
-
CREATE TABLE users (
|
|
771
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
772
|
-
name TEXT NOT NULL,
|
|
773
|
-
email TEXT UNIQUE NOT NULL,
|
|
774
|
-
created_at TEXT DEFAULT (datetime('now'))
|
|
775
|
-
);
|
|
776
|
-
CREATE INDEX idx_users_email ON users(email);
|
|
777
|
-
`,
|
|
778
|
-
down: "DROP TABLE users;"
|
|
779
|
-
},
|
|
780
|
-
{
|
|
781
|
-
version: 2,
|
|
782
|
-
description: "Add users.active column",
|
|
783
|
-
up: "ALTER TABLE users ADD COLUMN active BOOLEAN DEFAULT 1;",
|
|
784
|
-
down: "ALTER TABLE users DROP COLUMN active;"
|
|
785
|
-
}
|
|
786
|
-
];
|
|
787
|
-
|
|
788
|
-
await withConnection({ dbPath: "./app.db" }, (db) => {
|
|
789
|
-
// Run migrations
|
|
790
|
-
runMigrations(db, migrations);
|
|
791
|
-
|
|
792
|
-
// Check migration status
|
|
793
|
-
const status = getMigrationStatus(db);
|
|
794
|
-
console.log("Current version:", status.currentVersion);
|
|
795
|
-
console.log("Applied migrations:", status.appliedMigrations);
|
|
796
|
-
|
|
797
|
-
// Rollback last migration if needed
|
|
798
|
-
// rollbackMigration(db, migrations);
|
|
799
|
-
});
|
|
800
|
-
```
|
|
734
|
+
For migrations, use the procedure `sqlite.migrate`, or `migrate` of `@mark1russell7/sqlite`.
|
|
801
735
|
|
|
802
736
|
### Type-Safe Queries
|
|
803
737
|
|
|
@@ -844,7 +778,7 @@ import "@procedures/s3/register.js";
|
|
|
844
778
|
### Runtime Dependencies
|
|
845
779
|
|
|
846
780
|
- **@procedures/client** - Core client procedure system
|
|
847
|
-
- **@mark1russell7/
|
|
781
|
+
- **@mark1russell7/sqlite** - The SQLite engine on `node:sqlite`: the open of a file, transactions, migrations and snapshots
|
|
848
782
|
|
|
849
783
|
### Development Dependencies
|
|
850
784
|
|
|
@@ -852,40 +786,6 @@ import "@procedures/s3/register.js";
|
|
|
852
786
|
- **typescript** (^5.9.3) - TypeScript compiler
|
|
853
787
|
- **@types/node** (^22.0.0) - Node.js type definitions
|
|
854
788
|
|
|
855
|
-
## Re-exported docker-sqlite Utilities
|
|
856
|
-
|
|
857
|
-
`client-sqlite` re-exports all utilities from `docker-sqlite` for direct use:
|
|
858
|
-
|
|
859
|
-
```typescript
|
|
860
|
-
// Connection management
|
|
861
|
-
export { withConnection, createConnection } from "@mark1russell7/docker-sqlite";
|
|
862
|
-
|
|
863
|
-
// Query execution
|
|
864
|
-
export { query, execute, execMultiple, lastInsertRowId } from "@mark1russell7/docker-sqlite";
|
|
865
|
-
|
|
866
|
-
// Table utilities
|
|
867
|
-
export { tableExists } from "@mark1russell7/docker-sqlite";
|
|
868
|
-
|
|
869
|
-
// Migration utilities
|
|
870
|
-
export {
|
|
871
|
-
getMigrationStatus,
|
|
872
|
-
runMigrations,
|
|
873
|
-
rollbackMigration,
|
|
874
|
-
createMigration
|
|
875
|
-
} from "@mark1russell7/docker-sqlite";
|
|
876
|
-
|
|
877
|
-
// Types
|
|
878
|
-
export type {
|
|
879
|
-
ConnectionConfig,
|
|
880
|
-
ConnectionCallback,
|
|
881
|
-
QueryResult,
|
|
882
|
-
ExecuteResult,
|
|
883
|
-
Migration,
|
|
884
|
-
MigrationStatus,
|
|
885
|
-
Database
|
|
886
|
-
} from "@mark1russell7/docker-sqlite";
|
|
887
|
-
```
|
|
888
|
-
|
|
889
789
|
## Package Structure
|
|
890
790
|
|
|
891
791
|
```
|
|
@@ -895,10 +795,11 @@ client-sqlite/
|
|
|
895
795
|
│ ├── module.ts # The procedures as data, and the sqlite.query, sqlite.execute and logs.* handlers
|
|
896
796
|
│ ├── register.ts # The ./register entry: registers the module
|
|
897
797
|
│ ├── storage.ts # SqliteStorage, the collection storage (the ./storage entry)
|
|
898
|
-
│ ├── engines.ts # The
|
|
798
|
+
│ ├── engines.ts # The engine of SqliteStorage: node:sqlite through @mark1russell7/sqlite
|
|
799
|
+
│ ├── load.ts # The load of @mark1russell7/sqlite
|
|
899
800
|
│ ├── procedures/db/ # sqlite.tables, sqlite.schema, sqlite.transaction, sqlite.migrate, sqlite.backup
|
|
900
801
|
│ └── index.ts # Main entry point
|
|
901
|
-
├── bench/ # The benchmarks of the
|
|
802
|
+
├── bench/ # The benchmarks of the storage (the nightly workflow runs them)
|
|
902
803
|
├── dist/ # Compiled output
|
|
903
804
|
├── package.json
|
|
904
805
|
├── tsconfig.json
|
package/dist/engines.d.ts
CHANGED
|
@@ -1,27 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The
|
|
3
|
-
* `
|
|
4
|
-
* transactions of
|
|
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
|
-
*
|
|
7
|
-
*
|
|
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.
|
|
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 {
|
|
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
|
-
/** The engine of a database: `node:sqlite` (`node`)
|
|
20
|
-
export type SqliteEngineName = "node"
|
|
21
|
-
/**
|
|
22
|
-
* The engine choice of a `SqliteStorage`. With `auto`, the storage uses `node` when Node.js has
|
|
23
|
-
* `node:sqlite`, and `sqljs` when it does not.
|
|
24
|
-
*/
|
|
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`. */
|
|
25
20
|
export type SqliteEngine = "auto" | SqliteEngineName;
|
|
26
21
|
/** This function gives the engine choice when it is known. Else it throws a `TypeError`. */
|
|
27
22
|
export declare function checkEngine(engine: unknown): SqliteEngine;
|
|
@@ -42,33 +37,38 @@ export interface EngineDatabase {
|
|
|
42
37
|
close(): void;
|
|
43
38
|
}
|
|
44
39
|
/**
|
|
45
|
-
* This function gives true when this Node.js has `node:sqlite` without a flag
|
|
46
|
-
* later
|
|
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.
|
|
47
42
|
*/
|
|
48
43
|
export declare function isNodeSqliteAvailable(): Promise<boolean>;
|
|
49
44
|
/**
|
|
50
|
-
* This function copies the schema of the storages from one database to another. The
|
|
45
|
+
* This function copies the schema of the storages from one database to another. The engine
|
|
51
46
|
* calls it when it makes the file at the first write.
|
|
52
47
|
*/
|
|
53
48
|
export type CopySchema = (from: EngineDatabase, to: EngineDatabase) => void;
|
|
54
|
-
/**
|
|
55
|
-
|
|
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;
|
|
56
57
|
readonly engine: SqliteEngineName;
|
|
57
|
-
/** The current database.
|
|
58
|
-
|
|
58
|
+
/** The current database. It changes when the storage makes the file. */
|
|
59
|
+
db: EngineDatabase;
|
|
59
60
|
/**
|
|
60
|
-
* True
|
|
61
|
-
*
|
|
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.
|
|
62
63
|
*/
|
|
63
|
-
|
|
64
|
-
|
|
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. */
|
|
65
67
|
makeFile(copy: CopySchema): void;
|
|
66
68
|
/** This method opens a file that another process made after the open, before a read. */
|
|
67
69
|
refresh(copy: CopySchema): void;
|
|
68
|
-
/** This method writes the database to the file. Only sql.js has a write to do. */
|
|
69
|
-
saveFile(): Promise<void>;
|
|
70
70
|
close(): void;
|
|
71
71
|
}
|
|
72
|
-
/** This function opens the database of a path
|
|
73
|
-
export declare function openEngine(path: string,
|
|
72
|
+
/** This function opens the database of a path. */
|
|
73
|
+
export declare function openEngine(path: string, busyTimeoutMs: number): Promise<OpenDatabase>;
|
|
74
74
|
//# sourceMappingURL=engines.d.ts.map
|
package/dist/engines.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engines.d.ts","sourceRoot":"","sources":["../src/engines.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"engines.d.ts","sourceRoot":"","sources":["../src/engines.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,KAAK,EAAE,aAAa,EAAgB,MAAM,gCAAgC,CAAC;AAClF,OAAO,EAAc,KAAK,MAAM,EAAE,MAAM,WAAW,CAAC;AAEpD,qDAAqD;AACrD,eAAO,MAAM,WAAW,aAAa,CAAC;AAEtC,mGAAmG;AACnG,MAAM,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAEtC,wFAAwF;AACxF,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,gBAAgB,CAAC;AAIrD,4FAA4F;AAC5F,wBAAgB,WAAW,CAAC,MAAM,EAAE,OAAO,GAAG,YAAY,CAUzD;AAED,+CAA+C;AAC/C,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;AAK9C,mFAAmF;AACnF,MAAM,WAAW,cAAc;IAC7B,gGAAgG;IAChG,QAAQ,CAAC,UAAU,EAAE,aAAa,CAAC;IACnC,uCAAuC;IACvC,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,QAAQ,EAAE,GAAG,CAAC,EAAE,CAAC;IAC9C,yFAAyF;IACzF,WAAW,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,GAAG,KAAK,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC;IACxE,oFAAoF;IACpF,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,QAAQ,EAAE,GAAG,MAAM,CAAC;IAC9C,kEAAkE;IAClE,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,KAAK,IAAI,IAAI,CAAC;CACf;AA6DD;;;GAGG;AACH,wBAAsB,qBAAqB,IAAI,OAAO,CAAC,OAAO,CAAC,CAE9D;AAyBD;;;GAGG;AACH,MAAM,MAAM,UAAU,GAAG,CAAC,IAAI,EAAE,cAAc,EAAE,EAAE,EAAE,cAAc,KAAK,IAAI,CAAC;AAE5E;;;GAGG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IAEvC,QAAQ,CAAC,MAAM,EAAE,gBAAgB,CAAU;IAC3C,wEAAwE;IACxE,EAAE,EAAE,cAAc,CAAC;IACnB;;;OAGG;IACH,OAAO,EAAE,OAAO,CAAC;IAEjB,YAAY,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAc9D;IAED,sFAAsF;IACtF,QAAQ,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,CAY/B;IAED,wFAAwF;IACxF,OAAO,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI,CAE9B;IAED,KAAK,IAAI,IAAI,CAEZ;CACF;AASD,kDAAkD;AAClD,wBAAsB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,CAI3F"}
|