tsx runs tests without type checking and tsconfig.json covers only src, so 71 type errors had accumulated unnoticed. tsconfig.tests.json now checks every test and helper, and npm run typecheck includes it. Fixtures gain the fields the DTOs have grown since they were written (manualQuantity, circuit apparent power/cos phi, downstream board loads, section totals, building, cover and board info fields). Repository lookups that return "row ?? null" now declare the null in their type, which their callers already handle. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
66 lines
2.1 KiB
TypeScript
66 lines
2.1 KiB
TypeScript
import crypto from "node:crypto";
|
|
import { eq } from "drizzle-orm";
|
|
import type { AppDatabase } from "../database-context.js";
|
|
import { globalDevices } from "../schema/global-devices.js";
|
|
import type {
|
|
CreateGlobalDeviceInput,
|
|
UpdateGlobalDeviceInput,
|
|
} from "../../shared/validation/global-device.schemas.js";
|
|
|
|
export class GlobalDeviceRepository {
|
|
constructor(private readonly database: AppDatabase) {}
|
|
|
|
async list() {
|
|
const db = this.database;
|
|
return db.select().from(globalDevices);
|
|
}
|
|
|
|
async create(input: CreateGlobalDeviceInput) {
|
|
const db = this.database;
|
|
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: null,
|
|
phaseCount: input.phaseCount,
|
|
powerFactor: input.powerFactor ?? null,
|
|
note: input.note ?? null,
|
|
});
|
|
return { id, ...input };
|
|
}
|
|
|
|
async update(globalDeviceId: string, input: UpdateGlobalDeviceInput) {
|
|
const db = this.database;
|
|
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: null,
|
|
phaseCount: input.phaseCount,
|
|
powerFactor: input.powerFactor ?? null,
|
|
note: input.note ?? null,
|
|
})
|
|
.where(eq(globalDevices.id, globalDeviceId));
|
|
}
|
|
|
|
async findById(globalDeviceId: string): Promise<typeof globalDevices.$inferSelect | null> {
|
|
const db = this.database;
|
|
const [row] = await db.select().from(globalDevices).where(eq(globalDevices.id, globalDeviceId)).limit(1);
|
|
return row ?? null;
|
|
}
|
|
|
|
async delete(globalDeviceId: string) {
|
|
const db = this.database;
|
|
await db.delete(globalDevices).where(eq(globalDevices.id, globalDeviceId));
|
|
}
|
|
}
|