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:
@@ -13,7 +13,14 @@ class SettingsService extends EventEmitter {
|
||||
async init() {
|
||||
if (this.initialized) return;
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -24,10 +31,11 @@ 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({
|
||||
where: { key },
|
||||
update: { value, updatedBy },
|
||||
create: { key, value, updatedBy },
|
||||
update: { value: valueStr, updatedBy },
|
||||
create: { key, value: valueStr, updatedBy },
|
||||
});
|
||||
this.cache.set(key, value);
|
||||
this.emit('update', { key, value });
|
||||
|
||||
Reference in New Issue
Block a user