first commit
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
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(),
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user