db.js
1.23 KB
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
const Sequelize = require('sequelize');
// Models
//const ProductFields = require('../app/models/product-fields');
let db = {
init: function (dbOptions) {
const options = dbOptions || {};
// Connect to db
const sequelize = new Sequelize(options.name, options.user, options.password, {
host: options.host,
dialect: 'mysql',
pool: {
max: 1,
min: 0,
idle: 10000
}
});
db.sequelize = sequelize;
db.models = {
//'ProductFields': sequelize.define(ProductFields.name, ProductFields.model.schema, ProductFields.model.options)
};
/** Associations and Relations **/
//db.models.Broker.hasMany(db.models.Product);
//db.models.Product.belongsTo(db.models.InsuranceCompany);
// Join tables
/*db.models.Customer.belongsToMany(db.models.Product, {
through: {
model: db.models.Order,
unique: false
}
});
db.models.Product.belongsToMany(db.models.Customer, {
through: {
model: db.models.Order,
unique: false
}
});*/
// Sync models and config to db
sequelize.sync().then(function () {
console.log('Tables have been synced');
return db;
});
}
};
module.exports = db;