import { and, eq } from "drizzle-orm"; import type { CardType } from "./cards"; import { getDb } from "./db"; import { cardCache } from "./db/schema"; export async function getCachedCard( movieId: number, cardType: CardType, ): Promise { const db = getDb(); const rows = await db .select() .from(cardCache) .where( and(eq(cardCache.movieId, movieId), eq(cardCache.cardType, cardType)), ) .limit(1); if (rows.length === 0) { return null; } return rows[0].payload as T; } export async function setCachedCard( movieId: number, cardType: CardType, payload: T, ): Promise { const db = getDb(); await db .insert(cardCache) .values({ movieId, cardType, payload, }) .onConflictDoUpdate({ target: [cardCache.movieId, cardCache.cardType], set: { payload, createdAt: new Date(), }, }); }