redid database structures and now running SQLite and Seqelize

This commit is contained in:
plane000
2018-08-27 17:53:45 +01:00
parent c11aab31a0
commit 1ac8ea9b96
12 changed files with 899 additions and 251 deletions

View File

@@ -1,41 +1,29 @@
import mysql from 'mysql';
import Sequelize from 'sequelize';
import {BaseDatabase} from './baseDatabase';
import {Logger} from '../logger';
import {Config} from '../../config/config';
let connection;
export class Database extends BaseDatabase {
static async exec(query) {
let connection = BaseDatabase.Connection;
let res;
export class Database {
static get Connection() {return connection}
static async connect() {
Logger.info('Connecting to mySQL database');
let promise = new Promise((resolve, reject) => {
connection = mysql.createConnection(Config.Database);
connection.connect((err) => {
if (err) {
Logger.panic('Failed to connect to the database as user ' + Config.Database.user);
reject();
}
Logger.info('Connected to mySQL as id ' + connection.threadId);
resolve();
});
connection
.query(query)
.then(result => {
res = result[0][0].result;
resolve();
})
.catch(err => {
Logger.error('An error occured while querying a database: ' + err);
reject()
});
});
await promise;
}
static async testConnection() {
let promise = new Promise((resolve, reject) => {
connection.query('SELECT 1 + 1 AS solution', async function (err, results, fields) {
if (err) {
Logger.panic('Failed to query the database');
reject();
}
Logger.info('Database connection tested and secure');
resolve();
});
});
await promise;
return res;
}
}
Database.users = require('./users').UserTools;