-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.js
57 lines (45 loc) · 1.09 KB
/
model.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 Sequelize = require('sequelize');
const dbName = 'ganti_dengan_nama_databasemu';
const dbUser = 'ganti_dengan_user_databasemu';
const dbPassword = 'ganti_dengan_password_databasemu';
const sequelize = new Sequelize(dbName, dbUser, dbPassword, {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
operatorsAliases: false
});
const Product = sequelize.define('product', {
name: Sequelize.STRING,
price: Sequelize.INTEGER,
description: Sequelize.TEXT,
}, {
timestamps: false
})
const Cart = sequelize.define('cart', {
code: Sequelize.STRING,
}, {
tableName: 'carts',
timestamps: false,
});
const CartItem = sequelize.define('cartItems', {
cart_id: Sequelize.INTEGER,
productId: Sequelize.INTEGER,
qty: Sequelize.INTEGER,
}, {
tableName: 'cart_items',
timestamps: false
});
Cart.hasMany(CartItem, { foreignKey: 'cart_id' })
CartItem.belongsTo(Cart, { foreignKey: 'cart_id' })
CartItem.belongsTo(Product, { foreignKey: 'productId' })
Product.hasMany(CartItem)
module.exports = {
Product,
Cart,
CartItem,
}