-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.js
39 lines (32 loc) · 901 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
36
37
38
39
import fs from 'fs';
import path from 'path';
import Sequelize from 'sequelize';
let db=null;
module.exports= app => {
if (!db)
{
const config=app.libs.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);
db.models[model.name]=model;
});
Object.keys(db.models).forEach(key => {
console.log("debbuging " + key + " associate" + JSON.stringify(db.models));
db.models[key].options.classMethods.associate(db.models);
});
}
return db;
};