-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
57 lines (46 loc) · 1.35 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
const fs = require('fs');
const path = require('path');
const { Sequelize, DataTypes } = require('sequelize');
const logger = require('../helpers/logger');
const filebasename = path.basename(__filename);
const db = {};
const config = {
host: 'localhost',
dialect: 'sqlite',
logging: function (msg) {
logger.debug(msg)
},
// SQLite only
storage: './db/database.sqlite'
}
const sequelize = new Sequelize(config);
fs
.readdirSync(__dirname + "/models")
.filter((file) => {
const returnFile = (file.indexOf('.') !== 0)
&& (file !== filebasename)
&& (file.slice(-3) === '.js');
return returnFile;
})
.forEach((file) => {
const model = require(path.join(__dirname, "/models", file))(sequelize, DataTypes)
db[model.name] = model;
});
Object.keys(db).forEach((modelName) => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
const sequelizeOptions = { logging: console.log, };
// Removes all tables and recreates them (only available if env is not in production)
// if (process.env.ENV !== 'production') {
// sequelizeOptions.force = true;
// }
sequelize.sync(sequelizeOptions)
.catch((err) => {
console.log(err);
process.exit();
});
module.exports = db;