fix: use shared db instance in settingsService to resolve Prisma type errors
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
// app/lib/settings.server.ts
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import EventEmitter from 'events';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
import { db } from "./db.server";
|
||||
import EventEmitter from "events";
|
||||
|
||||
type JSONValue = any;
|
||||
|
||||
@@ -12,12 +10,11 @@ class SettingsService extends EventEmitter {
|
||||
|
||||
async init() {
|
||||
if (this.initialized) return;
|
||||
const rows = await prisma.appSetting.findMany();
|
||||
rows.forEach(r => {
|
||||
const rows = await db.appSetting.findMany();
|
||||
rows.forEach((r) => {
|
||||
try {
|
||||
this.cache.set(r.key, JSON.parse(r.value));
|
||||
} catch (e) {
|
||||
// fall back to raw string if parse fails
|
||||
this.cache.set(r.key, r.value);
|
||||
}
|
||||
});
|
||||
@@ -31,20 +28,20 @@ class SettingsService extends EventEmitter {
|
||||
|
||||
async set(key: string, value: JSONValue, updatedBy?: string) {
|
||||
if (!this.initialized) await this.init();
|
||||
const valueStr = typeof value === 'string' ? value : JSON.stringify(value);
|
||||
await prisma.appSetting.upsert({
|
||||
const valueStr = typeof value === "string" ? value : JSON.stringify(value);
|
||||
await db.appSetting.upsert({
|
||||
where: { key },
|
||||
update: { value: valueStr, updatedBy },
|
||||
create: { key, value: valueStr, updatedBy },
|
||||
});
|
||||
this.cache.set(key, value);
|
||||
this.emit('update', { key, value });
|
||||
this.emit("update", { key, value });
|
||||
return { key, value };
|
||||
}
|
||||
|
||||
subscribe(fn: (payload: { key: string; value: any }) => void) {
|
||||
this.on('update', fn);
|
||||
return () => this.off('update', fn);
|
||||
this.on("update", fn);
|
||||
return () => this.off("update", fn);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user