fix(settings): store JSON as string in DB and parse on read\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This commit is contained in:
2026-05-16 20:18:32 +02:00
parent dba81832c1
commit 9b63d981b0
+11 -3
View File
@@ -13,7 +13,14 @@ 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 prisma.appSetting.findMany();
rows.forEach(r => this.cache.set(r.key, r.value)); 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);
}
});
this.initialized = true; this.initialized = true;
} }
@@ -24,10 +31,11 @@ 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);
await prisma.appSetting.upsert({ await prisma.appSetting.upsert({
where: { key }, where: { key },
update: { value, updatedBy }, update: { value: valueStr, updatedBy },
create: { key, value, 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 });