47 lines
929 B
TypeScript
47 lines
929 B
TypeScript
import { and, eq } from "drizzle-orm";
|
|
import type { CardType } from "./cards";
|
|
import { getDb } from "./db";
|
|
import { cardCache } from "./db/schema";
|
|
|
|
export async function getCachedCard<T>(
|
|
movieId: number,
|
|
cardType: CardType,
|
|
): Promise<T | null> {
|
|
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<T>(
|
|
movieId: number,
|
|
cardType: CardType,
|
|
payload: T,
|
|
): Promise<void> {
|
|
const db = getDb();
|
|
|
|
await db
|
|
.insert(cardCache)
|
|
.values({
|
|
movieId,
|
|
cardType,
|
|
payload,
|
|
})
|
|
.onConflictDoUpdate({
|
|
target: [cardCache.movieId, cardCache.cardType],
|
|
set: {
|
|
payload,
|
|
createdAt: new Date(),
|
|
},
|
|
});
|
|
} |