(key: string, value: unknown, txDb: typeof db = db)
| 9 | * dialect, so we branch at runtime. |
| 10 | */ |
| 11 | export async function upsertSetting(key: string, value: unknown, txDb: typeof db = db): Promise<void> { |
| 12 | const jsonValue = JSON.stringify(value); |
| 13 | |
| 14 | if (runtimeDbDialect === 'mysql') { |
| 15 | // MySQL path – try update first, insert if not exists |
| 16 | const existing = await txDb.select({ key: schema.settings.key }) |
| 17 | .from(schema.settings) |
| 18 | .where(eq(schema.settings.key, key)) |
| 19 | .get(); |
| 20 | |
| 21 | if (existing) { |
| 22 | await txDb.update(schema.settings) |
| 23 | .set({ value: jsonValue }) |
| 24 | .where(eq(schema.settings.key, key)) |
| 25 | .run(); |
| 26 | } else { |
| 27 | await txDb.insert(schema.settings) |
| 28 | .values({ key, value: jsonValue }) |
| 29 | .run(); |
| 30 | } |
| 31 | } else { |
| 32 | // SQLite / PostgreSQL path |
| 33 | await (txDb.insert(schema.settings) |
| 34 | .values({ key, value: jsonValue }) as any) |
| 35 | .onConflictDoUpdate({ |
| 36 | target: schema.settings.key, |
| 37 | set: { value: jsonValue }, |
| 38 | }) |
| 39 | .run(); |
| 40 | } |
| 41 | } |
no test coverage detected