@aria-framework/db-worker 0.8.1 → 0.9.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/package.json +42 -38
- package/settingsStore.js +66 -26
package/package.json
CHANGED
|
@@ -1,38 +1,42 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@aria-framework/db-worker",
|
|
3
|
-
"version": "0.
|
|
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": {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
},
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@aria-framework/db-worker",
|
|
3
|
+
"version": "0.9.0",
|
|
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": {
|
|
29
|
+
"optional": true
|
|
30
|
+
},
|
|
31
|
+
"pg": {
|
|
32
|
+
"optional": true
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"better-sqlite3": "^12.10.0",
|
|
37
|
+
"pg": "^8.23.0"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"test": "node test/driverContract.js && node test/migrations.js && node test/migrations-notx.js && node test/regressions.js && node test/smoke.js && node test/tier2.js"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/settingsStore.js
CHANGED
|
@@ -9,52 +9,78 @@
|
|
|
9
9
|
*
|
|
10
10
|
* The typed accessors (session-timeout parsing, brand-name defaults, currency tables) are app
|
|
11
11
|
* CONTENT and stay in the consuming model, which keeps this store as its base.
|
|
12
|
+
*
|
|
13
|
+
* EVERY METHOD IS ASYNC as of 0.9.0, and takes the DRIVER. There is no synchronous Postgres
|
|
14
|
+
* driver, so a store that runs on both engines has no choice. `{ getDb }` is still accepted and
|
|
15
|
+
* adopted — a consumer can upgrade this package before it rewires — but the handle it returns is
|
|
16
|
+
* wrapped rather than used, because the app owns the connection and opening a second one to the
|
|
17
|
+
* same SQLite file is how a writer ends up fighting its own lock.
|
|
18
|
+
*
|
|
19
|
+
* This store needs no lazy require for that adoption, unlike its siblings in auth/notify/backup/
|
|
20
|
+
* api-auth: it lives INSIDE db-worker, so createSqliteDriver is a local module.
|
|
12
21
|
*/
|
|
13
22
|
|
|
14
23
|
'use strict';
|
|
15
24
|
|
|
25
|
+
const { createSqliteDriver } = require('./driver/sqlite');
|
|
26
|
+
|
|
16
27
|
/**
|
|
17
|
-
* @param {
|
|
28
|
+
* @param {object} opts
|
|
29
|
+
* driver the db-worker driver (preferred since 0.9.0) — engine-agnostic
|
|
30
|
+
* getDb LEGACY: a function returning a better-sqlite3 handle, adopted into a driver here
|
|
31
|
+
* table defaults to 'settings'
|
|
18
32
|
* touchUpdatedAt: set true when the table carries an updated_at column to stamp on write.
|
|
19
33
|
*/
|
|
20
34
|
function createSettingsStore(opts = {}) {
|
|
21
|
-
|
|
22
|
-
if (
|
|
35
|
+
let driver = opts.driver || null;
|
|
36
|
+
if (!driver) {
|
|
37
|
+
if (typeof opts.getDb !== 'function') {
|
|
38
|
+
throw new Error('createSettingsStore: pass { driver } (preferred) or { getDb } (legacy)');
|
|
39
|
+
}
|
|
40
|
+
// LAZY, and it has to be. getDb() throws until the worker has opened the database, while
|
|
41
|
+
// stores are constructed at module load — so the handle is fetched on first use, not here.
|
|
42
|
+
let adopted = null;
|
|
43
|
+
driver = new Proxy({}, {
|
|
44
|
+
get(_t, prop) {
|
|
45
|
+
if (!adopted) adopted = createSqliteDriver({ adopt: opts.getDb() });
|
|
46
|
+
return adopted[prop];
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
23
51
|
const table = opts.table || 'settings';
|
|
24
52
|
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(table)) throw new Error(`createSettingsStore: suspicious table name "${table}"`);
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
const upsert = `INSERT INTO ${table} ${insertCols} VALUES ${insertVals}
|
|
31
|
-
ON CONFLICT(key) DO UPDATE SET value = excluded.value${touch}`;
|
|
53
|
+
|
|
54
|
+
// The timestamp comes from the DIALECT rather than being spelled out. SQLite has no date type
|
|
55
|
+
// and stores 'YYYY-MM-DD HH:MM:SS' UTC text; Postgres has a real timestamptz. A hand-written
|
|
56
|
+
// datetime() would be a silent wrong value on the second engine rather than a syntax error on it.
|
|
57
|
+
const now = () => driver.dialect.now();
|
|
32
58
|
|
|
33
59
|
return {
|
|
34
60
|
/** Raw value (string) or null if unset. */
|
|
35
|
-
get(key) {
|
|
36
|
-
const row =
|
|
61
|
+
async get(key) {
|
|
62
|
+
const row = await driver.get(`SELECT value FROM ${table} WHERE key = ?`, [key]);
|
|
37
63
|
return row ? row.value : null;
|
|
38
64
|
},
|
|
39
65
|
|
|
40
66
|
/** Upsert one setting (non-destructive; see the header). */
|
|
41
|
-
set(key, value) {
|
|
42
|
-
return
|
|
67
|
+
async set(key, value) {
|
|
68
|
+
return driver.run(upsertSql(), [key, value == null ? null : String(value)]);
|
|
43
69
|
},
|
|
44
70
|
|
|
45
71
|
/**
|
|
46
72
|
* Upsert several settings in ONE transaction. `undefined` values are skipped (an absent form
|
|
47
73
|
* field is not a request to clear); null is stored as null.
|
|
48
74
|
*/
|
|
49
|
-
setMultiple(keyValues) {
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
75
|
+
async setMultiple(keyValues) {
|
|
76
|
+
const sql = upsertSql();
|
|
77
|
+
await driver.transaction(async (tx) => {
|
|
78
|
+
// for...of, not forEach: forEach discards the promise, so the transaction would commit
|
|
79
|
+
// before a single row was written.
|
|
53
80
|
for (const [key, value] of Object.entries(keyValues || {})) {
|
|
54
|
-
if (value !== undefined)
|
|
81
|
+
if (value !== undefined) await tx.run(sql, [key, value == null ? null : String(value)]);
|
|
55
82
|
}
|
|
56
83
|
});
|
|
57
|
-
run();
|
|
58
84
|
},
|
|
59
85
|
|
|
60
86
|
/**
|
|
@@ -62,21 +88,35 @@ function createSettingsStore(opts = {}) {
|
|
|
62
88
|
* `_` is a LIKE wildcard, so an unescaped 'company_' would also match 'companyXname' — a
|
|
63
89
|
* latent bug in the hand-rolled version this replaces.
|
|
64
90
|
*/
|
|
65
|
-
getByPrefix(prefix) {
|
|
91
|
+
async getByPrefix(prefix) {
|
|
66
92
|
const esc = String(prefix).replace(/[\\%_]/g, (c) => '\\' + c);
|
|
67
|
-
const rows =
|
|
68
|
-
|
|
69
|
-
|
|
93
|
+
const rows = await driver.all(
|
|
94
|
+
`SELECT key, value FROM ${table} WHERE key LIKE ? ESCAPE '\\'`, [esc + '%']
|
|
95
|
+
);
|
|
70
96
|
const out = {};
|
|
71
97
|
for (const r of rows) out[r.key] = r.value;
|
|
72
98
|
return out;
|
|
73
99
|
},
|
|
74
100
|
|
|
75
101
|
/** All settings as an array of { key, value }, ordered by key. */
|
|
76
|
-
all() {
|
|
77
|
-
return
|
|
102
|
+
async all() {
|
|
103
|
+
return driver.all(`SELECT key, value FROM ${table} ORDER BY key`);
|
|
78
104
|
}
|
|
79
105
|
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Built per call rather than once at construction, because it reads the dialect — and with a
|
|
109
|
+
* LEGACY { getDb } the driver is a lazy proxy whose dialect is not resolvable until the first
|
|
110
|
+
* real use. Building it at construction would call getDb() before the worker opened the
|
|
111
|
+
* database, which is the failure the proxy exists to avoid.
|
|
112
|
+
*/
|
|
113
|
+
function upsertSql() {
|
|
114
|
+
const touch = opts.touchUpdatedAt ? `, updated_at = ${now()}` : '';
|
|
115
|
+
const insertCols = opts.touchUpdatedAt ? '(key, value, updated_at)' : '(key, value)';
|
|
116
|
+
const insertVals = opts.touchUpdatedAt ? `(?, ?, ${now()})` : '(?, ?)';
|
|
117
|
+
return `INSERT INTO ${table} ${insertCols} VALUES ${insertVals}
|
|
118
|
+
ON CONFLICT(key) DO UPDATE SET value = excluded.value${touch}`;
|
|
119
|
+
}
|
|
80
120
|
}
|
|
81
121
|
|
|
82
122
|
module.exports = { createSettingsStore };
|