This commit is contained in:
@@ -50,12 +50,11 @@ async function getSetting(key) {
|
|||||||
// Helper: set setting in DB
|
// Helper: set setting in DB
|
||||||
async function setSetting(key, value) {
|
async function setSetting(key, value) {
|
||||||
const db = getDb();
|
const db = getDb();
|
||||||
// Upsert
|
// Try update first, then insert if nothing was updated
|
||||||
const existing = await db.get('SELECT key FROM settings WHERE key = ?', [key]);
|
const result = await db.run('UPDATE settings SET value = ?, updated_at = CURRENT_TIMESTAMP WHERE key = ?', [value, key]);
|
||||||
if (existing) {
|
if (result.changes === 0) {
|
||||||
await db.run('UPDATE settings SET value = ?, updated_at = CURRENT_TIMESTAMP WHERE key = ?', [value, key]);
|
// Use INSERT with a dummy RETURNING to satisfy PG adapter, or just use exec-style
|
||||||
} else {
|
await db.run('INSERT INTO settings (key, value) VALUES (?, ?) RETURNING key', [key, value]);
|
||||||
await db.run('INSERT INTO settings (key, value) VALUES (?, ?)', [key, value]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user