40 lines
902 B
TypeScript
40 lines
902 B
TypeScript
"use server";
|
|
|
|
import { db } from "@/db";
|
|
import { items } from "@/db/schema";
|
|
import { eq, desc } from "drizzle-orm";
|
|
import { revalidatePath } from "next/cache";
|
|
|
|
export async function getItems() {
|
|
return await db.query.items.findMany({
|
|
orderBy: [desc(items.createdAt)],
|
|
});
|
|
}
|
|
|
|
export async function createItem(formData: FormData) {
|
|
const key = formData.get("key") as string;
|
|
const title = formData.get("title") as string;
|
|
const content = formData.get("content") as string;
|
|
const type = formData.get("type") as string;
|
|
|
|
await db.insert(items).values({
|
|
key,
|
|
title,
|
|
content,
|
|
type,
|
|
});
|
|
|
|
revalidatePath("/");
|
|
}
|
|
|
|
export async function deleteItem(id: number) {
|
|
await db.delete(items).where(eq(items.id, id));
|
|
revalidatePath("/");
|
|
}
|
|
|
|
export async function getItemByKey(key: string) {
|
|
return await db.query.items.findFirst({
|
|
where: eq(items.key, key),
|
|
});
|
|
}
|