import crypto from "node:crypto"; import { eq } from "drizzle-orm"; import { db } from "../client.js"; import { globalDevices } from "../schema/global-devices.js"; import type { CreateGlobalDeviceInput, UpdateGlobalDeviceInput, } from "../../shared/validation/global-device.schemas.js"; export class GlobalDeviceRepository { async list() { return db.select().from(globalDevices); } async create(input: CreateGlobalDeviceInput) { const id = crypto.randomUUID(); await db.insert(globalDevices).values({ id, name: input.name, displayName: input.displayName, category: input.category ?? null, quantity: input.quantity, installedPowerPerUnitKw: input.installedPowerPerUnitKw, demandFactor: input.demandFactor, voltageV: input.voltageV ?? null, phaseCount: input.phaseCount ?? null, powerFactor: input.powerFactor ?? null, note: input.note ?? null, }); return { id, ...input }; } async update(globalDeviceId: string, input: UpdateGlobalDeviceInput) { await db .update(globalDevices) .set({ name: input.name, displayName: input.displayName, category: input.category ?? null, quantity: input.quantity, installedPowerPerUnitKw: input.installedPowerPerUnitKw, demandFactor: input.demandFactor, voltageV: input.voltageV ?? null, phaseCount: input.phaseCount ?? null, powerFactor: input.powerFactor ?? null, note: input.note ?? null, }) .where(eq(globalDevices.id, globalDeviceId)); } async findById(globalDeviceId: string) { const [row] = await db.select().from(globalDevices).where(eq(globalDevices.id, globalDeviceId)).limit(1); return row ?? null; } async delete(globalDeviceId: string) { await db.delete(globalDevices).where(eq(globalDevices.id, globalDeviceId)); } }