-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbModelLoader.js
53 lines (46 loc) · 1.31 KB
/
dbModelLoader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/** dbModelLoader.js
@def: Creates db connection, and loads all sequilize models in memory
@TODO:
-
*/
"use strict";
var fs = require("fs");
var path = require("path");
// NOTE: it's global
var Sequelize = require("sequelize");
var sequelize = new Sequelize(
CONFIG.Mysql.database,
CONFIG.Mysql.username,
CONFIG.Mysql.password, {
host: CONFIG.Mysql.host,
dialect: 'mysql',
pool: {
max: (CONFIG.Mysql.pool || {}).max || 5,
min: (CONFIG.Mysql.pool || {}).min || 0,
idle: (CONFIG.Mysql.pool || {}).idle || 10000
},
logging: (str) => {
console.log('SEQUALIZE: ', str, '\n\n');
}
}
);
var db = {};
fs
.readdirSync(ARGV.DIR.HOME + "/db_models")
.filter((file) => {
return (file.indexOf(".") !== 0) && (file !== "dbModelLoader.js");
})
.forEach((file) => {
let model = sequelize.import(path.join(ARGV.DIR.HOME + "/db_models", file));
let name = model.name;
name = String.fromCharCode((name.charCodeAt(0) - 32)) + name.substr(1);
db[name] = model;
});
Object.keys(db).forEach((modelName) => {
if ("associate" in db[modelName]) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.DataTypes = Sequelize;
module.exports = db;