2019-11-21 16:35:56 +00:00
|
|
|
import { createConnection, Connection } from "mysql";
|
|
|
|
import { conf } from "./ConfigHelper";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Database helper
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
2019-11-22 08:31:13 +00:00
|
|
|
export interface QueryInformation {
|
|
|
|
table: string,
|
2019-11-23 12:16:28 +00:00
|
|
|
fields ?: Array<String>,
|
2019-11-22 08:31:13 +00:00
|
|
|
where ?: Object,
|
|
|
|
limit ?: number,
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:35:56 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-22 08:31:13 +00:00
|
|
|
/**
|
|
|
|
* Query the database (SELECT)
|
|
|
|
*
|
|
|
|
* @param info Information about the query
|
|
|
|
*/
|
|
|
|
static async Query(info: QueryInformation) : Promise<Array<any>> {
|
|
|
|
// Prepare SQL request
|
2019-11-23 12:16:28 +00:00
|
|
|
let request = "SELECT ";
|
|
|
|
|
|
|
|
// Requested fields
|
|
|
|
request += info.fields ? info.fields.join(",") : "*";
|
|
|
|
|
|
|
|
request += " FROM " + info.table;
|
2019-11-22 08:31:13 +00:00
|
|
|
let args = [];
|
|
|
|
|
|
|
|
// Add where arguments
|
|
|
|
if(info.where) {
|
|
|
|
request += " WHERE ";
|
|
|
|
|
|
|
|
for(const k in info.where) {
|
|
|
|
if(!info.where.hasOwnProperty(k))
|
|
|
|
continue;
|
2019-11-23 12:16:28 +00:00
|
|
|
const v = info.where[k].toString();
|
2019-11-22 08:31:13 +00:00
|
|
|
|
|
|
|
request += k;
|
|
|
|
request += v.startsWith("%") || v.endsWith("%") ? " LIKE " : " = "
|
|
|
|
request += "? AND "
|
|
|
|
|
|
|
|
args.push(v);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Remove the last (useless) AND
|
|
|
|
request = request.substr(0, request.length - 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit (if any)
|
|
|
|
if(info.limit)
|
|
|
|
request += " LIMIT " + info.limit;
|
|
|
|
|
|
|
|
|
|
|
|
// Execute request
|
|
|
|
return await new Promise((resolve, reject) => {
|
|
|
|
this.connection.query( request, args, (err, result, fields) => {
|
|
|
|
if(err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query a single row on the database
|
|
|
|
*
|
|
|
|
* @param info Information about the request
|
|
|
|
* @returns First matching row / null if none found
|
|
|
|
*/
|
|
|
|
static async QueryRow(info : QueryInformation) : Promise<any | null> {
|
|
|
|
info.limit = 1;
|
|
|
|
const result = await this.Query(info);
|
|
|
|
|
|
|
|
if(result.length == 0)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return result[0];
|
|
|
|
}
|
2019-11-23 12:16:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert an new entry into the database
|
|
|
|
*
|
|
|
|
* @param info Information about the entry
|
|
|
|
* @returns The ID of the inserted column (if any)
|
|
|
|
*/
|
|
|
|
static async InsertRow(table : string, values : any) : Promise<number> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.connection.query("INSERT INTO " + table + " SET ?", values, (err, results, fields) => {
|
|
|
|
if(err)
|
|
|
|
reject(err);
|
|
|
|
|
|
|
|
resolve(results.insertId);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-11-21 16:35:56 +00:00
|
|
|
}
|