Files
TREK/server/src/db/repositories/PluginUserErasureQueue.repository.ts
T
Maurice 5f17a5df50 refactor(db): generate the Kysely table types from the migrated schema
The repositories typed their Kysely statements against 159 table interfaces
written by hand, one or more per repository. Nothing compared them with the
schema: a migration that renamed a column or changed its type left them
stale while tsc stayed green, and several had already drifted
(reservations.accommodation_id is TEXT, not INTEGER).

scripts/generate-db-types.ts migrates a throwaway database with the real
migrations, reads every table back and writes one interface per table into
src/db/kysely/ plus the DB map in db.ts. Every hand-written interface is now
Pick<DB, ...> of that type, and npm run check:db-types fails when the
committed files no longer match the migrations.

A column an insert may leave out (rowid, DEFAULT, nullable) is
InsertOptional<T>, declared as ColumnType<T, T | void, T>. The server
compiles without strictNullChecks, where Kysely's own Generated<T> still
demands the column; that is why the old code kept separate insert-only
interfaces. The ids callers pass as number | string are cast to the column
type at the bind, so every statement binds the same value as before.
2026-10-08 18:39:33 +02:00

64 lines
2.7 KiB
TypeScript

import type { PluginUserErasureQueue } from '../entities/PluginUserErasureQueue.entity';
import { TrekRepository } from './_shared/trek-repository';
import type { DB } from '../kysely/db';
/** The drain's own pending-row projection. */
export interface PendingErasureRow {
id: number;
plugin_id: string;
user_id: number;
}
/** The tables the orphan-scan's correlated `NOT IN (SELECT id FROM plugins)` subquery reads. */
type PluginUserErasureQueueKyselyDB = Pick<DB, 'plugin_user_erasure_queue' | 'plugins'>;
export class PluginUserErasureQueueRepository extends TrekRepository<PluginUserErasureQueue> {
/** PR8 — `INSERT OR IGNORE INTO plugin_user_erasure_queue (plugin_id, user_id) VALUES (?, ?)` (idempotent — a duplicate enqueue for the same pair is a no-op). */
async insertIgnore(pluginId: string, userId: number): Promise<void> {
await this.upsert({ plugin_id: pluginId, user_id: userId }, { onConflictFields: ['plugin_id', 'user_id'], onConflictAction: 'ignore' });
}
/**
* `runDrainOnce`'s orphan scan — `SELECT DISTINCT plugin_id FROM plugin_user_erasure_queue
* WHERE plugin_id NOT IN (SELECT id FROM plugins)`. A cross-table correlated `NOT IN`
* has no `FilterQuery` equivalent, so this goes through `this.kysely()`.
*/
async listOrphanPluginIds(): Promise<string[]> {
const db = this.kysely<PluginUserErasureQueueKyselyDB>();
const rows = await db
.selectFrom('plugin_user_erasure_queue')
.select('plugin_id')
.distinct()
.where('plugin_id', 'not in', db.selectFrom('plugins').select('id'))
.execute();
return rows.map((r) => r.plugin_id);
}
/**
* `runDrainOnce`'s reap (a truly orphaned data dir) / `uninstall`'s `deleteData` branch —
* `DELETE FROM plugin_user_erasure_queue WHERE plugin_id = ?` (identical text, two call sites).
*/
async deleteAllForPlugin(pluginId: string): Promise<void> {
await this.nativeDelete({ plugin_id: pluginId });
}
/**
* `runDrainOnce`'s delivery window — `` SELECT id, plugin_id, user_id FROM
* plugin_user_erasure_queue WHERE plugin_id IN (${ph}) ORDER BY id LIMIT 200 ``.
* `pluginIds` — never empty: the caller already returns early when
* `supervisor.activeIds()` is empty.
*/
async findPendingForPlugins(pluginIds: string[], limit = 200): Promise<PendingErasureRow[]> {
const rows = await this.find(
{ plugin_id: { $in: pluginIds } },
{ fields: ['id', 'plugin_id', 'user_id'], orderBy: { id: 'asc' }, limit },
);
return rows.map((r) => ({ id: r.id, plugin_id: r.plugin_id, user_id: r.user_id }));
}
/** `runDrainOnce`'s ACK — `DELETE FROM plugin_user_erasure_queue WHERE id = ?` (a delivered row). */
async deleteById(id: number): Promise<void> {
await this.nativeDelete({ id });
}
}