Files
LaunchPad/apps/backend/src/db/repositories/logs.ts

53 lines
1.3 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { desc } from "drizzle-orm";
import type { ScanLogEntry } from "@launchpad/shared";
import { db } from "../client.js";
import { scanLogs } from "../schema.js";
function mapRow(row: typeof scanLogs.$inferSelect): ScanLogEntry {
return {
id: row.id,
type: row.type as ScanLogEntry["type"],
targetId: row.targetId,
level: row.level as ScanLogEntry["level"],
message: row.message,
createdAt: row.createdAt,
};
}
export interface LogScanInput {
type: ScanLogEntry["type"];
targetId?: string | null;
level: ScanLogEntry["level"];
message: string;
}
/** Schreibt einen Eintrag für einen Scan-Versuch (Erfolg oder Fehler). */
export function logScan(input: LogScanInput): ScanLogEntry {
const id = randomUUID();
const createdAt = new Date().toISOString();
db.insert(scanLogs)
.values({
id,
type: input.type,
targetId: input.targetId ?? null,
level: input.level,
message: input.message,
createdAt,
})
.run();
return { id, type: input.type, targetId: input.targetId ?? null, level: input.level, message: input.message, createdAt };
}
export function listLogs(limit = 100): ScanLogEntry[] {
return db
.select()
.from(scanLogs)
.orderBy(desc(scanLogs.createdAt))
.limit(limit)
.all()
.map(mapRow);
}