1

I am building an API and came across an issue that I have a few ideas of how to solve, but I was wondering what is the most optimal one. The issue is the following:

I have a Product model which has, for the sake of simplicity one field called totalValue.

I have another model called InventoryItems, which, whenever is updated, the totalValue of Product must also be updated.

For example, if the current totalValue of a product is say $1000, when someone purchases 10 screws at a cost of $1 each, a new InventoryItem record will be created:

InventoryItem: {
  costPerItem: 1,
  quantity: 10,
  relatedToProduct: "ProductXYZ"
}

At the same time of creation of that item, totalValue of the respective ProductXYZ must be updated to now $1100.

The question is what is the most efficient and user-friendly way to do this?

Two ways come to my mind (and keep in mind that the code bellow is kinda pseudo, I have intentionally omitted parts of it, that are irrelevant for the problem at hand):

  1. When the new InventoryItem is created, it also queries the database for the product and updates it, so both things happen in the same function that creates the inventory item:

    function async createInventoryItem(req, res) {
      const item = { ...req.body };
    
      const newInventoryItem = await new InventoryItem({...item}).save();
    
      const foundProduct = await Product.find({ name: item.relatedtoProduct }).exec();
    
      foundProduct.totalValue = foundProduct.totalValue + item.costPerItem * item.quantity;
    
      foundProduct.save();
    
      res.json({ newInventoryItem, newTotalOfProduct: foundProduct.totalValue });
    }
    

That would work, my problem with that is that I will no longer have "a single source of truth" as that approach will make it hard to update the code, as updating a given Product will be scattered all over the project.

  1. The second approach that comes to my mind is that, when I receive the request to create the item, I do create the item, and then I make an internal request to the other endpoint that handles product updates, something like:

    function async createInventoryItem(req, res) {
      const item = { ...req.body };  
    
      const newInventoryItem = await new InventoryItem({...item}).save();
    
      const totalCostOfNewInventoryItem = item.costPerItem * item.quantity;
    
      // THIS is the part that I don't know how to do
      const putResponse = putrequest("/api/product/update", { 
        product: item.relatedtoProduct, 
        addToTotalValue: totalCostOfNewInventoryItem 
      });
    
      res.json({ newInventoryItem, newTotalOfProduct: putResponse.totalValue });
    }
    

This second approach solves the problem of the first approach, but I don't know how to implement it, and it is I'm guessing a form of requests chaining or rerouting? Also I am guessing that the second approach will not have a performance penalty, since node will be sending requests to itself, so no time lost in accessing servers across the world or whatever)

I am pretty sure that the second approach is the one that I have to take (or is there another way that I am currently not aware of??? I am open to any suggestions, I am aiming for performance), but I am unsure of exactly how to implement it.

Darkbound
  • 3,026
  • 7
  • 34
  • 72
  • Found the answer: https://stackoverflow.com/questions/38946943/calling-express-route-internally-from-inside-nodejs – Darkbound Jan 09 '21 at 19:51

0 Answers0