Inject application database contexts

This commit is contained in:
Julian Appel 2026-07-26 12:30:42 +02:00
parent 859fa4a42a
commit d7b98de56a
26 changed files with 201 additions and 63 deletions

View file

@ -1,6 +1,6 @@
import crypto from "node:crypto";
import { eq } from "drizzle-orm";
import { db } from "../client.js";
import type { AppDatabase } from "../database-context.js";
import { globalDevices } from "../schema/global-devices.js";
import type {
CreateGlobalDeviceInput,
@ -8,11 +8,15 @@ import type {
} 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,
@ -31,6 +35,7 @@ export class GlobalDeviceRepository {
}
async update(globalDeviceId: string, input: UpdateGlobalDeviceInput) {
const db = this.database;
await db
.update(globalDevices)
.set({
@ -49,11 +54,13 @@ export class GlobalDeviceRepository {
}
async findById(globalDeviceId: string) {
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));
}
}