mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-12-01 17:56:28 +00:00
41 lines
755 B
TypeScript
41 lines
755 B
TypeScript
|
import { createConnection, Connection } from "mysql";
|
||
|
import { conf } from "./ConfigHelper";
|
||
|
|
||
|
/**
|
||
|
* Database helper
|
||
|
*
|
||
|
* @author Pierre HUBERT
|
||
|
*/
|
||
|
|
||
|
export class DatabaseHelper {
|
||
|
|
||
|
private static connection : Connection;
|
||
|
|
||
|
/**
|
||
|
* Connect to database
|
||
|
*/
|
||
|
static async connect() {
|
||
|
this.connection = createConnection({
|
||
|
host: conf().database.host,
|
||
|
user: conf().database.user,
|
||
|
password: conf().database.password,
|
||
|
database: conf().database.dbName
|
||
|
});
|
||
|
|
||
|
await new Promise((resolve, reject) => {
|
||
|
this.connection.connect(err => {
|
||
|
|
||
|
if(err) {
|
||
|
console.error("Could not connect to database !");
|
||
|
console.error(err);
|
||
|
reject(err);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
console.info("Connected to database.");
|
||
|
resolve();
|
||
|
});
|
||
|
})
|
||
|
}
|
||
|
|
||
|
}
|