Files
TREK/server/src/db/repositories/PluginUserErasureQueue.repository.ts
T
Maurice 724c88c201 style: format server and shared with prettier
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.
2026-10-10 21:23:37 +02:00

67 lines
2.8 KiB
TypeScript

import type { PluginUserErasureQueue } from '../entities/PluginUserErasureQueue.entity';
import type { DB } from '../kysely/db';
import { TrekRepository } from './_shared/trek-repository';
/** 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 });
}
}