import { and, eq } from "drizzle-orm"; import { db } from "../client.js"; import { circuitLists } from "../schema/circuit-lists.js"; export class CircuitListRepository { async listByProject(projectId: string) { return db.select().from(circuitLists).where(eq(circuitLists.projectId, projectId)); } async createForDistributionBoard(input: { projectId: string; distributionBoardId: string; name: string; }) { const entry = { id: input.distributionBoardId, projectId: input.projectId, distributionBoardId: input.distributionBoardId, name: input.name, }; await db.insert(circuitLists).values(entry); return entry; } async findByDistributionBoardId(projectId: string, distributionBoardId: string) { const [row] = await db .select() .from(circuitLists) .where( and( eq(circuitLists.projectId, projectId), eq(circuitLists.distributionBoardId, distributionBoardId) ) ) .limit(1); return row ?? null; } async existsInProject(projectId: string, circuitListId: string) { const [row] = await db .select({ id: circuitLists.id }) .from(circuitLists) .where(and(eq(circuitLists.projectId, projectId), eq(circuitLists.id, circuitListId))) .limit(1); return Boolean(row); } async findById(projectId: string, circuitListId: string) { const [row] = await db .select() .from(circuitLists) .where(and(eq(circuitLists.projectId, projectId), eq(circuitLists.id, circuitListId))) .limit(1); return row ?? null; } async findByIdByListIdOnly(circuitListId: string) { const [row] = await db .select() .from(circuitLists) .where(eq(circuitLists.id, circuitListId)) .limit(1); return row ?? null; } }