fix: use shared db instance in settingsService to resolve Prisma type errors

This commit is contained in:
2026-05-16 21:23:19 +02:00
parent 1f7c07b427
commit 2c0d639c32
+9 -12
View File
@@ -1,8 +1,6 @@
// app/lib/settings.server.ts // app/lib/settings.server.ts
import { PrismaClient } from '@prisma/client'; import { db } from "./db.server";
import EventEmitter from 'events'; import EventEmitter from "events";
const prisma = new PrismaClient();
type JSONValue = any; type JSONValue = any;
@@ -12,12 +10,11 @@ class SettingsService extends EventEmitter {
async init() { async init() {
if (this.initialized) return; if (this.initialized) return;
const rows = await prisma.appSetting.findMany(); const rows = await db.appSetting.findMany();
rows.forEach(r => { rows.forEach((r) => {
try { try {
this.cache.set(r.key, JSON.parse(r.value)); this.cache.set(r.key, JSON.parse(r.value));
} catch (e) { } catch (e) {
// fall back to raw string if parse fails
this.cache.set(r.key, r.value); this.cache.set(r.key, r.value);
} }
}); });
@@ -31,20 +28,20 @@ class SettingsService extends EventEmitter {
async set(key: string, value: JSONValue, updatedBy?: string) { async set(key: string, value: JSONValue, updatedBy?: string) {
if (!this.initialized) await this.init(); if (!this.initialized) await this.init();
const valueStr = typeof value === 'string' ? value : JSON.stringify(value); const valueStr = typeof value === "string" ? value : JSON.stringify(value);
await prisma.appSetting.upsert({ await db.appSetting.upsert({
where: { key }, where: { key },
update: { value: valueStr, updatedBy }, update: { value: valueStr, updatedBy },
create: { key, value: valueStr, updatedBy }, create: { key, value: valueStr, updatedBy },
}); });
this.cache.set(key, value); this.cache.set(key, value);
this.emit('update', { key, value }); this.emit("update", { key, value });
return { key, value }; return { key, value };
} }
subscribe(fn: (payload: { key: string; value: any }) => void) { subscribe(fn: (payload: { key: string; value: any }) => void) {
this.on('update', fn); this.on("update", fn);
return () => this.off('update', fn); return () => this.off("update", fn);
} }
} }