-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (52 loc) · 1.65 KB
/
index.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
54
55
56
57
58
59
60
61
const fs = require('fs');
const path = require('path');
const Foola = require('foola');
const Sequelize = require('sequelize');
class FoollSeq extends Foola {
constructor(dirname, server) {
super(dirname, server);
this.loadModels();
}
loadDBConfig() {
var rcPath = path.resolve('.sequelizerc');
var rc = require(rcPath);
var configText = fs.readFileSync(rc.config);
var config = JSON.parse(configText);
return config[this.server.env];
}
loadModels() {
var db = this.loadDBConfig();
var sequelize = new Sequelize(db.database, db.username, db.password, db);
var models = {};
var modelsFolderPath = path.resolve(this.dirname, 'models');
if (fs.existsSync(modelsFolderPath)) {
var modelsDirFiles = fs.readdirSync(modelsFolderPath);
modelsDirFiles.forEach(function (modelBase) {
try {
var modelPath = path.resolve(modelsFolderPath, modelBase);
var model = sequelize.import(modelPath);
var modelName = path.parse(modelBase).name;
models[modelName] = model;
} catch (error) {
console.error(error);
}
});
this.models = models;
}
}
static coordinateDB(server) {
var db = {};
for (var moduleName in server.modules) {
var appModule = server.modules[moduleName];
Object.assign(db, appModule.models);
}
for (var moduleName in server.modules) {
for (var modelName in server.modules[moduleName].models) {
if (server.modules[moduleName].models[modelName].associate) {
server.modules[moduleName].models[modelName].associate(db);
}
}
}
}
}
module.exports = FoollSeq;