You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.7 KiB
76 lines
1.7 KiB
import { dbGlobal } from "drizzle-pkg/lib/db";
|
|
import { tools } from "drizzle-pkg/lib/schema/content";
|
|
import { eq, asc } from "drizzle-orm";
|
|
|
|
// ============ Types ============
|
|
export interface CreateToolInput {
|
|
name: string;
|
|
slug: string;
|
|
icon?: string;
|
|
sortOrder?: number;
|
|
}
|
|
|
|
export interface UpdateToolInput {
|
|
name?: string;
|
|
slug?: string;
|
|
icon?: string | null;
|
|
sortOrder?: number;
|
|
}
|
|
|
|
// ============ Helpers ============
|
|
function uuid() {
|
|
return crypto.randomUUID();
|
|
}
|
|
|
|
// ============ CRUD ============
|
|
|
|
export async function listTools() {
|
|
const rows = await dbGlobal
|
|
.select()
|
|
.from(tools)
|
|
.orderBy(asc(tools.sortOrder), asc(tools.name));
|
|
|
|
return rows;
|
|
}
|
|
|
|
export async function getToolById(id: string) {
|
|
const rows = await dbGlobal
|
|
.select()
|
|
.from(tools)
|
|
.where(eq(tools.id, id))
|
|
.limit(1);
|
|
return rows[0] ?? null;
|
|
}
|
|
|
|
export async function createTool(input: CreateToolInput) {
|
|
const id = uuid();
|
|
await dbGlobal.insert(tools).values({
|
|
id,
|
|
name: input.name,
|
|
slug: input.slug,
|
|
icon: input.icon ?? null,
|
|
sortOrder: input.sortOrder ?? 0,
|
|
});
|
|
return getToolById(id);
|
|
}
|
|
|
|
export async function updateTool(id: string, input: UpdateToolInput) {
|
|
const existing = await getToolById(id);
|
|
if (!existing) return null;
|
|
|
|
await dbGlobal
|
|
.update(tools)
|
|
.set({
|
|
...(input.name !== undefined && { name: input.name }),
|
|
...(input.slug !== undefined && { slug: input.slug }),
|
|
...(input.icon !== undefined && { icon: input.icon }),
|
|
...(input.sortOrder !== undefined && { sortOrder: input.sortOrder }),
|
|
})
|
|
.where(eq(tools.id, id));
|
|
|
|
return getToolById(id);
|
|
}
|
|
|
|
export async function deleteTool(id: string) {
|
|
await dbGlobal.delete(tools).where(eq(tools.id, id));
|
|
}
|
|
|