2018-10-26 06:41:20 +03:00
|
|
|
import { Connection, createConnection } from "typeorm";
|
2021-06-06 23:51:32 +02:00
|
|
|
import { SimpleError } from "../SimpleError";
|
2018-10-26 06:41:20 +03:00
|
|
|
|
|
|
|
let connectionPromise: Promise<Connection>;
|
|
|
|
|
|
|
|
export let connection: Connection;
|
|
|
|
|
|
|
|
export function connect() {
|
|
|
|
if (!connectionPromise) {
|
2021-09-11 19:06:51 +03:00
|
|
|
connectionPromise = createConnection().then((newConnection) => {
|
2019-05-07 19:54:16 +03:00
|
|
|
// Verify the DB timezone is set to UTC
|
2021-09-11 19:06:51 +03:00
|
|
|
return newConnection.query("SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP) AS tz").then((r) => {
|
2020-05-13 07:56:39 +03:00
|
|
|
if (r[0].tz !== "00:00:00") {
|
2018-10-26 06:41:20 +03:00
|
|
|
throw new SimpleError(`Database timezone must be UTC (detected ${r[0].tz})`);
|
|
|
|
}
|
|
|
|
|
|
|
|
connection = newConnection;
|
|
|
|
return newConnection;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return connectionPromise;
|
|
|
|
}
|