feat: Implement default API site seeding, add a dialect-aware setting upsert utility, and introduce check-in API routes with background task processing.

This commit is contained in:
cita
2026-03-11 14:31:23 +08:00
parent f43247e91d
commit 921c551a3a
6 changed files with 52 additions and 38 deletions
+41
View File
@@ -0,0 +1,41 @@
import { eq } from 'drizzle-orm';
import { db, runtimeDbDialect, schema } from './index.js';
/**
* Dialect-aware upsert for the `settings` table.
*
* SQLite / PostgreSQL use `onConflictDoUpdate` while MySQL uses
* `onDuplicateKeyUpdate`. Drizzle exposes different builder types for each
* dialect, so we branch at runtime.
*/
export async function upsertSetting(key: string, value: unknown, txDb: typeof db = db): Promise<void> {
const jsonValue = JSON.stringify(value);
if (runtimeDbDialect === 'mysql') {
// MySQL path – try update first, insert if not exists
const existing = await txDb.select({ key: schema.settings.key })
.from(schema.settings)
.where(eq(schema.settings.key, key))
.get();
if (existing) {
await txDb.update(schema.settings)
.set({ value: jsonValue })
.where(eq(schema.settings.key, key))
.run();
} else {
await txDb.insert(schema.settings)
.values({ key, value: jsonValue })
.run();
}
} else {
// SQLite / PostgreSQL path
await (txDb.insert(schema.settings)
.values({ key, value: jsonValue }) as any)
.onConflictDoUpdate({
target: schema.settings.key,
set: { value: jsonValue },
})
.run();
}
}