-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_sequelize.js
83 lines (70 loc) · 2.54 KB
/
app_sequelize.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const errorHandlerController = require('./controllers/errorHandlers')
const adminRoutes = require('./routes/admin');
const shopRoutes = require('./routes/shop');
const sequelize = require('./utils/db');
const ProductsModel = require('./models/product');
const UserModel = require('./models/user');
const CartModel = require('./models/cart');
const CartItemModel = require('./models/cart-item');
const OrderModel = require('./models/order');
const OrderItemModel = require('./models/order-item');
let PORT = 4000;
const app = express();
//for ejs
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static(path.join(__dirname,'public')));
app.use((req,res,next)=>{
UserModel.findByPk(1)
.then(user=>{
req.user = user;
next();
})
.catch(err=>console.log(err));
})
//you can add a filter to this base url
app.use('/admin',adminRoutes);
app.use(shopRoutes);
app.use(errorHandlerController.get404);
async function connectToDataBase(){
ProductsModel.belongsTo(UserModel,{constraints:true, onDelete:'CASCADE' });
UserModel.hasMany(ProductsModel);
UserModel.hasOne(CartModel);
CartModel.belongsTo(UserModel); //optional the above one direction is enough
CartModel.belongsToMany(ProductsModel,{through:CartItemModel});
ProductsModel.belongsToMany(CartModel,{through:CartItemModel});
OrderModel.belongsTo(UserModel);
UserModel.hasMany(OrderModel);
// ProductsModel.belongsToMany(OrderModel,{through:OrderItemModel});
OrderModel.belongsToMany(ProductsModel,{through:OrderItemModel})
try {
await sequelize.authenticate();
console.log('Connection has been established successfully.');
// sequelize.sync({force:true}) // on load the app drops existing tables and create a new one
sequelize.sync()
.then(result=>{
return UserModel.findByPk(1);
})
.then(user=>{
if(!user){
return UserModel.create({
name:"Miles",
email:"[email protected]"
});
}
return user;
})
.then(user=>{
// console.log(user);
user.createCart();
app.listen(PORT,()=>`server running on port ${PORT}`);
})
.catch(err=>console.log(err));
} catch (error) {
console.error('Unable to connect to the database:', error);
}
}
connectToDataBase();