-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (28 loc) · 1.04 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
const Sequelize = require('sequelize')
const defineModels = require('./models')
module.exports = (config = {}) => {
let options = Object.assign({}, {
define : {
timestamps : false,
freezeTableName : true
},
logging: console.log
}, config.options)
/* http://docs.sequelizejs.com/en/v3/docs/getting-started/#application-wide-model-options
is only usable with the string connection form. It's most certainly a bug on sequelize.
*/
let connexion = new Sequelize(
`mysql://${config.user}:${config.password}@${config.options.host}:3306/${config.dbname}`,
options
)
defineModels(connexion)
Object.keys(connexion.models).forEach((modelName) => {
let model = connexion.models[modelName]
const hasAssociate = model.associate !== undefined
const associateIsFunction = model.associate instanceof Function
if (hasAssociate && associateIsFunction) {
model.associate(connexion.models)
}
})
return connexion
}