Sequelize
First install sequelize
install mysql2 before it
Creating Database rules for seuqlize
Now define Model
Now for syncing models into database and creating table for them we need to use sync function
First import sequelize
Next use sequelize sync function
Now create a function to create a product using Product model
Now if we go to add product page we can than create product using the above function
Get all Products without any limitation using findAll() method
Get Single Product
We are using findById but now it has been replaced findByPk(). Also unlike the prev way where we are not getting array of product and only single product so we are replacing the array boilerplate.
We have also option of finding multiple product option. But one issue is for fetching single product is that we still have to use array as the products are fetched as array.
Updating Edit Product Page
1)First check if it is edit mode and next get product like before
2) All Values getting populate
3) Now go to edit products
a) use pkgbyId
b) add updated content
c) use product.save() . If there is no product than it will create new product or it will update existing product.
d) Now instead of using then with product.save() we simply return it to avoid chaining of nested callback
Now if we reload the edit product page than we will get the update change.
However there is a gotcha.....
This controller will not wait for it to finish and directly go to res.redirect since it is an async function and so to avoid it we add it inside then
Hiwveer this is not a good thing as it will not go to next page wehnerv some error occur.It will be explained in next section on how to fix it.
DELETING PRODUCT
Create User Model
Adding a one to many relationship
Now Product.belongsTo means the product are associated a User. In second option we have onDelete where it meanse that Products will delete on deleting of user.
More info can be learnt by knowing more about SQL
Also User.hasMany(Product) means that
We also using force as true in sync so as to delete all table and again create them as we haved added relation in this new setup
Now after running the server again we have userid field in product as well which is automatically created.
CREATING AND MANAGING A NEW USER
Create a new user if User don't exist else return the user and log the user.
Also, both created user and returned user need to be same but since we know it is same we arer simply returning user else we need to do like this Promise.resolve(user)
Learn more about Promise.resolve()
We also add a middleware so that for incoming request it has an user
Also, since it will first run the server and only reuqest will occure after it so it is guranteed that the middleware will always have the user. npm start only will run seuelize and register this middleware but not execute the middleware.
req.user here is not a simple js object but a sequelize object so it will have all method like destroy ect
Here again userId is not a simple object but sequeslize object containing both data as well as helper fn
Now let's take about the magic part. Coz of sequelize we can automatically create assocaite product. When we create product and add the relation it automatically added createProduct() for our req.user.
You can learn more by reading the doc.
Get products mathcing id of a particular user
Cart Mode & Cart Item Model
Now lets create the associations
User.hasMany & hasOne -> only one is enough
CartItem is the one where relation is stored. So we have cartId as well as ProductID
Now lets fetch cart data
Let's see what we get when we use getCart() . We don'te get null as we don't have any cart. So,lets create cart in app.js
Creating cart in sequlize function of app.js
Upon running the server we no get an cart associated with ID 1
Now lets use getProducts using magic function
However, weill still not get the products we need to do extra stuff
b
Comments
Post a Comment