ionian/packages/server/src/database.ts

27 lines
604 B
TypeScript
Raw Normal View History

2024-03-29 00:19:36 +09:00
import { existsSync } from "node:fs";
2024-03-26 23:58:26 +09:00
import { get_setting } from "./SettingConfig";
2024-03-29 00:19:36 +09:00
import { getKysely } from "./db/kysely";
2024-03-26 23:58:26 +09:00
export async function connectDB() {
2024-03-29 00:19:36 +09:00
const kysely = getKysely();
2024-03-26 23:58:26 +09:00
let tries = 0;
for (;;) {
try {
console.log("try to connect db");
2024-03-29 00:19:36 +09:00
await kysely.selectNoFrom(eb=> eb.val(1).as("dummy")).execute();
2024-03-26 23:58:26 +09:00
console.log("connect success");
} catch (err) {
if (tries < 3) {
tries++;
console.error(`connection fail ${err} retry...`);
2024-03-29 00:19:36 +09:00
await new Promise((resolve) => setTimeout(resolve, 1000));
2024-03-26 23:58:26 +09:00
continue;
}
2024-03-29 00:19:36 +09:00
throw err;
2024-03-26 23:58:26 +09:00
}
break;
}
2024-03-29 00:19:36 +09:00
return kysely;
2024-03-26 23:58:26 +09:00
}