import { and, eq } from "drizzle-orm"; import { danmakuDocs, type DanmakuDocRow } from "@app/models"; import { db } from "@app/db"; export const danmakuDocsDao = { async get(userId: string, mediaItemId: string, source: string): Promise { const [row] = await db .select() .from(danmakuDocs) .where( and( eq(danmakuDocs.userId, userId), eq(danmakuDocs.mediaItemId, mediaItemId), eq(danmakuDocs.source, source), ), ) .limit(1); return row ?? null; }, async upsert(data: { userId: string; mediaItemId: string; source: string; xml: string; byteSize: number; }): Promise { const existing = await danmakuDocsDao.get(data.userId, data.mediaItemId, data.source); if (existing) { const [row] = await db .update(danmakuDocs) .set({ xml: data.xml, byteSize: data.byteSize, updatedAt: new Date() }) .where(eq(danmakuDocs.id, existing.id)) .returning(); if (!row) throw new Error("Failed to update danmaku doc"); return row; } const rows = await db.insert(danmakuDocs).values(data).returning(); const row = rows[0]; if (!row) throw new Error("Failed to insert danmaku doc"); return row; }, async remove(userId: string, mediaItemId: string, source: string): Promise { await db .delete(danmakuDocs) .where( and( eq(danmakuDocs.userId, userId), eq(danmakuDocs.mediaItemId, mediaItemId), eq(danmakuDocs.source, source), ), ); }, };