feat(38-01): schema parentId + tag service CRUD + cycle detection

- Add parentId self-ref FK to tags table (ON DELETE SET NULL)
- Generate Drizzle migration 0010_yielding_random.sql
- Extend tag.service.ts with getAdminTags, getTagWithCounts, createTag, updateTag, deleteTag, isDescendant
- Add service tests (14 tests, all pass)
This commit is contained in:
2026-04-19 22:26:47 +02:00
parent c0a0aeff77
commit 8cefdf625b
4 changed files with 245 additions and 3 deletions

View File

@@ -204,6 +204,7 @@ export const globalItems = pgTable(
export const tags = pgTable("tags", {
id: serial("id").primaryKey(),
name: text("name").notNull().unique(),
parentId: integer("parent_id").references(() => tags.id, { onDelete: "set null" }),
createdAt: timestamp("created_at").defaultNow().notNull(),
});

View File

@@ -1,6 +1,6 @@
import { asc } from "drizzle-orm";
import { asc, count, eq } from "drizzle-orm";
import { db as prodDb } from "../../db/index.ts";
import { tags } from "../../db/schema.ts";
import { globalItemTags, tags } from "../../db/schema.ts";
type Db = typeof prodDb;
@@ -10,3 +10,91 @@ export async function getAllTags(db: Db = prodDb) {
.from(tags)
.orderBy(asc(tags.name));
}
export async function getAdminTags(db: Db = prodDb) {
return db
.select({
id: tags.id,
name: tags.name,
parentId: tags.parentId,
itemCount: count(globalItemTags.globalItemId),
})
.from(tags)
.leftJoin(globalItemTags, eq(globalItemTags.tagId, tags.id))
.groupBy(tags.id, tags.name, tags.parentId)
.orderBy(asc(tags.name));
}
export async function getTagWithCounts(db: Db, id: number) {
const [tag] = await db
.select({
id: tags.id,
name: tags.name,
parentId: tags.parentId,
itemCount: count(globalItemTags.globalItemId),
})
.from(tags)
.leftJoin(globalItemTags, eq(globalItemTags.tagId, tags.id))
.where(eq(tags.id, id))
.groupBy(tags.id, tags.name, tags.parentId);
return tag ?? null;
}
export async function createTag(
db: Db,
data: { name: string; parentId?: number | null },
) {
const [tag] = await db
.insert(tags)
.values({ name: data.name, parentId: data.parentId ?? null })
.returning();
return tag!;
}
function isDescendant(
allTags: { id: number; parentId: number | null }[],
candidateParentId: number,
tagId: number,
): boolean {
let current: number | null = candidateParentId;
const visited = new Set<number>();
while (current !== null) {
if (current === tagId) return true;
if (visited.has(current)) break;
visited.add(current);
const node = allTags.find((t) => t.id === current);
current = node?.parentId ?? null;
}
return false;
}
export async function updateTag(
db: Db,
id: number,
data: { name?: string; parentId?: number | null },
) {
if (data.parentId != null) {
const allTags = await db
.select({ id: tags.id, parentId: tags.parentId })
.from(tags);
if (isDescendant(allTags, data.parentId, id)) {
throw new Error(
"Cycle detected: the selected parent is a descendant of this tag.",
);
}
}
const [updated] = await db
.update(tags)
.set({ ...(data.name && { name: data.name }), parentId: data.parentId })
.where(eq(tags.id, id))
.returning();
return updated ?? null;
}
export async function deleteTag(db: Db, id: number) {
const [deleted] = await db
.delete(tags)
.where(eq(tags.id, id))
.returning({ id: tags.id });
return deleted != null;
}