@aria-framework/db-worker 0.14.0 → 0.15.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/README.md +25 -0
- package/dialect.js +55 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -250,8 +250,33 @@ untouched. Like `aria:no-transaction` it is read from the HEADER only — a comm
|
|
|
250
250
|
or inside a multi-line string literal, is not a declaration about the file — and a near miss
|
|
251
251
|
(`aria: covers-through`, `aria:covers_through`) is refused rather than ignored.
|
|
252
252
|
|
|
253
|
+
### Date helpers on the dialect (0.15.0)
|
|
254
|
+
|
|
255
|
+
An app should say WHAT it wants from a date, not how one engine spells it.
|
|
256
|
+
|
|
257
|
+
| | sqlite | postgres |
|
|
258
|
+
|---|---|---|
|
|
259
|
+
| now() | datetime(now) | to_char(NOW() AT TIME ZONE UTC, …) |
|
|
260
|
+
| nowMinusDays(n) | a retention cut | the same, as an INTERVAL |
|
|
261
|
+
| nowPlusMinutes(n) | an expiry window; the SIGN comes from the number | INTERVAL |
|
|
262
|
+
| startOf(month/year) | start of month | date_trunc on the UTC value |
|
|
263
|
+
| dateGroup(col, month/week) | strftime | to_char |
|
|
264
|
+
| daysBetween(a, b) | julianday difference | EXTRACT(EPOCH …) / 86400 |
|
|
265
|
+
|
|
266
|
+
daysBetween is RAW and unrounded on purpose: callers wrap it in AVG or SUM and multiply to the
|
|
267
|
+
unit they want. A helper that rounded would not compose with either.
|
|
268
|
+
|
|
269
|
+
dateGroup and daysBetween cast to ::timestamp on Postgres — those columns are TEXT on both
|
|
270
|
+
engines by SQLite heritage, and Postgres refuses date arithmetic on text.
|
|
271
|
+
|
|
272
|
+
ONE DIVERGENCE, ACCEPTED RATHER THAN HIDDEN: SQLite %W counts weeks from the first Monday,
|
|
273
|
+
Postgres IW is the ISO week. They can disagree by one at a year boundary. The value is a GROUP BY
|
|
274
|
+
key on a chart axis, and an ISO week is the more defensible thing to show a person.
|
|
275
|
+
|
|
253
276
|
## Changelog
|
|
254
277
|
|
|
278
|
+
- **0.15.0** — nowPlusMinutes, startOf, dateGroup, daysBetween (above).
|
|
279
|
+
|
|
255
280
|
- **0.14.0** — `-- aria:covers-through NNN` (above).
|
|
256
281
|
- **0.13.0** — the Postgres driver had the sibling savepoint defect too. Two nested transactions
|
|
257
282
|
started together on one client destroyed each other, and the failed RELEASE aborted the
|
package/dialect.js
CHANGED
|
@@ -32,8 +32,41 @@ const sqlite = {
|
|
|
32
32
|
now: () => "datetime('now')",
|
|
33
33
|
/** N whole days before now — used by retention cuts. */
|
|
34
34
|
nowMinusDays: (n) => `datetime('now', '-${Number(n)} days')`,
|
|
35
|
+
/**
|
|
36
|
+
* N minutes AFTER now — an expiry window.
|
|
37
|
+
*
|
|
38
|
+
* Rendered rather than bound. A caller passing the modifier as a parameter
|
|
39
|
+
* (`datetime('now', ?)` with '+15 minutes') is putting SQLite's own grammar into its data,
|
|
40
|
+
* which is unreadable to any other engine. The number stays the caller's; only the spelling
|
|
41
|
+
* belongs here.
|
|
42
|
+
*/
|
|
43
|
+
// The sign comes from the NUMBER, not from a literal '+'. datetime('now', '+-15 minutes') is
|
|
44
|
+
// not valid SQLite, and a negative offset is how an expiry test reaches a moment already past.
|
|
45
|
+
nowPlusMinutes: (n) => `datetime('now', '${Number(n) >= 0 ? '+' : ''}${Number(n)} minutes')`,
|
|
46
|
+
/** The first instant of the current month or year. */
|
|
47
|
+
startOf: (unit) => {
|
|
48
|
+
if (unit !== 'month' && unit !== 'year') throw new Error(`startOf: expected 'month' or 'year', got ${JSON.stringify(unit)}`);
|
|
49
|
+
return `datetime('now', 'start of ${unit}')`;
|
|
50
|
+
},
|
|
35
51
|
/** Native timestamp type. SQLite has none, so this is the same text value. */
|
|
36
52
|
nowTz: () => "datetime('now')",
|
|
53
|
+
/**
|
|
54
|
+
* The period a timestamp column falls in: 'month' gives 2026-09, 'week' gives 2026-W36.
|
|
55
|
+
* Used as a GROUP BY key on a report axis, so it only has to be stable and sortable.
|
|
56
|
+
*/
|
|
57
|
+
dateGroup: (col, unit) => {
|
|
58
|
+
if (unit === 'month') return `strftime('%Y-%m', ${col})`;
|
|
59
|
+
if (unit === 'week') return `strftime('%Y-W%W', ${col})`;
|
|
60
|
+
throw new Error(`dateGroup: expected 'month' or 'week', got ${JSON.stringify(unit)}`);
|
|
61
|
+
},
|
|
62
|
+
/**
|
|
63
|
+
* The FRACTIONAL difference in days between two timestamp expressions.
|
|
64
|
+
*
|
|
65
|
+
* Raw and unrounded on purpose: every caller wraps it in AVG or SUM and multiplies to the
|
|
66
|
+
* unit it wants (x24 for hours, x1440 for minutes). A helper that rounded would not compose.
|
|
67
|
+
* Either side may be the literal 'now', which both engines understand.
|
|
68
|
+
*/
|
|
69
|
+
daysBetween: (from, to) => `(julianday(${to}) - julianday(${from}))`,
|
|
37
70
|
/** Read a JSON path out of a column: jsonPath('detail', '$.model'). */
|
|
38
71
|
jsonPath: (col, path) => `json_extract(${col}, '${String(path).replace(/'/g, "''")}')`,
|
|
39
72
|
/** Case-insensitive LIKE. SQLite's LIKE is already case-insensitive for ASCII. */
|
|
@@ -60,6 +93,28 @@ const postgres = {
|
|
|
60
93
|
// for both; an app wanting a real timestamptz column in its OWN tables uses nowTz().
|
|
61
94
|
now: () => "to_char(NOW() AT TIME ZONE 'UTC', 'YYYY-MM-DD HH24:MI:SS')",
|
|
62
95
|
nowMinusDays: (n) => `to_char((NOW() - INTERVAL '${Number(n)} days') AT TIME ZONE 'UTC', 'YYYY-MM-DD HH24:MI:SS')`,
|
|
96
|
+
nowPlusMinutes: (n) => `to_char((NOW() + INTERVAL '${Number(n)} minutes') AT TIME ZONE 'UTC', 'YYYY-MM-DD HH24:MI:SS')`,
|
|
97
|
+
// date_trunc on the UTC value, not on NOW(): truncating a timestamptz uses the SESSION time
|
|
98
|
+
// zone, so 'start of month' would land on a different instant depending on where the server
|
|
99
|
+
// thinks it is. Every other timestamp here is UTC text; this has to agree with them.
|
|
100
|
+
startOf: (unit) => {
|
|
101
|
+
if (unit !== 'month' && unit !== 'year') throw new Error(`startOf: expected 'month' or 'year', got ${JSON.stringify(unit)}`);
|
|
102
|
+
return `to_char(date_trunc('${unit}', NOW() AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS')`;
|
|
103
|
+
},
|
|
104
|
+
// ::timestamp on both, because these columns are TEXT on both engines by SQLite heritage and
|
|
105
|
+
// Postgres refuses date arithmetic on text ("operator does not exist: text - text").
|
|
106
|
+
//
|
|
107
|
+
// A NOTE ON WEEKS. SQLite's %W counts weeks from the first Monday, 00-53; Postgres IW is the
|
|
108
|
+
// ISO week, 01-53, paired with the ISO year IYYY. They can disagree by one at a year boundary.
|
|
109
|
+
// That is accepted rather than papered over: the value is a GROUP BY key on a chart axis, and
|
|
110
|
+
// an ISO week is the more defensible of the two to show a person.
|
|
111
|
+
dateGroup: (col, unit) => {
|
|
112
|
+
if (unit === 'month') return `to_char(${col}::timestamp, 'YYYY-MM')`;
|
|
113
|
+
if (unit === 'week') return `to_char(${col}::timestamp, 'IYYY-"W"IW')`;
|
|
114
|
+
throw new Error(`dateGroup: expected 'month' or 'week', got ${JSON.stringify(unit)}`);
|
|
115
|
+
},
|
|
116
|
+
daysBetween: (from, to) =>
|
|
117
|
+
`(EXTRACT(EPOCH FROM (${to}::timestamp - ${from}::timestamp)) / 86400)`,
|
|
63
118
|
/** Native timestamptz, for an app that declares real timestamp columns. */
|
|
64
119
|
nowTz: () => 'NOW()',
|
|
65
120
|
// The ::jsonb cast is load-bearing: `->>` is not defined on TEXT, and the framework's JSON
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aria-framework/db-worker",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.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,
|