-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
47 lines (39 loc) · 1.18 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
const rread = require('readdir-recursive'),
path = require('path'),
Sequelize = require('sequelize'),
sync = require('./sync');
module.exports = function (options) {
let basename = path.basename(module.filename + '/model');
let sequelize = new Sequelize(options.name, options.user, options.pass,{
host: options.host,
dialect: options.driver,
port: options.port,
define: {
createdAt: 'created_date',
updatedAt: 'updated_date',
underscored: true,
timestamps: false
},
logging: false
});
let db = {};
rread
.fileSync(__dirname + '/model')
.filter(file => {
file = path.basename(file);
return (file.slice(-3) === '.js') && (file !== basename);
})
.forEach(file => {
let model = sequelize['import'](file);
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sync = sync.bind(this,options);
db.sequelize = sequelize;
db.Sequelize = Sequelize;
return db;
};