leistungsbilanz-ts/src/db/repositories/project-device.repository.ts

30 lines
975 B
TypeScript

import { and, eq } from "drizzle-orm";
import { db } from "../client.js";
import { projectDevices } from "../schema/project-devices.js";
import { calculateRowTotalPower } from "../../domain/calculations/circuit-power-calculation.js";
function withTotalPower(row: typeof projectDevices.$inferSelect) {
return {
...row,
totalPower: calculateRowTotalPower(row.quantity, row.powerPerUnit, row.simultaneityFactor),
};
}
export class ProjectDeviceRepository {
async listByProject(projectId: string) {
const rows = await db.select().from(projectDevices).where(eq(projectDevices.projectId, projectId));
return rows.map(withTotalPower);
}
async findById(projectId: string, projectDeviceId: string) {
const [row] = await db
.select()
.from(projectDevices)
.where(
and(eq(projectDevices.id, projectDeviceId), eq(projectDevices.projectId, projectId))
)
.limit(1);
return row ? withTotalPower(row) : null;
}
}