69 lines
1.6 KiB
JavaScript
69 lines
1.6 KiB
JavaScript
'use strict';
|
|
const apiConfig = require( '../../../config/apiConfig.json' );
|
|
const Knex = require('knex');
|
|
const { Model } = require('objection');
|
|
|
|
const UNINIT = 0;
|
|
const INIT = 1;
|
|
const ONLINE = 2;
|
|
const OFFLINE = 3;
|
|
|
|
class SystemServices {
|
|
constructor(){
|
|
this.SystemServiceState = UNINIT;
|
|
}
|
|
|
|
async setup(){
|
|
this.SystemServiceState = UNINIT;
|
|
}
|
|
|
|
async init(){
|
|
this.SystemServiceState = INIT;
|
|
}
|
|
|
|
async connect(){
|
|
const knex = Knex({
|
|
client: 'mysql',
|
|
useNullAsDefault: true,
|
|
connection: {
|
|
host: apiConfig.sql.host,
|
|
port: apiConfig.sql.port,
|
|
user: apiConfig.sql.user,
|
|
password: apiConfig.sql.password,
|
|
database: apiConfig.sql.database,
|
|
}
|
|
});
|
|
Model.knex(knex);
|
|
this.knex = knex;
|
|
console.log("Connected to SQL");
|
|
this.SystemServiceState = ONLINE;
|
|
}
|
|
|
|
async disconnect(){
|
|
this.knex.destroy();
|
|
|
|
this.SystemServiceState = OFFLINE;
|
|
}
|
|
|
|
async deinit(){
|
|
this.SystemServiceState = UNINIT;
|
|
}
|
|
|
|
async getState(){
|
|
switch( this.SystemServiceState ){
|
|
case UNINIT:
|
|
return "UNINIT";
|
|
case INIT:
|
|
return "INIT";
|
|
case ONLINE:
|
|
return "ONLINE";
|
|
case OFFLINE:
|
|
return "OFFLINE";
|
|
default:
|
|
return "UNINIT";
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = new SystemServices();
|