@aria-framework/db-worker 0.6.0 → 0.6.1
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/database.js +3 -29
- package/driver/params.js +75 -0
- package/driver/postgres.js +23 -5
- package/driver/sqlite.js +32 -7
- package/package.json +38 -38
package/database.js
CHANGED
|
@@ -140,36 +140,10 @@ function runMigrations(migrationsDir) {
|
|
|
140
140
|
})();
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
-
/**
|
|
144
|
-
* Wrap an ALREADY-OPEN better-sqlite3 handle in the driver contract.
|
|
145
|
-
*
|
|
146
|
-
* The legacy init() path opens and configures the handle itself, so the driver must adopt it
|
|
147
|
-
* rather than open a second one — two connections to the same file is how you get a writer
|
|
148
|
-
* fighting its own lock.
|
|
149
|
-
*/
|
|
143
|
+
/** Adopt the handle init() just opened — the driver must not open a second connection. */
|
|
150
144
|
function _adoptSqliteDriver(db) {
|
|
151
|
-
const {
|
|
152
|
-
|
|
153
|
-
const api = {
|
|
154
|
-
kind: 'sqlite',
|
|
155
|
-
dialect: dialectFor('sqlite'),
|
|
156
|
-
concurrency: 'serial',
|
|
157
|
-
raw: db,
|
|
158
|
-
async get(sql, params = []) { return stmt(sql).get(...params) || null; },
|
|
159
|
-
async all(sql, params = []) { return stmt(sql).all(...params); },
|
|
160
|
-
async run(sql, params = []) {
|
|
161
|
-
const info = stmt(sql).run(...params);
|
|
162
|
-
return { changes: info.changes, lastId: info.lastInsertRowid };
|
|
163
|
-
},
|
|
164
|
-
async exec(sql) { db.exec(sql); },
|
|
165
|
-
async transaction(fn) {
|
|
166
|
-
db.exec('BEGIN');
|
|
167
|
-
try { const out = await fn(api); db.exec('COMMIT'); return out; }
|
|
168
|
-
catch (e) { try { db.exec('ROLLBACK'); } catch (_) {} throw e; }
|
|
169
|
-
},
|
|
170
|
-
async close() { db.close(); }
|
|
171
|
-
};
|
|
172
|
-
return api;
|
|
145
|
+
const { createSqliteDriver } = require('./driver/sqlite');
|
|
146
|
+
return createSqliteDriver({ adopt: db });
|
|
173
147
|
}
|
|
174
148
|
|
|
175
149
|
function getDb() {
|
package/driver/params.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Named parameters, normalised to positional before either driver sees them.
|
|
3
|
+
*
|
|
4
|
+
* better-sqlite3 supports `@name` natively; `pg` supports only `$1..$n`. Rather than make every
|
|
5
|
+
* store choose a style per engine — or worse, rewrite caller-supplied WHERE fragments, which in
|
|
6
|
+
* this framework are built by the APP and passed in — the contract accepts both and converts
|
|
7
|
+
* here, once, for both drivers. A store's SQL is then identical on either engine.
|
|
8
|
+
*
|
|
9
|
+
* THE SCAN IS THE SAME ONE the placeholder rewriter uses, and for the same reason: an `@name`
|
|
10
|
+
* inside a string literal ('user@example.com') must not be treated as a parameter. Getting that
|
|
11
|
+
* wrong binds the wrong value silently rather than erroring, so it is tested directly.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
'use strict';
|
|
15
|
+
|
|
16
|
+
const NAME_CHAR = /[A-Za-z0-9_]/;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Convert `@name` placeholders to `?`, returning values in positional order.
|
|
20
|
+
*
|
|
21
|
+
* @param {string} sql
|
|
22
|
+
* @param {object|Array} params an object binds by name; an array is already positional
|
|
23
|
+
* @returns {{sql: string, values: Array}}
|
|
24
|
+
*/
|
|
25
|
+
function bindNamed(sql, params) {
|
|
26
|
+
if (Array.isArray(params) || params == null) return { sql, values: params || [] };
|
|
27
|
+
|
|
28
|
+
const values = [];
|
|
29
|
+
let out = '';
|
|
30
|
+
let i = 0;
|
|
31
|
+
while (i < sql.length) {
|
|
32
|
+
const c = sql[i];
|
|
33
|
+
|
|
34
|
+
if (c === "'") { // string literal, '' escapes
|
|
35
|
+
let j = i + 1;
|
|
36
|
+
while (j < sql.length) {
|
|
37
|
+
if (sql[j] === "'" && sql[j + 1] === "'") { j += 2; continue; }
|
|
38
|
+
if (sql[j] === "'") break;
|
|
39
|
+
j++;
|
|
40
|
+
}
|
|
41
|
+
out += sql.slice(i, j + 1); i = j + 1; continue;
|
|
42
|
+
}
|
|
43
|
+
if (c === '"') { // quoted identifier
|
|
44
|
+
const j = sql.indexOf('"', i + 1);
|
|
45
|
+
const end = j === -1 ? sql.length - 1 : j;
|
|
46
|
+
out += sql.slice(i, end + 1); i = end + 1; continue;
|
|
47
|
+
}
|
|
48
|
+
if (c === '-' && sql[i + 1] === '-') { // line comment
|
|
49
|
+
const j = sql.indexOf('\n', i);
|
|
50
|
+
const end = j === -1 ? sql.length : j;
|
|
51
|
+
out += sql.slice(i, end); i = end; continue;
|
|
52
|
+
}
|
|
53
|
+
if (c === '/' && sql[i + 1] === '*') { // block comment
|
|
54
|
+
const j = sql.indexOf('*/', i + 2);
|
|
55
|
+
const end = j === -1 ? sql.length : j + 2;
|
|
56
|
+
out += sql.slice(i, end); i = end; continue;
|
|
57
|
+
}
|
|
58
|
+
if (c === '@' && NAME_CHAR.test(sql[i + 1] || '')) {
|
|
59
|
+
let j = i + 1;
|
|
60
|
+
while (j < sql.length && NAME_CHAR.test(sql[j])) j++;
|
|
61
|
+
const name = sql.slice(i + 1, j);
|
|
62
|
+
if (!(name in params)) {
|
|
63
|
+
throw new Error(`bindNamed: SQL references @${name} but no such key was supplied`);
|
|
64
|
+
}
|
|
65
|
+
values.push(params[name]);
|
|
66
|
+
out += '?';
|
|
67
|
+
i = j;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
out += c; i++;
|
|
71
|
+
}
|
|
72
|
+
return { sql: out, values };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
module.exports = { bindNamed };
|
package/driver/postgres.js
CHANGED
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
'use strict';
|
|
20
20
|
|
|
21
21
|
const { dialectFor } = require('../dialect');
|
|
22
|
+
const { bindNamed } = require('./params');
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
25
|
* Rewrite `?` placeholders to `$1..$n`, leaving any `?` inside a literal alone.
|
|
@@ -105,11 +106,13 @@ function createPostgresDriver(opts = {}) {
|
|
|
105
106
|
concurrency: 'pool',
|
|
106
107
|
|
|
107
108
|
async get(sql, params = []) {
|
|
108
|
-
const
|
|
109
|
+
const b = bindNamed(sql, params);
|
|
110
|
+
const r = await exec(toDollarParams(b.sql), b.values);
|
|
109
111
|
return r.rows[0] || null;
|
|
110
112
|
},
|
|
111
113
|
async all(sql, params = []) {
|
|
112
|
-
const
|
|
114
|
+
const b = bindNamed(sql, params);
|
|
115
|
+
const r = await exec(toDollarParams(b.sql), b.values);
|
|
113
116
|
return r.rows;
|
|
114
117
|
},
|
|
115
118
|
/**
|
|
@@ -118,7 +121,8 @@ function createPostgresDriver(opts = {}) {
|
|
|
118
121
|
* ` RETURNING "id"` here — so the store's SQL stays one string for both engines.
|
|
119
122
|
*/
|
|
120
123
|
async run(sql, params = []) {
|
|
121
|
-
const
|
|
124
|
+
const b = bindNamed(sql, params);
|
|
125
|
+
const r = await exec(toDollarParams(b.sql), b.values);
|
|
122
126
|
const row = r.rows && r.rows[0];
|
|
123
127
|
const lastId = row ? (row.id !== undefined ? row.id : Object.values(row)[0]) : undefined;
|
|
124
128
|
return { changes: r.rowCount, lastId };
|
|
@@ -137,9 +141,23 @@ function createPostgresDriver(opts = {}) {
|
|
|
137
141
|
*/
|
|
138
142
|
async transaction(fn) {
|
|
139
143
|
const client = await pool.connect();
|
|
144
|
+
let depth = 0;
|
|
140
145
|
const txApi = apiOver((sql, params) => client.query(sql, params), {
|
|
141
|
-
// Nested transaction() reuses the same
|
|
142
|
-
|
|
146
|
+
// Nested transaction() reuses the same CLIENT (a second pooled connection would
|
|
147
|
+
// deadlock against this one's locks) and brackets the body in a SAVEPOINT, so an inner
|
|
148
|
+
// failure rolls back only its own work rather than discarding the outer transaction.
|
|
149
|
+
async transaction(inner) {
|
|
150
|
+
const sp = `aria_sp_${depth++}`;
|
|
151
|
+
await client.query(`SAVEPOINT ${sp}`);
|
|
152
|
+
try {
|
|
153
|
+
const out = await inner(txApi);
|
|
154
|
+
await client.query(`RELEASE SAVEPOINT ${sp}`);
|
|
155
|
+
return out;
|
|
156
|
+
} catch (e) {
|
|
157
|
+
try { await client.query(`ROLLBACK TO SAVEPOINT ${sp}`); } catch (_) { /* noop */ }
|
|
158
|
+
throw e;
|
|
159
|
+
} finally { depth--; }
|
|
160
|
+
}
|
|
143
161
|
});
|
|
144
162
|
try {
|
|
145
163
|
await client.query('BEGIN');
|
package/driver/sqlite.js
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
const fs = require('fs');
|
|
22
22
|
const path = require('path');
|
|
23
23
|
const { dialectFor } = require('../dialect');
|
|
24
|
+
const { bindNamed } = require('./params');
|
|
24
25
|
|
|
25
26
|
/**
|
|
26
27
|
* @param {object} opts
|
|
@@ -30,7 +31,12 @@ const { dialectFor } = require('../dialect');
|
|
|
30
31
|
* pragmas override/extend the defaults
|
|
31
32
|
*/
|
|
32
33
|
function createSqliteDriver(opts = {}) {
|
|
33
|
-
|
|
34
|
+
// ADOPT an already-open handle rather than opening a second one. Two connections to the same
|
|
35
|
+
// file is how a writer ends up fighting its own lock, so a consumer that already owns a handle
|
|
36
|
+
// (the legacy init() path, or a store handed a getDb) passes it here instead.
|
|
37
|
+
if (opts.adopt) return wrap(opts.adopt);
|
|
38
|
+
|
|
39
|
+
if (!opts.path) throw new Error("createSqliteDriver({ path }) or ({ adopt }): a database file path or an open handle is required");
|
|
34
40
|
let Database;
|
|
35
41
|
try {
|
|
36
42
|
Database = require('better-sqlite3');
|
|
@@ -57,7 +63,13 @@ function createSqliteDriver(opts = {}) {
|
|
|
57
63
|
};
|
|
58
64
|
for (const [k, v] of Object.entries(pragmas)) db.pragma(`${k} = ${v}`);
|
|
59
65
|
|
|
66
|
+
return wrap(db);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The contract over an open better-sqlite3 handle. */
|
|
70
|
+
function wrap(db) {
|
|
60
71
|
const stmt = (sql) => db.prepare(sql);
|
|
72
|
+
let txDepth = 0;
|
|
61
73
|
|
|
62
74
|
const api = {
|
|
63
75
|
kind: 'sqlite',
|
|
@@ -67,13 +79,16 @@ function createSqliteDriver(opts = {}) {
|
|
|
67
79
|
raw: db,
|
|
68
80
|
|
|
69
81
|
async get(sql, params = []) {
|
|
70
|
-
|
|
82
|
+
const b = bindNamed(sql, params);
|
|
83
|
+
return stmt(b.sql).get(...b.values) || null;
|
|
71
84
|
},
|
|
72
85
|
async all(sql, params = []) {
|
|
73
|
-
|
|
86
|
+
const b = bindNamed(sql, params);
|
|
87
|
+
return stmt(b.sql).all(...b.values);
|
|
74
88
|
},
|
|
75
89
|
async run(sql, params = []) {
|
|
76
|
-
const
|
|
90
|
+
const b = bindNamed(sql, params);
|
|
91
|
+
const info = stmt(b.sql).run(...b.values);
|
|
77
92
|
return { changes: info.changes, lastId: info.lastInsertRowid };
|
|
78
93
|
},
|
|
79
94
|
/** Multi-statement DDL. Never parameterised — migrations only. */
|
|
@@ -88,14 +103,24 @@ function createSqliteDriver(opts = {}) {
|
|
|
88
103
|
* can issue a statement between the BEGIN and the COMMIT.
|
|
89
104
|
*/
|
|
90
105
|
async transaction(fn) {
|
|
91
|
-
|
|
106
|
+
// NESTING VIA SAVEPOINTS. Stores compose — an app wrapper can put its own transaction
|
|
107
|
+
// around a store call that already opens one (a role rename that must retarget users in
|
|
108
|
+
// the same atomic step is the case that surfaced this). A second BEGIN is an error in
|
|
109
|
+
// SQLite, so depth is tracked: the outermost is a real transaction, inner ones are
|
|
110
|
+
// savepoints that can roll back independently without discarding the outer work.
|
|
111
|
+
const depth = txDepth++;
|
|
112
|
+
const sp = `aria_sp_${depth}`;
|
|
113
|
+
db.exec(depth === 0 ? 'BEGIN' : `SAVEPOINT ${sp}`);
|
|
92
114
|
try {
|
|
93
115
|
const out = await fn(api);
|
|
94
|
-
db.exec('COMMIT');
|
|
116
|
+
db.exec(depth === 0 ? 'COMMIT' : `RELEASE ${sp}`);
|
|
95
117
|
return out;
|
|
96
118
|
} catch (e) {
|
|
97
|
-
|
|
119
|
+
// The original error is what matters; a failed rollback must not mask it.
|
|
120
|
+
try { db.exec(depth === 0 ? 'ROLLBACK' : `ROLLBACK TO ${sp}`); } catch (_) { /* noop */ }
|
|
98
121
|
throw e;
|
|
122
|
+
} finally {
|
|
123
|
+
txDepth = depth;
|
|
99
124
|
}
|
|
100
125
|
},
|
|
101
126
|
|
package/package.json
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@aria-framework/db-worker",
|
|
3
|
-
"version": "0.6.
|
|
4
|
-
"description": "Aria App Framework — db-worker module. Async RPC bridge over synchronous better-sqlite3: a worker-thread dispatcher (runWorker), a main-thread client with per-call timeouts and crash handling (DbClient), and a SQLite layer with WAL pragmas + a numbered-migration runner (database).",
|
|
5
|
-
"license": "UNLICENSED",
|
|
6
|
-
"private": false,
|
|
7
|
-
"publishConfig": {
|
|
8
|
-
"access": "public"
|
|
9
|
-
},
|
|
10
|
-
"main": "index.js",
|
|
11
|
-
"files": [
|
|
12
|
-
"index.js",
|
|
13
|
-
"database.js",
|
|
14
|
-
"worker.js",
|
|
15
|
-
"client.js",
|
|
16
|
-
"tableOrder.js",
|
|
17
|
-
"schemaSnapshot.js",
|
|
18
|
-
"workerRegistry.js",
|
|
19
|
-
"settingsStore.js",
|
|
20
|
-
"dialect.js",
|
|
21
|
-
"driver/"
|
|
22
|
-
],
|
|
23
|
-
"peerDependencies": {
|
|
24
|
-
"better-sqlite3": ">=9",
|
|
25
|
-
"pg": ">=8"
|
|
26
|
-
},
|
|
27
|
-
"peerDependenciesMeta": {
|
|
28
|
-
"better-sqlite3": { "optional": true },
|
|
29
|
-
"pg": { "optional": true }
|
|
30
|
-
},
|
|
31
|
-
"devDependencies": {
|
|
32
|
-
"better-sqlite3": "^12.10.0",
|
|
33
|
-
"pg": "^8.23.0"
|
|
34
|
-
},
|
|
35
|
-
"scripts": {
|
|
36
|
-
"test": "node test/driverContract.js && node test/migrations.js && node test/smoke.js && node test/tier2.js"
|
|
37
|
-
}
|
|
38
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@aria-framework/db-worker",
|
|
3
|
+
"version": "0.6.1",
|
|
4
|
+
"description": "Aria App Framework — db-worker module. Async RPC bridge over synchronous better-sqlite3: a worker-thread dispatcher (runWorker), a main-thread client with per-call timeouts and crash handling (DbClient), and a SQLite layer with WAL pragmas + a numbered-migration runner (database).",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"private": false,
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"main": "index.js",
|
|
11
|
+
"files": [
|
|
12
|
+
"index.js",
|
|
13
|
+
"database.js",
|
|
14
|
+
"worker.js",
|
|
15
|
+
"client.js",
|
|
16
|
+
"tableOrder.js",
|
|
17
|
+
"schemaSnapshot.js",
|
|
18
|
+
"workerRegistry.js",
|
|
19
|
+
"settingsStore.js",
|
|
20
|
+
"dialect.js",
|
|
21
|
+
"driver/"
|
|
22
|
+
],
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"better-sqlite3": ">=9",
|
|
25
|
+
"pg": ">=8"
|
|
26
|
+
},
|
|
27
|
+
"peerDependenciesMeta": {
|
|
28
|
+
"better-sqlite3": { "optional": true },
|
|
29
|
+
"pg": { "optional": true }
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"better-sqlite3": "^12.10.0",
|
|
33
|
+
"pg": "^8.23.0"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"test": "node test/driverContract.js && node test/migrations.js && node test/smoke.js && node test/tier2.js"
|
|
37
|
+
}
|
|
38
|
+
}
|