mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-08-29 10:21:34 +00:00
DatabaseContext handed every repository a raw better-sqlite3 handle alongside
drizzle, and three of them used it for retention queries built on datetime('now',
?) — a SQLite-only function. That handle is the one thing standing between the
repository layer and a second engine.
Drop it. The two time-based prunes compute their cutoff in JS against the
CURRENT_TIMESTAMP text format, which every engine writes the same way and which
compares correctly as a string; the health-history prune becomes a select of the
rows to keep followed by a NOT IN delete. All three turn async, so their two
callers await them.
Name the dialect rather than repeating a string literal, so adding an engine is
one edit instead of a search.
Tests built their schema through context.sqlite?.exec(). Optional chaining meant
removing the field type-checked cleanly and then silently created no tables, so
the fixture now owns exec() and a raw handle for direct assertions — schema setup
belongs to the test harness, not to the interface repositories consume.
No behaviour change, and no Postgres yet: this only removes the coupling that
would have to be undone first.
21 lines
718 B
TypeScript
21 lines
718 B
TypeScript
/**
|
|
* Timestamp columns are stored as text defaulting to `CURRENT_TIMESTAMP`, which
|
|
* every supported engine writes as `YYYY-MM-DD HH:MM:SS` in UTC. That format
|
|
* sorts lexicographically in time order, so retention cutoffs can be plain
|
|
* string comparisons.
|
|
*
|
|
* Computing the cutoff here rather than with `datetime('now', ?)` keeps the
|
|
* queries free of engine-specific date functions.
|
|
*/
|
|
export function sqlTimestampDaysAgo(
|
|
days: number,
|
|
now: Date = new Date(),
|
|
): string {
|
|
const cutoff = new Date(now.getTime() - days * 24 * 60 * 60 * 1000);
|
|
return formatSqlTimestamp(cutoff);
|
|
}
|
|
|
|
export function formatSqlTimestamp(date: Date): string {
|
|
return date.toISOString().slice(0, 19).replace("T", " ");
|
|
}
|