mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 03:50:30 +00:00
724c88c201
A mechanical prettier --write over server/src, server/tests, server/scripts and shared/src with the workspaces' .prettierrc; nothing but layout and import order changes, and every file keeps the same set of imports. shared is formatted throughout and its format baseline is empty. The server baseline keeps the 35 files that Prettier would grow past their lint:size limit (they need a split first, which is not a formatting change), and the generated entity files stay as their generator writes them.
62 lines
2.8 KiB
TypeScript
62 lines
2.8 KiB
TypeScript
import type { PluginErrorLog } from '../entities/PluginErrorLog.entity';
|
|
import type { DB } from '../kysely/db';
|
|
import { TrekRepository } from './_shared/trek-repository';
|
|
|
|
/** PR2 delivery row / plugins.service.ts `errors()`'s own projection. */
|
|
export interface PluginErrorLogRow {
|
|
ts: string;
|
|
level: string;
|
|
message: string;
|
|
}
|
|
|
|
/** The tables `pruneKeepingRecent`'s own `this.kysely()` escape hatch (`_shared/trek-repository.ts`) reads, for its correlated subquery on `id`/`plugin_id`. */
|
|
type PluginErrorLogKyselyDB = Pick<DB, 'plugin_error_log'>;
|
|
|
|
export class PluginErrorLogRepository extends TrekRepository<PluginErrorLog> {
|
|
/** PR2 — `INSERT INTO plugin_error_log (plugin_id, level, message) VALUES (?, ?, ?)` (the supervisor's `onLog` callback — §4a, lazily resolved, own try/catch swallow unchanged in the caller). */
|
|
async insertLog(pluginId: string, level: string, message: string): Promise<void> {
|
|
await this.insert({ plugin_id: pluginId, level, message });
|
|
}
|
|
|
|
/**
|
|
* PR3 — `` DELETE FROM plugin_error_log WHERE plugin_id = ? AND id NOT IN (
|
|
* SELECT id FROM plugin_error_log WHERE plugin_id = ? ORDER BY id DESC LIMIT ${retention}
|
|
* ) `` (`pruneErrorLog`'s retention trim). The `NOT IN (subquery … ORDER BY … LIMIT)`
|
|
* shape has no `nativeDelete`/`FilterQuery` equivalent (no correlated-subquery support),
|
|
* so this is one of the few statements per repository that goes through `this.kysely()`
|
|
* (`_shared/trek-repository.ts`'s escape hatch) rather than the QueryBuilder — the
|
|
* `retention` count is a `?`-bound `limit()` call, never interpolated into SQL text.
|
|
*/
|
|
async pruneKeepingRecent(pluginId: string, retention: number): Promise<void> {
|
|
const db = this.kysely<PluginErrorLogKyselyDB>();
|
|
await db
|
|
.deleteFrom('plugin_error_log')
|
|
.where('plugin_id', '=', pluginId)
|
|
.where(
|
|
'id',
|
|
'not in',
|
|
db
|
|
.selectFrom('plugin_error_log')
|
|
.select('id')
|
|
.where('plugin_id', '=', pluginId)
|
|
.orderBy('id', 'desc')
|
|
.limit(retention),
|
|
)
|
|
.execute();
|
|
}
|
|
|
|
/** PR38 (uninstall cascade) / plugins.service.ts's `clearErrors` — `DELETE FROM plugin_error_log WHERE plugin_id = ?` (identical text, two call sites). */
|
|
async deleteAllForPlugin(pluginId: string): Promise<void> {
|
|
await this.nativeDelete({ plugin_id: pluginId });
|
|
}
|
|
|
|
/** plugins.service.ts `errors()` — `SELECT ts, level, message FROM plugin_error_log WHERE plugin_id = ? ORDER BY ts DESC, id DESC LIMIT 200`. */
|
|
async listRecent(pluginId: string, limit = 200): Promise<PluginErrorLogRow[]> {
|
|
const rows = await this.find(
|
|
{ plugin_id: pluginId },
|
|
{ fields: ['ts', 'level', 'message'], orderBy: [{ ts: 'desc' }, { id: 'desc' }], limit },
|
|
);
|
|
return rows.map((r) => ({ ts: r.ts ?? '', level: r.level, message: r.message ?? '' }));
|
|
}
|
|
}
|