-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdb.js
More file actions
40 lines (31 loc) · 719 Bytes
/
Copy pathdb.js
File metadata and controls
40 lines (31 loc) · 719 Bytes
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
import Sequelize from 'sequelize';
import fs from 'fs';
import path from 'path';
let db = null;
module.exports = (app) => {
if (db !== null) return db;
const config = app.config;
const sequelize = new Sequelize(
config.database,
config.username,
config.password,
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);
if (model) {
db.models[model.name] = model;
}
});
Object.keys(db.models).forEach(key => {
db.models[key].associate(db.models);
});
return db;
};