-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
35 lines (31 loc) · 949 Bytes
/
db.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
const Sequelize = require('sequelize');
const fs = require('fs');
const path = require('path');
let db = null;
module.exports = app => {
if (!db) {
const sequelize = new Sequelize(
app.libs.config.database,
app.libs.config.username,
app.libs.config.password,
app.libs.config.params
);
db = {
sequelize,
Sequelize,
models: {}
};
const dir = path.join(__dirname, 'models');
fs.readdirSync(dir).forEach( file => {
const modelDir = path.join(dir, file);
const model = sequelize.import(modelDir);
db.models[model.name] = model;
});
Object.keys(db.models).forEach( key => {
if (db.models[key].hasOwnProperty('associate')) {
db.models[key].associate(db.models);
}
});
}
return db;
}