63 lines
1.6 KiB
TypeScript
63 lines
1.6 KiB
TypeScript
import "server-only";
|
|
|
|
import { and, asc, eq } from "drizzle-orm";
|
|
|
|
import { getDb } from "@/db";
|
|
import {
|
|
catalogArtifacts,
|
|
catalogCharacters,
|
|
catalogWeapons,
|
|
glossaryAliases,
|
|
} from "@/db/schema";
|
|
import {
|
|
glossaryAliasInputSchema,
|
|
normalizeGlossaryAlias,
|
|
type GlossaryKind,
|
|
} 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;
|
|
}
|
|
|
|
export async function aliasesForItem(kind: GlossaryKind, catalogKey: string) {
|
|
return getDb().select().from(glossaryAliases).where(and(
|
|
eq(glossaryAliases.kind, kind),
|
|
eq(glossaryAliases.catalogKey, catalogKey),
|
|
));
|
|
}
|