Files
buzz-sheet/lib/glossary-repository.ts
T
gunshiz 9896e8a4ad
CI / Verify (push) Successful in 1m35s
CI / Build immutable images and deploy (push) Successful in 5m27s
feat(guides) : add safer publishing and discovery
2026-09-19 13:32:57 +07:00

55 lines
1.4 KiB
TypeScript

import "server-only";
import { asc, eq } from "drizzle-orm";
import { getDb } from "@/db";
import {
catalogArtifacts,
catalogCharacters,
catalogWeapons,
glossaryAliases,
} from "@/db/schema";
import {
glossaryAliasInputSchema,
normalizeGlossaryAlias,
} from "@/lib/glossary";
const catalogTables = {
character: catalogCharacters,
weapon: catalogWeapons,
artifact: catalogArtifacts,
} as const;
export async function listGlossaryAliases() {
return getDb().select().from(glossaryAliases).orderBy(
asc(glossaryAliases.kind),
asc(glossaryAliases.alias),
);
}
export async function createGlossaryAlias(input: unknown) {
const data = glossaryAliasInputSchema.parse(input);
const table = catalogTables[data.kind];
const [item] = await getDb()
.select({ key: table.key })
.from(table)
.where(eq(table.key, data.catalogKey))
.limit(1);
if (!item) throw new Error("ไม่พบรายการใน Catalog");
const [created] = await getDb().insert(glossaryAliases).values({
...data,
alias: data.alias.trim(),
normalizedAlias: normalizeGlossaryAlias(data.alias),
}).returning();
return created;
}
export async function deleteGlossaryAlias(id: string) {
const [deleted] = await getDb()
.delete(glossaryAliases)
.where(eq(glossaryAliases.id, id))
.returning();
if (!deleted) throw new Error("ไม่พบ Shortcut");
return deleted;
}