@aria-framework/db-worker 0.16.0 → 0.18.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/dialect.js +40 -1
- package/package.json +1 -1
- package/settingsStore.js +20 -1
package/dialect.js
CHANGED
|
@@ -36,6 +36,11 @@ const sqlite = {
|
|
|
36
36
|
now: () => "datetime('now')",
|
|
37
37
|
/** N whole days before now — used by retention cuts. */
|
|
38
38
|
nowMinusDays: (n) => `datetime('now', '-${Number(n)} days')`,
|
|
39
|
+
/**
|
|
40
|
+
* Seconds, because some windows are short and expressing them as a fraction of a minute is a
|
|
41
|
+
* rounding error waiting to be argued about. api-auth's key-touch throttle is the caller.
|
|
42
|
+
*/
|
|
43
|
+
nowMinusSeconds: (n) => `datetime('now', '-${Number(n)} seconds')`,
|
|
39
44
|
/**
|
|
40
45
|
* WHICH COLUMNS A TABLE HAS, as SQL returning rows with a `name` column.
|
|
41
46
|
*
|
|
@@ -81,7 +86,21 @@ const sqlite = {
|
|
|
81
86
|
*
|
|
82
87
|
* Raw and unrounded on purpose: every caller wraps it in AVG or SUM and multiplies to the
|
|
83
88
|
* unit it wants (x24 for hours, x1440 for minutes). A helper that rounded would not compose.
|
|
84
|
-
*
|
|
89
|
+
*
|
|
90
|
+
* NEVER PASS THE LITERAL 'now'. This used to say "either side may be the literal 'now', which
|
|
91
|
+
* both engines understand" — true, and wrong, which is the worst kind of doc. Both engines
|
|
92
|
+
* PARSE it and they mean different instants:
|
|
93
|
+
*
|
|
94
|
+
* sqlite julianday('now') UTC, always
|
|
95
|
+
* postgres 'now'::timestamp the SESSION's time zone
|
|
96
|
+
*
|
|
97
|
+
* Every timestamp this framework writes is UTC text (see now() above), so on a server whose
|
|
98
|
+
* session time zone is not UTC the subtraction is off by exactly the offset. Support101 met it
|
|
99
|
+
* on an America/Chicago server: stopping a timer computed elapsed minutes 300 SHORT, negative
|
|
100
|
+
* for anything under five hours, and the caller's own MAX(1, …) floor turned that into a
|
|
101
|
+
* plausible "1 minute" instead of an obvious absurdity. Billable data, quietly wrong.
|
|
102
|
+
*
|
|
103
|
+
* Pass now() instead, so both sides are in the same frame. Two stored COLUMNS are always safe.
|
|
85
104
|
*/
|
|
86
105
|
daysBetween: (from, to) => `(julianday(${to}) - julianday(${from}))`,
|
|
87
106
|
/** Read a JSON path out of a column: jsonPath('detail', '$.model'). */
|
|
@@ -92,6 +111,19 @@ const sqlite = {
|
|
|
92
111
|
// `role LIKE 'read_only'` also matches 'read-only' because `_` is a single-character wildcard,
|
|
93
112
|
// and a value of '%' matches everything. Equality has to be spelled as equality.
|
|
94
113
|
ciEquals: (col, param) => `LOWER(${col}) = LOWER(${param})`,
|
|
114
|
+
/**
|
|
115
|
+
* The larger / smaller of two VALUES — the scalar, not the aggregate.
|
|
116
|
+
*
|
|
117
|
+
* SQLite's MAX is two functions wearing one name: an aggregate with one argument, a scalar with
|
|
118
|
+
* two. Postgres has only the aggregate and spells the scalar GREATEST, so `MAX(1, x)` there is
|
|
119
|
+
* not a wrong answer — it is `function max(integer, integer) does not exist`.
|
|
120
|
+
*
|
|
121
|
+
* Named here rather than solved in each app: Support101 hit it in two places, both on paths
|
|
122
|
+
* nothing routine exercises (stopping a timer; requeueing a refused job), and an app that fixes
|
|
123
|
+
* it locally leaves every other consumer to rediscover the same thing.
|
|
124
|
+
*/
|
|
125
|
+
greatest: (a, b) => `MAX(${a}, ${b})`,
|
|
126
|
+
least: (a, b) => `MIN(${a}, ${b})`,
|
|
95
127
|
/** Suffix that makes run() report a new row's id. SQLite reports it out of band. */
|
|
96
128
|
returning: () => '',
|
|
97
129
|
/** DDL fragment for an auto-incrementing primary key (only secure-keystore needs this). */
|
|
@@ -110,6 +142,7 @@ const postgres = {
|
|
|
110
142
|
// for both; an app wanting a real timestamptz column in its OWN tables uses nowTz().
|
|
111
143
|
now: () => "to_char(NOW() AT TIME ZONE 'UTC', 'YYYY-MM-DD HH24:MI:SS')",
|
|
112
144
|
nowMinusDays: (n) => `to_char((NOW() - INTERVAL '${Number(n)} days') AT TIME ZONE 'UTC', 'YYYY-MM-DD HH24:MI:SS')`,
|
|
145
|
+
nowMinusSeconds: (n) => `to_char((NOW() - INTERVAL '${Number(n)} seconds') AT TIME ZONE 'UTC', 'YYYY-MM-DD HH24:MI:SS')`,
|
|
113
146
|
/**
|
|
114
147
|
* WHICH COLUMNS A TABLE HAS, as SQL returning rows with a `name` column.
|
|
115
148
|
*
|
|
@@ -144,6 +177,9 @@ const postgres = {
|
|
|
144
177
|
if (unit === 'week') return `to_char(${col}::timestamp, 'IYYY-"W"IW')`;
|
|
145
178
|
throw new Error(`dateGroup: expected 'month' or 'week', got ${JSON.stringify(unit)}`);
|
|
146
179
|
},
|
|
180
|
+
// NEVER PASS THE LITERAL 'now' — see the sqlite twin. `'now'::timestamp` is resolved in the
|
|
181
|
+
// SESSION's time zone, and everything this framework stores is UTC text, so the subtraction is
|
|
182
|
+
// silently off by the server's offset. Pass now().
|
|
147
183
|
daysBetween: (from, to) =>
|
|
148
184
|
`(EXTRACT(EPOCH FROM (${to}::timestamp - ${from}::timestamp)) / 86400)`,
|
|
149
185
|
/** Native timestamptz, for an app that declares real timestamp columns. */
|
|
@@ -162,6 +198,9 @@ const postgres = {
|
|
|
162
198
|
},
|
|
163
199
|
ilike: () => 'ILIKE',
|
|
164
200
|
ciEquals: (col, param) => `LOWER(${col}) = LOWER(${param})`,
|
|
201
|
+
// The scalar, not the aggregate — see the sqlite twin for why this needs naming at all.
|
|
202
|
+
greatest: (a, b) => `GREATEST(${a}, ${b})`,
|
|
203
|
+
least: (a, b) => `LEAST(${a}, ${b})`,
|
|
165
204
|
returning: (col = 'id') => ` RETURNING ${ident(col)}`,
|
|
166
205
|
autoIncrementPk: () => 'BIGSERIAL PRIMARY KEY',
|
|
167
206
|
textType: () => 'TEXT'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aria-framework/db-worker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.0",
|
|
4
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
5
|
"license": "UNLICENSED",
|
|
6
6
|
"private": false,
|
package/settingsStore.js
CHANGED
|
@@ -30,6 +30,20 @@ const { createSqliteDriver } = require('./driver/sqlite');
|
|
|
30
30
|
* getDb LEGACY: a function returning a better-sqlite3 handle, adopted into a driver here
|
|
31
31
|
* table defaults to 'settings'
|
|
32
32
|
* touchUpdatedAt: set true when the table carries an updated_at column to stamp on write.
|
|
33
|
+
* conflictTarget: the column list the upsert conflicts on, defaulting to 'key'.
|
|
34
|
+
*
|
|
35
|
+
* WHY conflictTarget EXISTS. An upsert's conflict target must match a real unique constraint, and
|
|
36
|
+
* a MULTI-TENANT settings table is not unique on `key` — two partners each need their own
|
|
37
|
+
* `brand_name`, so the constraint becomes `(partner_id, key)`. `ON CONFLICT(key)` against that is
|
|
38
|
+
* not a wrong answer; Postgres refuses the statement outright with "there is no unique or
|
|
39
|
+
* exclusion constraint matching the ON CONFLICT specification".
|
|
40
|
+
*
|
|
41
|
+
* Support101 met this after its tenancy migration: reads worked, and EVERY WRITE failed — brand
|
|
42
|
+
* name, timezone, session timeout, AI ceilings, audit retention. It stayed hidden through a full
|
|
43
|
+
* Postgres test pass because nothing in that pass CHANGED a setting; the first person to edit one
|
|
44
|
+
* in the admin UI found it immediately.
|
|
45
|
+
*
|
|
46
|
+
* Defaults to 'key', so every existing consumer is unaffected.
|
|
33
47
|
*/
|
|
34
48
|
function createSettingsStore(opts = {}) {
|
|
35
49
|
let driver = opts.driver || null;
|
|
@@ -114,8 +128,13 @@ function createSettingsStore(opts = {}) {
|
|
|
114
128
|
const touch = opts.touchUpdatedAt ? `, updated_at = ${now()}` : '';
|
|
115
129
|
const insertCols = opts.touchUpdatedAt ? '(key, value, updated_at)' : '(key, value)';
|
|
116
130
|
const insertVals = opts.touchUpdatedAt ? `(?, ?, ${now()})` : '(?, ?)';
|
|
131
|
+
// The INSERT still names only key/value: a tenant column is supplied by its own DEFAULT
|
|
132
|
+
// (`current_setting('app.partner_id')` under RLS), so the row lands on the right tenant
|
|
133
|
+
// without this store knowing that tenants exist. Only the CONFLICT TARGET has to agree with
|
|
134
|
+
// the constraint the app actually built.
|
|
135
|
+
const conflict = opts.conflictTarget || 'key';
|
|
117
136
|
return `INSERT INTO ${table} ${insertCols} VALUES ${insertVals}
|
|
118
|
-
ON CONFLICT(
|
|
137
|
+
ON CONFLICT(${conflict}) DO UPDATE SET value = excluded.value${touch}`;
|
|
119
138
|
}
|
|
120
139
|
}
|
|
121
140
|
|