@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.
- package/CHANGELOG.md +33 -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/dist/engines.js
CHANGED
|
@@ -1,48 +1,31 @@
|
|
|
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. 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 { existsSync
|
|
17
|
-
import {
|
|
18
|
-
import { sqlJsConnection } from "@mark1russell7/sqlite/portable";
|
|
13
|
+
import { existsSync } from "node:fs";
|
|
14
|
+
import { loadSqlite } from "./load.js";
|
|
19
15
|
/** The path of a database that is only in memory. */
|
|
20
16
|
export const MEMORY_PATH = ":memory:";
|
|
21
|
-
|
|
22
|
-
const SQLJS_DEPRECATION_CODE = "PROCEDURES_SQLITE_SQLJS";
|
|
23
|
-
let sqlJsWarned = false;
|
|
24
|
-
/**
|
|
25
|
-
* This function shows the deprecation warning of the engine `sqljs`, one time in a process. The
|
|
26
|
-
* flag `--no-deprecation` of Node.js hides it.
|
|
27
|
-
*/
|
|
28
|
-
function warnSqlJs(engine) {
|
|
29
|
-
if (sqlJsWarned)
|
|
30
|
-
return;
|
|
31
|
-
sqlJsWarned = true;
|
|
32
|
-
const cause = engine === "sqljs"
|
|
33
|
-
? "The SQLite engine sqljs is deprecated"
|
|
34
|
-
: `This Node.js (${process.version}) has no node:sqlite, so the engine auto uses the deprecated engine sqljs`;
|
|
35
|
-
process.emitWarning(`${cause}. Version 0.61.0 of @procedures/sqlite removes it: use Node.js 22.13 or later, with the engine node or auto.`, { type: "DeprecationWarning", code: SQLJS_DEPRECATION_CODE });
|
|
36
|
-
}
|
|
37
|
-
const ENGINES = new Set(["auto", "node", "sqljs"]);
|
|
17
|
+
const ENGINES = new Set(["auto", "node"]);
|
|
38
18
|
/** This function gives the engine choice when it is known. Else it throws a `TypeError`. */
|
|
39
19
|
export function checkEngine(engine) {
|
|
20
|
+
if (engine === "sqljs") {
|
|
21
|
+
throw new TypeError("The SQLite engine sqljs was removed in version 0.61.0: use auto or node. A file of the engine sqljs opens with the engine node.");
|
|
22
|
+
}
|
|
40
23
|
if (typeof engine !== "string" || !ENGINES.has(engine)) {
|
|
41
|
-
throw new TypeError(`Unknown SQLite engine ${JSON.stringify(engine)}: use auto
|
|
24
|
+
throw new TypeError(`Unknown SQLite engine ${JSON.stringify(engine)}: use auto or node`);
|
|
42
25
|
}
|
|
43
26
|
return engine;
|
|
44
27
|
}
|
|
45
|
-
/** A database of
|
|
28
|
+
/** A database of the engine. It keeps one prepared statement for each SQL text. */
|
|
46
29
|
class ConnectionDatabase {
|
|
47
30
|
connection;
|
|
48
31
|
statements = new Map();
|
|
@@ -82,7 +65,7 @@ let nodeModule;
|
|
|
82
65
|
* `node:sqlite` only with the flag `--experimental-sqlite`.
|
|
83
66
|
*/
|
|
84
67
|
function loadNodeSqlite() {
|
|
85
|
-
nodeModule ??=
|
|
68
|
+
nodeModule ??= loadSqlite()
|
|
86
69
|
.then((module) => {
|
|
87
70
|
module.openDatabase(MEMORY_PATH, { wal: false }).close();
|
|
88
71
|
return module;
|
|
@@ -91,8 +74,8 @@ function loadNodeSqlite() {
|
|
|
91
74
|
return nodeModule;
|
|
92
75
|
}
|
|
93
76
|
/**
|
|
94
|
-
* This function gives true when this Node.js has `node:sqlite` without a flag
|
|
95
|
-
* later
|
|
77
|
+
* This function gives true when this Node.js has `node:sqlite` without a flag. Node.js 22.13 and
|
|
78
|
+
* later have it, and the package needs Node.js 22.13 or later.
|
|
96
79
|
*/
|
|
97
80
|
export async function isNodeSqliteAvailable() {
|
|
98
81
|
return (await loadNodeSqlite()) !== undefined;
|
|
@@ -117,15 +100,20 @@ function openNodeMemory(sqlite) {
|
|
|
117
100
|
return new ConnectionDatabase(sqlite.openDatabase(MEMORY_PATH, { wal: false }));
|
|
118
101
|
}
|
|
119
102
|
/**
|
|
120
|
-
*
|
|
121
|
-
*
|
|
103
|
+
* An open database of a path. A missing file stays missing until the first write that changes an
|
|
104
|
+
* item: until then, the database is in memory.
|
|
122
105
|
*/
|
|
123
|
-
class
|
|
106
|
+
export class OpenDatabase {
|
|
124
107
|
sqlite;
|
|
125
108
|
path;
|
|
126
109
|
busyTimeoutMs;
|
|
127
110
|
engine = "node";
|
|
111
|
+
/** The current database. It changes when the storage makes the file. */
|
|
128
112
|
db;
|
|
113
|
+
/**
|
|
114
|
+
* True while the storage waits for the first write to make the file. Until then, the database
|
|
115
|
+
* is in memory, and it has the tables and no item.
|
|
116
|
+
*/
|
|
129
117
|
waiting;
|
|
130
118
|
constructor(sqlite, path, busyTimeoutMs) {
|
|
131
119
|
this.sqlite = sqlite;
|
|
@@ -144,6 +132,7 @@ class NodeOpenDatabase {
|
|
|
144
132
|
this.waiting = true;
|
|
145
133
|
}
|
|
146
134
|
}
|
|
135
|
+
/** This method opens the file and copies the schema to it, before the first write. */
|
|
147
136
|
makeFile(copy) {
|
|
148
137
|
if (!this.waiting)
|
|
149
138
|
return;
|
|
@@ -159,73 +148,24 @@ class NodeOpenDatabase {
|
|
|
159
148
|
this.db = file;
|
|
160
149
|
this.waiting = false;
|
|
161
150
|
}
|
|
151
|
+
/** This method opens a file that another process made after the open, before a read. */
|
|
162
152
|
refresh(copy) {
|
|
163
153
|
if (this.waiting && existsSync(this.path))
|
|
164
154
|
this.makeFile(copy);
|
|
165
155
|
}
|
|
166
|
-
saveFile() {
|
|
167
|
-
return Promise.resolve();
|
|
168
|
-
}
|
|
169
156
|
close() {
|
|
170
157
|
this.db.close();
|
|
171
158
|
}
|
|
172
159
|
}
|
|
173
|
-
|
|
174
|
-
// sql.js
|
|
175
|
-
// ---------------------------------------------------------------------------
|
|
176
|
-
/** A database of sql.js: the database is in memory, and `saveFile()` writes the whole file. */
|
|
177
|
-
class SqlJsOpenDatabase {
|
|
178
|
-
db;
|
|
179
|
-
save;
|
|
180
|
-
engine = "sqljs";
|
|
181
|
-
waiting = false;
|
|
182
|
-
constructor(db, save) {
|
|
183
|
-
this.db = db;
|
|
184
|
-
this.save = save;
|
|
185
|
-
}
|
|
186
|
-
makeFile() { }
|
|
187
|
-
refresh() { }
|
|
188
|
-
saveFile() {
|
|
189
|
-
return this.save();
|
|
190
|
-
}
|
|
191
|
-
close() {
|
|
192
|
-
this.db.close();
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
/**
|
|
196
|
-
* sql.js reads only the main file. A write-ahead log of the node engine (`<file>-wal`) can hold
|
|
197
|
-
* changes that are not in the main file yet. A whole-file write of sql.js does not agree with the
|
|
198
|
-
* log. Thus sql.js refuses such a file.
|
|
199
|
-
*/
|
|
200
|
-
function checkNoWriteAheadLog(path) {
|
|
201
|
-
let size = 0;
|
|
202
|
-
try {
|
|
203
|
-
size = statSync(`${path}-wal`).size;
|
|
204
|
-
}
|
|
205
|
-
catch {
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
if (size > 0) {
|
|
209
|
-
throw new Error(`The file ${path} has a write-ahead log (${path}-wal) of the engine node: open it with the engine node, or close the process that uses it`);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
/** The error of the engine `node` when this Node.js has no `node:sqlite`. */
|
|
160
|
+
/** The error when this Node.js has no `node:sqlite` without a flag. */
|
|
213
161
|
function noNodeSqlite() {
|
|
214
|
-
return new Error(`
|
|
162
|
+
return new Error(`SqliteStorage needs node:sqlite, and this Node.js (${process.version}) does not have it without a flag: use Node.js 22.13 or later`);
|
|
215
163
|
}
|
|
216
|
-
/** This function opens the database of a path
|
|
217
|
-
export async function openEngine(path,
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
if (engine === "node")
|
|
223
|
-
throw noNodeSqlite();
|
|
224
|
-
}
|
|
225
|
-
warnSqlJs(engine);
|
|
226
|
-
if (path !== MEMORY_PATH)
|
|
227
|
-
checkNoWriteAheadLog(path);
|
|
228
|
-
const { db, save } = await createConnection({ dbPath: path });
|
|
229
|
-
return new SqlJsOpenDatabase(new ConnectionDatabase(sqlJsConnection(db)), save);
|
|
164
|
+
/** This function opens the database of a path. */
|
|
165
|
+
export async function openEngine(path, busyTimeoutMs) {
|
|
166
|
+
const sqlite = await loadNodeSqlite();
|
|
167
|
+
if (!sqlite)
|
|
168
|
+
throw noNodeSqlite();
|
|
169
|
+
return new OpenDatabase(sqlite, path, busyTimeoutMs);
|
|
230
170
|
}
|
|
231
171
|
//# sourceMappingURL=engines.js.map
|
package/dist/engines.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engines.js","sourceRoot":"","sources":["../src/engines.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"engines.js","sourceRoot":"","sources":["../src/engines.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAErC,OAAO,EAAE,UAAU,EAAe,MAAM,WAAW,CAAC;AAEpD,qDAAqD;AACrD,MAAM,CAAC,MAAM,WAAW,GAAG,UAAU,CAAC;AAQtC,MAAM,OAAO,GAAwB,IAAI,GAAG,CAAe,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;AAE7E,4FAA4F;AAC5F,MAAM,UAAU,WAAW,CAAC,MAAe;IACzC,IAAI,MAAM,KAAK,OAAO,EAAE,CAAC;QACvB,MAAM,IAAI,SAAS,CACjB,iIAAiI,CAClI,CAAC;IACJ,CAAC;IACD,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC;QACvD,MAAM,IAAI,SAAS,CAAC,yBAAyB,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;IAC3F,CAAC;IACD,OAAO,MAAsB,CAAC;AAChC,CAAC;AAuBD,mFAAmF;AACnF,MAAM,kBAAkB;IACb,UAAU,CAAmB;IAErB,UAAU,GAAG,IAAI,GAAG,EAAwB,CAAC;IAE9D,YAAY,UAA4B;QACtC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;IAED,GAAG,CAAI,GAAW,EAAE,MAAM,GAAe,EAAE;QACzC,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,GAAG,MAAM,CAAQ,CAAC;IACnD,CAAC;IAED,WAAW,CAAI,GAAW,EAAE,MAAoB;QAC9C,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;QACtC,OAAO,MAAM,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AAE,CAAC;IACtC,IAAI,CAAC,MAAM;QAAE,MAAM,YAAY,EAAE,CAAC;IAClC,OAAO,IAAI,YAAY,CAAC,MAAM,EAAE,IAAI,EAAE,aAAa,CAAC,CAAC;AACvD,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* @procedures/sqlite
|
|
3
3
|
*
|
|
4
|
-
* SQLite
|
|
5
|
-
*
|
|
4
|
+
* The SQLite procedures and the SQLite collection storage. Each procedure opens the file through the engine of
|
|
5
|
+
* `@mark1russell7/sqlite`. That package has the functions of a connection (`openDatabase`, `migrate`, `transaction`).
|
|
6
6
|
*
|
|
7
7
|
* @example
|
|
8
8
|
* ```typescript
|
|
@@ -38,6 +38,4 @@ export { DEFAULT_DB_PATH, LOG_LEVELS } from "./types.js";
|
|
|
38
38
|
export { dbTables, dbSchema, dbTransaction, dbMigrate, dbBackup } from "./procedures/db/index.js";
|
|
39
39
|
export { registerSqliteProcedures } from "./module.js";
|
|
40
40
|
export { SqliteStorage, checkTableName, isNodeSqliteAvailable, MEMORY_PATH, type SqliteEngine, type SqliteEngineName, type SqliteStorageOptions, } from "./storage.js";
|
|
41
|
-
export { withConnection, createConnection, query, execute, execMultiple, lastInsertRowId, tableExists, getMigrationStatus, runMigrations, rollbackMigration, createMigration, } from "@mark1russell7/docker-sqlite";
|
|
42
|
-
export type { ConnectionConfig, ConnectionCallback, QueryResult, ExecuteResult, Migration, MigrationStatus, Database, } from "@mark1russell7/docker-sqlite";
|
|
43
41
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAMH,YAAY,EACV,YAAY,EACZ,aAAa,EACb,cAAc,EACd,eAA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AAa,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAMlG,OAAO,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAMvD,OAAO,EACL,aAAa,EACb,cAAc,EACd,qBAAqB,EACrB,WAAW,EACX,KAAK,YAAY,EACjB,KAAK,gBAAgB,EACrB,KAAK,oBAAoB,GAC1B,MAAM,cAAc,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* @procedures/sqlite
|
|
3
3
|
*
|
|
4
|
-
* SQLite
|
|
5
|
-
*
|
|
4
|
+
* The SQLite procedures and the SQLite collection storage. Each procedure opens the file through the engine of
|
|
5
|
+
* `@mark1russell7/sqlite`. That package has the functions of a connection (`openDatabase`, `migrate`, `transaction`).
|
|
6
6
|
*
|
|
7
7
|
* @example
|
|
8
8
|
* ```typescript
|
|
@@ -44,8 +44,4 @@ export { registerSqliteProcedures } from "./module.js";
|
|
|
44
44
|
// The collection storage (roadmap R45). The entry @procedures/sqlite/storage has it alone.
|
|
45
45
|
// =============================================================================
|
|
46
46
|
export { SqliteStorage, checkTableName, isNodeSqliteAvailable, MEMORY_PATH, } from "./storage.js";
|
|
47
|
-
// =============================================================================
|
|
48
|
-
// Re-export docker-sqlite utilities for direct use
|
|
49
|
-
// =============================================================================
|
|
50
|
-
export { withConnection, createConnection, query, execute, execMultiple, lastInsertRowId, tableExists, getMigrationStatus, runMigrations, rollbackMigration, createMigration, } from "@mark1russell7/docker-sqlite";
|
|
51
47
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAoCH,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAEzD,yDAAyD;AACzD,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,aAAa,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAElG,gFAAgF;AAChF,eAAe;AACf,gFAAgF;AAEhF,OAAO,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAEvD,gFAAgF;AAChF,2FAA2F;AAC3F,gFAAgF;AAEhF,OAAO,EACL,aAAa,EACb,cAAc,EACd,qBAAqB,EACrB,WAAW,GAIZ,MAAM,cAAc,CAAC
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAoCH,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAEzD,yDAAyD;AACzD,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,aAAa,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,0BAA0B,CAAC;AAElG,gFAAgF;AAChF,eAAe;AACf,gFAAgF;AAEhF,OAAO,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAEvD,gFAAgF;AAChF,2FAA2F;AAC3F,gFAAgF;AAEhF,OAAO,EACL,aAAa,EACb,cAAc,EACd,qBAAqB,EACrB,WAAW,GAIZ,MAAM,cAAc,CAAC"}
|
package/dist/load.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The main entry of `@mark1russell7/sqlite`, loaded at its first use. The procedures and the storage load it, and with
|
|
3
|
+
* it `node:sqlite`, only when they open a database. An import of this package does not load them.
|
|
4
|
+
*/
|
|
5
|
+
/** The main entry of `@mark1russell7/sqlite`. */
|
|
6
|
+
export type Sqlite = typeof import("@mark1russell7/sqlite");
|
|
7
|
+
/** This function loads the main entry of `@mark1russell7/sqlite` one time. */
|
|
8
|
+
export declare function loadSqlite(): Promise<Sqlite>;
|
|
9
|
+
//# sourceMappingURL=load.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load.d.ts","sourceRoot":"","sources":["../src/load.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,iDAAiD;AACjD,MAAM,MAAM,MAAM,GAAG,cAAc,uBAAuB,CAAC,CAAC;AAI5D,8EAA8E;AAC9E,wBAAgB,UAAU,IAAI,OAAO,CAAC,MAAM,CAAC,CAG5C"}
|
package/dist/load.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The main entry of `@mark1russell7/sqlite`, loaded at its first use. The procedures and the storage load it, and with
|
|
3
|
+
* it `node:sqlite`, only when they open a database. An import of this package does not load them.
|
|
4
|
+
*/
|
|
5
|
+
let loading;
|
|
6
|
+
/** This function loads the main entry of `@mark1russell7/sqlite` one time. */
|
|
7
|
+
export function loadSqlite() {
|
|
8
|
+
loading ??= import("@mark1russell7/sqlite");
|
|
9
|
+
return loading;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=load.js.map
|
package/dist/load.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"load.js","sourceRoot":"","sources":["../src/load.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,IAAI,OAAoC,CAAC;AAEzC,8EAA8E;AAC9E,MAAM,UAAU,UAAU;IACxB,OAAO,KAAK,MAAM,CAAC,uBAAuB,CAAC,CAAC;IAC5C,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
package/dist/module.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAuE,KAAK,eAAe,EAAE,KAAK,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAuE,KAAK,eAAe,EAAE,KAAK,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAijBvJ;;;GAGG;AACH,eAAO,MAAM,YAAY,EAAE,eAAsC,CAAC;eAEnD,YAAY;AAE3B;;;;GAIG;AACH,eAAO,MAAM,wBAAwB,EAAE,iBAAiB,CAAC,UAAU,CAA0B,CAAC"}
|
package/dist/module.js
CHANGED
|
@@ -10,17 +10,18 @@
|
|
|
10
10
|
*/
|
|
11
11
|
import { definePackage, createProcedure } from "@procedures/client";
|
|
12
12
|
import * as z from "zod";
|
|
13
|
-
import {
|
|
14
|
-
import { DEFAULT_DB_PATH, LOG_LEVELS, } from "./types.js";
|
|
13
|
+
import { LOG_LEVELS, } from "./types.js";
|
|
15
14
|
import { dbTables, dbSchema, dbTransaction, dbMigrate, dbBackup } from "./procedures/db/index.js";
|
|
15
|
+
import { countStatements, databasePath, rowsOf, runStatement, runStatements, toSqlParams, totalChanges, withDatabase, } from "./procedures/db/shared.js";
|
|
16
16
|
import { DbTablesInputSchema, DbTablesOutputSchema, DbSchemaInputSchema, DbSchemaOutputSchema, DbTransactionInputSchema, DbTransactionOutputSchema, DbMigrateInputSchema, DbMigrateOutputSchema, DbBackupInputSchema, DbBackupOutputSchema, } from "./types.js";
|
|
17
17
|
// =============================================================================
|
|
18
18
|
// The rules under a root of the files (roadmap R33)
|
|
19
19
|
// =============================================================================
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
// So the paths of the input are all the paths that a call reaches.
|
|
23
|
-
// log database in the home folder) is outside a root, so under a root
|
|
20
|
+
// Each procedure opens the file through the engine of @mark1russell7/sqlite. The procedures that run
|
|
21
|
+
// the SQL of a caller (sqlite.query, sqlite.execute, sqlite.transaction and sqlite.migrate) keep that
|
|
22
|
+
// SQL on its file with confineToFile. So the paths of the input are all the paths that a call reaches.
|
|
23
|
+
// The default database (the CLI log database in the home folder) is outside a root, so under a root
|
|
24
|
+
// a call must give dbPath.
|
|
24
25
|
/** A procedure of one database file. `:memory:` is a database in memory, not a path. */
|
|
25
26
|
const DATABASE_IN_A_ROOT = { paths: ["dbPath"], required: ["dbPath"], values: [":memory:"] };
|
|
26
27
|
/** `sqlite.migrate`: the database file and the folder of the migrations. */
|
|
@@ -36,7 +37,7 @@ const BACKUP_IN_A_ROOT = { paths: ["dbPath", "to"], required: ["dbPath"] };
|
|
|
36
37
|
const dbPathField = z
|
|
37
38
|
.string()
|
|
38
39
|
.optional()
|
|
39
|
-
.describe("The SQLite database file (default: the CLI log database, ~/logs/cli/cli.db). A query does not make a missing file.");
|
|
40
|
+
.describe("The SQLite database file (default: the CLI log database, ~/logs/cli/cli.db). A query does not make a missing file, and a write makes it.");
|
|
40
41
|
const paramsField = z
|
|
41
42
|
.array(z.unknown())
|
|
42
43
|
.optional()
|
|
@@ -101,11 +102,8 @@ const logsQueryOutputSchema = z.looseObject({
|
|
|
101
102
|
// =============================================================================
|
|
102
103
|
// Log Table Migrations
|
|
103
104
|
// =============================================================================
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
version: 1,
|
|
107
|
-
description: "Create logs table",
|
|
108
|
-
up: `
|
|
105
|
+
/** The SQL of the logs table: the version 1 of the log migrations. The file format is the format of earlier versions. */
|
|
106
|
+
const LOG_TABLE_SQL = `
|
|
109
107
|
CREATE TABLE IF NOT EXISTS logs (
|
|
110
108
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
111
109
|
timestamp TEXT NOT NULL DEFAULT (datetime('now')),
|
|
@@ -122,17 +120,79 @@ const LOG_MIGRATIONS = [
|
|
|
122
120
|
CREATE INDEX IF NOT EXISTS idx_logs_level ON logs(level);
|
|
123
121
|
CREATE INDEX IF NOT EXISTS idx_logs_timestamp ON logs(timestamp);
|
|
124
122
|
CREATE INDEX IF NOT EXISTS idx_logs_command ON logs(command);
|
|
125
|
-
|
|
126
|
-
down: "DROP TABLE IF EXISTS logs;",
|
|
127
|
-
},
|
|
128
|
-
];
|
|
123
|
+
`;
|
|
129
124
|
/**
|
|
130
|
-
*
|
|
125
|
+
* This function makes the logs table when the file does not have it, in the transaction of the caller. The table
|
|
126
|
+
* `_migrations` records the version 1, as in earlier versions of this package, so a file of an earlier version
|
|
127
|
+
* opens with its logs.
|
|
131
128
|
*/
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
129
|
+
function ensureLogsTable(db) {
|
|
130
|
+
db.exec("CREATE TABLE IF NOT EXISTS _migrations (version INTEGER PRIMARY KEY, description TEXT NOT NULL, applied_at TEXT NOT NULL DEFAULT (datetime('now')))");
|
|
131
|
+
if (rowsOf(db, "SELECT 1 FROM _migrations WHERE version = 1").length > 0)
|
|
132
|
+
return;
|
|
133
|
+
db.exec(LOG_TABLE_SQL);
|
|
134
|
+
db.prepare("INSERT INTO _migrations (version, description) VALUES (?, ?)").run(1, "Create logs table");
|
|
135
|
+
}
|
|
136
|
+
/** True when the database has a table of the name. */
|
|
137
|
+
function tableExists(db, name) {
|
|
138
|
+
return rowsOf(db, "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?", [name]).length > 0;
|
|
139
|
+
}
|
|
140
|
+
/** `sqlite.query`: a missing file reads as an empty database, the connection only reads, and the guard is on. */
|
|
141
|
+
const QUERY_MODE = { procedure: "sqlite.query", missing: "empty", queryOnly: true, callerSql: true };
|
|
142
|
+
/** `sqlite.execute`: a missing file is made, and the guard is on. */
|
|
143
|
+
const EXECUTE_MODE = { procedure: "sqlite.execute", missing: "make", queryOnly: false, callerSql: true };
|
|
144
|
+
/** `logs.store`: a missing file is made. The procedure runs only its own SQL. */
|
|
145
|
+
const LOGS_STORE_MODE = { procedure: "logs.store", missing: "make", queryOnly: false, callerSql: false };
|
|
146
|
+
/** `logs.query`: the connection only reads. The procedure runs only its own SQL. */
|
|
147
|
+
const LOGS_QUERY_MODE = { procedure: "logs.query", missing: "empty", queryOnly: true, callerSql: false };
|
|
148
|
+
/**
|
|
149
|
+
* This function runs the SQL text of `sqlite.execute`, and gives the number of rows that it changed.
|
|
150
|
+
*
|
|
151
|
+
* - One statement runs alone with its params. Thus a `VACUUM` or a pragma works.
|
|
152
|
+
* - A script of several statements takes no params. It runs in one transaction: all its statements take effect, or none
|
|
153
|
+
* does.
|
|
154
|
+
* - A script that starts with `BEGIN` (or another statement that controls a transaction) runs as written. It controls
|
|
155
|
+
* its own transactions. After an error, the procedure rolls back the open transaction.
|
|
156
|
+
* - A text that leaves a transaction open fails, also one `BEGIN` alone. The procedure rolls the transaction back, so
|
|
157
|
+
* the file keeps none of its changes. Without this check, the close of the connection rolls it back with no error.
|
|
158
|
+
*/
|
|
159
|
+
function executeSql(sqlite, db, sql, params) {
|
|
160
|
+
const label = "sqlite.execute";
|
|
161
|
+
const changes = runExecuteSql(sqlite, db, sql, params, label);
|
|
162
|
+
if (db.isTransaction) {
|
|
163
|
+
db.exec("ROLLBACK");
|
|
164
|
+
throw new Error(`${label}: the SQL left a transaction open, so the procedure rolled it back. End each transaction with COMMIT or ROLLBACK.`);
|
|
165
|
+
}
|
|
166
|
+
return changes;
|
|
167
|
+
}
|
|
168
|
+
/** This function runs the SQL text of `sqlite.execute` in the ways that `executeSql` tells. */
|
|
169
|
+
function runExecuteSql(sqlite, db, sql, params, label) {
|
|
170
|
+
const { count, firstControls } = countStatements(sqlite, db, sql);
|
|
171
|
+
if (count === 0)
|
|
172
|
+
throw new Error(`${label}: the SQL has no statement`);
|
|
173
|
+
if (count === 1) {
|
|
174
|
+
const before = totalChanges(db);
|
|
175
|
+
runStatement(db.prepare(sql), params);
|
|
176
|
+
return totalChanges(db) - before;
|
|
177
|
+
}
|
|
178
|
+
if (params.length > 0) {
|
|
179
|
+
throw new Error(`${label}: the params bind to one statement, and the SQL has several. Send one statement for each call.`);
|
|
180
|
+
}
|
|
181
|
+
if (!firstControls)
|
|
182
|
+
return sqlite.transaction(db, () => runStatements(sqlite, db, sql, [], label).changes);
|
|
183
|
+
try {
|
|
184
|
+
return runStatements(sqlite, db, sql, [], label, "allow").changes;
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
try {
|
|
188
|
+
if (db.isTransaction)
|
|
189
|
+
db.exec("ROLLBACK");
|
|
190
|
+
}
|
|
191
|
+
catch {
|
|
192
|
+
// SQLite ended the transaction already
|
|
193
|
+
}
|
|
194
|
+
throw error;
|
|
195
|
+
}
|
|
136
196
|
}
|
|
137
197
|
// =============================================================================
|
|
138
198
|
// sqlite.query Procedure
|
|
@@ -149,15 +209,12 @@ const sqliteQueryProcedure = createProcedure()
|
|
|
149
209
|
fsRoot: DATABASE_IN_A_ROOT,
|
|
150
210
|
})
|
|
151
211
|
.handler(async (input) => {
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
const
|
|
157
|
-
return {
|
|
158
|
-
columns: result.columns,
|
|
159
|
-
rows: result.rows,
|
|
160
|
-
};
|
|
212
|
+
const params = toSqlParams(input.params, "sqlite.query");
|
|
213
|
+
return withDatabase(databasePath(input.dbPath), QUERY_MODE, (db) => {
|
|
214
|
+
// Only the first statement of the text runs
|
|
215
|
+
const statement = db.prepare(input.sql);
|
|
216
|
+
const columns = statement.columns().map((column) => column.name);
|
|
217
|
+
return { columns, rows: statement.all(...params) };
|
|
161
218
|
});
|
|
162
219
|
})
|
|
163
220
|
.build();
|
|
@@ -174,12 +231,8 @@ const sqliteExecuteProcedure = createProcedure()
|
|
|
174
231
|
fsRoot: DATABASE_IN_A_ROOT,
|
|
175
232
|
})
|
|
176
233
|
.handler(async (input) => {
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
return withConnection({ dbPath }, (db) => {
|
|
180
|
-
const result = execute(db, input.sql, params);
|
|
181
|
-
return { changes: result.changes };
|
|
182
|
-
});
|
|
234
|
+
const params = toSqlParams(input.params, "sqlite.execute");
|
|
235
|
+
return withDatabase(databasePath(input.dbPath), EXECUTE_MODE, (db, sqlite) => ({ changes: executeSql(sqlite, db, input.sql, params) }));
|
|
183
236
|
})
|
|
184
237
|
.build();
|
|
185
238
|
// =============================================================================
|
|
@@ -196,28 +249,19 @@ const logsStoreProcedure = createProcedure()
|
|
|
196
249
|
fsRoot: DATABASE_IN_A_ROOT,
|
|
197
250
|
})
|
|
198
251
|
.handler(async (input) => {
|
|
199
|
-
const dbPath = input.dbPath
|
|
252
|
+
const dbPath = databasePath(input.dbPath);
|
|
200
253
|
// Validate log level
|
|
201
254
|
if (!LOG_LEVELS.includes(input.level)) {
|
|
202
255
|
throw new Error(`Invalid log level: ${input.level}`);
|
|
203
256
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
dataJson,
|
|
213
|
-
input.sessionId ?? null,
|
|
214
|
-
input.command ?? null,
|
|
215
|
-
input.context ?? null,
|
|
216
|
-
input.errorStack ?? null,
|
|
217
|
-
]);
|
|
218
|
-
const id = lastInsertRowId(db);
|
|
219
|
-
return { id };
|
|
220
|
-
});
|
|
257
|
+
return withDatabase(dbPath, LOGS_STORE_MODE, (db, sqlite) => sqlite.transaction(db, () => {
|
|
258
|
+
ensureLogsTable(db);
|
|
259
|
+
const result = db
|
|
260
|
+
.prepare(`INSERT INTO logs (level, message, data, session_id, command, context, error_stack)
|
|
261
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`)
|
|
262
|
+
.run(input.level, input.message, input.data ? JSON.stringify(input.data) : null, input.sessionId ?? null, input.command ?? null, input.context ?? null, input.errorStack ?? null);
|
|
263
|
+
return { id: Number(result.lastInsertRowid) };
|
|
264
|
+
}));
|
|
221
265
|
})
|
|
222
266
|
.build();
|
|
223
267
|
// =============================================================================
|
|
@@ -247,12 +291,10 @@ const logsQueryProcedure = createProcedure()
|
|
|
247
291
|
fsRoot: DATABASE_IN_A_ROOT,
|
|
248
292
|
})
|
|
249
293
|
.handler(async (input) => {
|
|
250
|
-
|
|
251
|
-
return withConnection({ dbPath }, (db) => {
|
|
294
|
+
return withDatabase(databasePath(input.dbPath), LOGS_QUERY_MODE, (db) => {
|
|
252
295
|
if (!tableExists(db, "logs")) {
|
|
253
296
|
return { logs: [] };
|
|
254
297
|
}
|
|
255
|
-
db.run("PRAGMA query_only = 1");
|
|
256
298
|
const conditions = [];
|
|
257
299
|
const params = [];
|
|
258
300
|
if (input.sessionId !== undefined) {
|
|
@@ -292,9 +334,9 @@ const logsQueryProcedure = createProcedure()
|
|
|
292
334
|
${limitClause}
|
|
293
335
|
${offsetClause}
|
|
294
336
|
`;
|
|
295
|
-
const
|
|
296
|
-
const logs =
|
|
297
|
-
id: row.id,
|
|
337
|
+
const rows = rowsOf(db, sql, params);
|
|
338
|
+
const logs = rows.map((row) => ({
|
|
339
|
+
id: Number(row.id),
|
|
298
340
|
timestamp: row.timestamp,
|
|
299
341
|
level: row.level,
|
|
300
342
|
message: row.message,
|
|
@@ -306,8 +348,8 @@ const logsQueryProcedure = createProcedure()
|
|
|
306
348
|
}));
|
|
307
349
|
// Total number of rows matching the filters, ignoring LIMIT/OFFSET, so
|
|
308
350
|
// callers can paginate. `params` holds only the WHERE-clause bindings.
|
|
309
|
-
const
|
|
310
|
-
const total =
|
|
351
|
+
const counted = rowsOf(db, `SELECT COUNT(*) AS total FROM logs ${whereClause}`, params);
|
|
352
|
+
const total = Number(counted[0]?.total ?? 0);
|
|
311
353
|
return { logs, total };
|
|
312
354
|
});
|
|
313
355
|
})
|