Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

Is this Sequelize Characteristic True?

The following code works:

exports.postCart = (req, res, next) => {
  const prodId = req.body.productId;
  let fetchedCart;
  let newQuantity = 1;
  req.user
    .getCart()
    .then(cart => {
      fetchedCart = cart;
      return cart.getProducts({ where: { id: prodId } });
    })
    .then(stuff => {
      let product;
      if (stuff.length > 0) {
        product = stuff[0];
      }
      if (product) {
        const oldQuantity = product.cartItem.quantity;
        newQuantity = oldQuantity + 1;
        return product;
      }
      return Product.findByPk(prodId);
    })
    .then(product => {
      return fetchedCart.addProduct(product, {
        through: { quantity: newQuantity }
      });
    })
    .then(() => {
      res.redirect("/cart");
    });
};

Open in new window


This is Sequelize and I believe I'm about to be completely impressed because of what is apparently in place with "fetchedCart.addProduct."

It would seem that Sequelize knows by virtue of the "Magic Methods" anomaly that it knows whether or not to add a new row or simply update an existing one.

Is that true? And if so, where is there some documentation that elaborates on that?

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

Hey, Julian!

From what I can determine from the tutorial:

"addProduct" is a Magic Method that is initiated when I established a relationship between the Cart and the Product table -at least, that's what I'm understanding based on the tutorial's content and the info you'll find here: http://docs.sequelizejs.com/manual/associations.html (look under the "Belongs to Many" Associations.

It says:

This will add methods getUsers, setUsers, addUser,addUsers to Project, and getProjects, setProjects, addProject, and addProjects to User.

You may have already answered my question and I'm not aware of it, but...

 Bottom line: Do the aforementioned methods that get added automatically include what appears to be a necessary dynamic in the code that I've documented where if there's an already existing row in the "cart" table, it will do an "update" rather than an "insert?"

When I look at my console, I can see that very thing happen, but I wanted to see it documented somewhere so I know I'm not overlooking something else about my code.

What do you think?
Here are my relationships:

Cart.belongsToMany(Product, { through: CartItem });
Product.belongsToMany(Cart, { through: CartItem });
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's alright Julian! I was able to find some more info in subsequent lectures of the tutorial.

I was also able to determine that some of the methods I was being distracted by were not part of the routes in question, so I think I've got a better handle on it.

Thanks!
You are welcome.