2016-10-30 14:01:21 +00:00
|
|
|
var util = require("util");
|
|
|
|
var events = require("events");
|
|
|
|
var Sequelize = require("sequelize");
|
2017-02-09 19:39:42 +00:00
|
|
|
var debug = require("debug");
|
2016-10-30 14:01:21 +00:00
|
|
|
|
|
|
|
var utils = require("../utils");
|
|
|
|
|
|
|
|
class Database extends events.EventEmitter {
|
|
|
|
|
2017-02-22 15:25:46 +00:00
|
|
|
constructor(dbConfig) {
|
2016-10-30 14:01:21 +00:00
|
|
|
super();
|
|
|
|
|
2017-02-22 15:25:46 +00:00
|
|
|
this._conn = new Sequelize(dbConfig.database, dbConfig.user, dbConfig.password, {
|
|
|
|
dialect: dbConfig.type,
|
|
|
|
host: dbConfig.host,
|
|
|
|
port: dbConfig.port,
|
2016-10-30 14:01:21 +00:00
|
|
|
define: {
|
|
|
|
timestamps: false
|
2017-02-09 19:39:42 +00:00
|
|
|
},
|
|
|
|
logging: debug.enabled("sql") ? console.log : false
|
2016-10-30 14:01:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
for(let func of this._init)
|
|
|
|
func.call(this);
|
|
|
|
|
|
|
|
for(let func of this._afterInit)
|
|
|
|
func.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(force) {
|
|
|
|
return this._conn.authenticate().then(() => {
|
|
|
|
return this._conn.sync({ force: !!force });
|
|
|
|
}).then(() => {
|
2017-04-27 17:31:57 +00:00
|
|
|
return this._runMigrations()
|
2016-10-30 14:01:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Database.prototype._init = [ ];
|
|
|
|
Database.prototype._afterInit = [ ];
|
|
|
|
|
|
|
|
require("./migrations")(Database);
|
|
|
|
require("./helpers")(Database);
|
|
|
|
|
|
|
|
require("./pad")(Database);
|
|
|
|
require("./marker")(Database);
|
|
|
|
require("./line")(Database);
|
|
|
|
require("./view")(Database);
|
|
|
|
require("./type")(Database);
|
2016-10-31 12:11:36 +00:00
|
|
|
require("./history")(Database);
|
2017-02-25 12:46:37 +00:00
|
|
|
require("./meta")(Database);
|
2017-05-17 17:00:15 +00:00
|
|
|
require("./route")(Database);
|
2016-10-30 14:01:21 +00:00
|
|
|
|
|
|
|
module.exports = Database;
|