forked from exemplar-codes/mvc-basics-exploration-expressjs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
93 lines (76 loc) · 2.99 KB
/
Copy pathapp.js
File metadata and controls
93 lines (76 loc) · 2.99 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
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
84
85
86
87
88
89
90
91
92
93
const path = require("path");
const sequelize = require("./util/database");
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
const adminRoutes = require("./routes/admin");
const shopRoutes = require("./routes/shop");
const errorController = require("./controllers/error");
const Product = require("./models/Product");
const User = require("./models/User");
const Cart = require("./models/Cart");
const CartItem = require("./models/CartItem");
const Order = require("./models/Order");
const OrderItem = require("./models/OrderItem");
// app.set('view engine', 'pug');
// app.set('views', 'views'); // not needed for this case, actually
app.set("view engine", "ejs");
app.set("views", "views"); // not needed for this case, actually
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "public")));
app.use(cors());
// mock authentication, i.e. get user who's making the request
app.use(async (req, res, next) => {
req.user = await User.findByPk(1);
next();
});
app.get("/try", async (req, res, next) => {
await new Promise((r) => setTimeout(r, 1000));
return res.json({ time: new Date().toLocaleTimeString() });
});
app.use("/admin", adminRoutes);
app.use(shopRoutes);
app.use(errorController.get404);
// for the admin user, 1-N
User.hasMany(Product);
Product.belongsTo(User, { onDelete: "CASCADE" }); // syntax: talks about onDelete of target.
// What it does here? Delete all products related to a user when the user is deleted
// adding cart model, 1-1
User.hasOne(Cart);
Cart.belongsTo(User);
// extra stuff, for ease of 'joined' pages
// 1-
// CartItem.hasOne(Product);
// Product.belongsTo(CartItem); // no, doesn't make sense, OMIT
/*
Also, Sequelize does not raise an error if model associations are inconsistent/nonsense.
- It just creates all the tables, with links missing.
- no errors, but 'afterBulkSync' does not run, which implies that Sequelize is aware of the unsuccessful "sync". So, why no errors reported? strange.
*/
// N-M
// Cart.hasMany(Product, { through: CartItem }); // correct, but Sequelize has weird notation, it forces `belongsToMany` on both sides.
Cart.belongsToMany(Product, { through: CartItem });
Product.belongsToMany(Cart, { through: CartItem });
// For the magic methods, since they are absent.
// Note: redundant from an SQL POV, since all FKs, indexes were added above.
Cart.hasMany(CartItem);
CartItem.belongsTo(Cart);
// adding to avoid wasting time on query method quirks
Product.hasMany(CartItem);
CartItem.belongsTo(Product);
// duplicating the Cart associations - for Order and OrderItem
User.hasMany(Order);
Order.belongsTo(User);
Order.belongsToMany(Product, { through: OrderItem });
Product.belongsToMany(Order, { through: OrderItem });
Order.hasMany(OrderItem);
OrderItem.belongsTo(Order);
Product.hasMany(OrderItem);
OrderItem.belongsTo(Product);
sequelize
.sync()
.then(() => {
app.listen(process.env.PORT || 3000);
})
.catch(console.log);