3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 04:25:01 +00:00
zeppelin/backend/src/data/db.ts

23 lines
686 B
TypeScript

import { SimpleError } from "../SimpleError";
import { dataSource } from "./dataSource";
let connectionPromise: Promise<void>;
export function connect() {
if (!connectionPromise) {
connectionPromise = dataSource.initialize().then(async (initializedDataSource) => {
const tzResult = await initializedDataSource.query("SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP) AS tz");
if (tzResult[0].tz !== "00:00:00") {
throw new SimpleError(`Database timezone must be UTC (detected ${tzResult[0].tz})`);
}
});
}
return connectionPromise;
}
export function disconnect() {
if (connectionPromise) {
connectionPromise.then(() => dataSource.destroy());
}
}