1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2025-06-20 08:35:17 +00:00

Add connection to MySQL database

This commit is contained in:
2019-11-21 17:35:56 +01:00
parent 9d8f40be9a
commit 15b7b9ac1e
4 changed files with 85 additions and 16 deletions

View File

@ -0,0 +1,41 @@
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();
});
})
}
}

View File

@ -1,4 +1,5 @@
import { ConfigurationHelper, conf } from "./helpers/ConfigHelper";
import { ConfigurationHelper } from "./helpers/ConfigHelper";
import { DatabaseHelper } from "./helpers/DatabaseHelper";
/**
* Main project script
@ -9,4 +10,7 @@ import { ConfigurationHelper, conf } from "./helpers/ConfigHelper";
console.info("Comunic API v6\t@author Pierre HUBERT\t2019-" + new Date().getFullYear());
console.info("Load configuration...");
ConfigurationHelper.loadConf("config.json");
ConfigurationHelper.loadConf("config.json");
console.info("Connect to database");
DatabaseHelper.connect();