feat: add system configuration platform

This commit is contained in:
2026-07-02 00:03:27 -07:00
parent aed5c6db56
commit e3e7b0d8e0
41 changed files with 5746 additions and 405 deletions

37
modules/core/server/db.ts Normal file
View File

@@ -0,0 +1,37 @@
import "server-only";
import { sql } from "drizzle-orm";
import { drizzle, type PostgresJsDatabase } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "@/modules/core/server/schema";
export type AppDatabase = PostgresJsDatabase<typeof schema>;
let cachedDatabase: AppDatabase | null = null;
let cachedClient: postgres.Sql | null = null;
export function getDatabase(): AppDatabase {
const databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) {
throw new Error("DATABASE_URL is required for database-backed system configuration");
}
if (!cachedDatabase) {
cachedClient = postgres(databaseUrl, { max: 1 });
cachedDatabase = drizzle(cachedClient, { schema });
}
return cachedDatabase;
}
export async function checkDatabaseConnection(): Promise<{ ok: boolean; reason: string }> {
try {
const database = getDatabase();
await database.execute(sql`select 1`);
return { ok: true, reason: "数据库连接正常" };
} catch (error) {
const reason = error instanceof Error ? error.message : "数据库连接失败";
return { ok: false, reason };
}
}