1
0
forked from boos/BoosAPI

Add tests for settings database runtime configuration API

- Implement tests for retrieving and saving runtime database configuration.
- Validate handling of invalid configuration payloads.
- Utilize Fastify for API testing with Vitest.
- Ensure proper cleanup of database state between tests.
This commit is contained in:
cita
2026-03-05 18:57:18 +08:00
parent 283c052fa5
commit b122893796
6 changed files with 715 additions and 180 deletions
+32 -3
View File
@@ -9,7 +9,7 @@ import { config } from '../config.js';
import { mkdirSync } from 'fs';
import { dirname, resolve } from 'path';
type RuntimeDbDialect = 'sqlite' | 'mysql' | 'postgres';
export type RuntimeDbDialect = 'sqlite' | 'mysql' | 'postgres';
type SqlMethod = 'all' | 'get' | 'run' | 'values' | 'execute';
const TABLES_WITH_NUMERIC_ID = new Set([
@@ -26,7 +26,7 @@ const TABLES_WITH_NUMERIC_ID = new Set([
'events',
]);
export const runtimeDbDialect: RuntimeDbDialect = config.dbType;
export let runtimeDbDialect: RuntimeDbDialect = config.dbType;
let sqliteConnection: Database.Database | null = null;
let mysqlPool: mysql.Pool | null = null;
@@ -590,7 +590,13 @@ function initDb(): AppDb {
return initSqliteDb();
}
export const db: any = initDb();
let activeDb: AppDb = initDb();
export const db: any = new Proxy({}, {
get(_target, prop) {
return (activeDb as any)?.[prop as keyof typeof activeDb];
},
});
export { schema };
export async function closeDbConnections(): Promise<void> {
@@ -607,3 +613,26 @@ export async function closeDbConnections(): Promise<void> {
sqliteConnection = null;
}
}
export async function switchRuntimeDatabase(nextDialect: RuntimeDbDialect, nextDbUrl: string): Promise<void> {
const previousDialect = runtimeDbDialect;
const previousDbUrl = config.dbUrl;
const previousConfigDialect = config.dbType;
await closeDbConnections();
runtimeDbDialect = nextDialect;
config.dbType = nextDialect;
config.dbUrl = nextDbUrl;
try {
activeDb = initDb();
} catch (error) {
await closeDbConnections();
runtimeDbDialect = previousDialect;
config.dbType = previousConfigDialect;
config.dbUrl = previousDbUrl;
activeDb = initDb();
throw error;
}
}