What is the best way to determine total revenue by consolidating data from various tables within an IndexedDB database?

Seeking guidance on building a stock/sales application in JavaScript with Dexie.js. I need assistance in efficiently calculating the Total Sales amount without resorting to overly complicated recursive code that triggers multiple queries for a single product's sales total.

Here is an overview of my schema:

clients: "++id, name, phone",
order: "++id, clientId, date",
order_content: "orderId, productId, qty",
product: "++id, name, mu, mk_cost, sa_cost, prod_cost",
stock: "++id, date, productId, qty, lot"

I maintain different product types in the "Product" table along with their prices and other relevant information. When an order is placed, I record the clientId in the Order table and utilize "order_content" to store the items by linking them with the orderId.

Essentially, I am looking to compute totals for each item and then aggregate those amounts.

An attempt was made to run the following code within a db.product.each() loop, but it seems like there might be a more straightforward approach:

var product1Total = 0;
function calculateTotal(productId, price){
db.order_content
.where("productId")
.equals(productId)
.each(function(item){
product1Total += (price * qty)
})
}

Your insights are greatly appreciated!

Answer №1

If you are aiming to calculate the total price for a specific order using a single query, where prod_cost represents the product cost and you are interested in the total cost for a particular order, you can achieve this by implementing something similar to the following:

function computeOrderTotal (orderId) {
    return db.order_content
        .where('orderId').equals(orderId).toArray()
    .then(orderContents => {
        return Promise.all(
            orderContents.map(oc => db.product.get(oc.productId))
        ).then (products => {
            return orderContents.reduce (
                (total, oc, i) => total + oc.qty * products[i].prod_cost, 0);
        });
    });
}

Alternatively, using async functions:

async function computeOrderTotal (orderId) {
    let orderContents = await db.order_content
        .where('orderId').equals(orderId).toArray();

    let products = await Promise.all(orderContents.map(oc =>
        db.product.get(oc.productId));

    return orderContents.reduce (
        (total, oc, i) => total + oc.qty * products[i].prod_cost, 0);
}

Or utilizing vanilla ES5 javascript:

function computeOrderTotal (orderId) {
    return db.order_content
        .where('orderId').equals(orderId).toArray()
    .then(function (orderContents) {
        return Dexie.Promise.all(
            orderContents.map(function (oc) {
                return db.product.get(oc.productId);
            })
        ).then (function (products) {
            return orderContents.reduce (
                function (total, oc, i) {
                    return total + oc.qty * products[i].prod_cost;
                }, 0);
        });
    });
}

Answer №2

There's no issue with your query, although it is recommended to wrap it in a function that returns a promise. Achieving this can be done by utilizing the promise returned from Dexie's Collection.each().

function calculateTotalAmount(productId, price) {
    var total = 0;
    return db.order_content
        .where("productID")
        .equals(productId)
        .each(function(item){
            total += (price * item.qty)
        }).then (function () {
            return total;
        });
}

Alternatively, using ES7 syntax:

async function calculateTotalPrice(productId, price) {
    var total = 0;

    await db.order_content
        .where("productID")
        .equals(productId)
        .each (item => total += (price * item.qty));

    return total;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Struggling to access values from your model using EL in JavaScript? Let me provide some guidance

In my model, there is an object named "domain" with two methods: getDescriptionEn() and getDescriptionFr(). I am trying to retrieve the appropriate description based on the current locale. My issue lies in the following code snippet: var locale = "${cur ...

The OTP submission in Phone Email's phone authentication using Node JS did not result in the reception of the token

I have implemented the “Login with Phone” Button from Phone Email on my Node JS website. The button opens a popup to enter the mobile number and then displays an OTP window after submission. Although I receive the OTP SMS and enter it successfully, I a ...

Why are the variables not reflecting the changes when an event is triggered?

I'm a beginner in programming and would really appreciate some guidance. Why are my variables not updating when I click the button?? Here is the HTML code snippet: <h1>NIM</h1> <p>Welcome to a simple edition of the game NIM</p& ...

Modify Bootstrap Card Styling Using JavaScript

When the clock strikes certain evening hours on my website, the Bootstrap card's default light style no longer fits the dark theme. I've attempted to switch the card to a dark style by tying in some JavaScript code, but it's not quite doing ...

Numeric keypad causing issues with setting minimum and maximum lengths and displaying submit button

I designed a password pin page that resembles a POS NUMPAD for users to enter their password. I am struggling to make the condition specified in the JavaScript function properly. Rule: Hide the submit button if the minimum input is less than 4 characters ...

Namespacing is not applied to dynamic modules in Vuex

I've been tackling a modular vue application that enrolls the modules during compile time. Take a look at the code snippet below - app.js import store from './vue-components/store'; var components = { erp_inventory: true, erp_purc ...

Executing JavaScript code within a Django application to load a file

Utilizing a JavaScript tool called jQuery FileTree within my Django application has presented a dilemma. This particular JavaScript requires access to a python script path, but incorporating Django template tags directly into JavaScript poses an issue. Wi ...

Can spreading be used for destructuring?

These were the initial props I attempted to pass to a component: const allprops = { mainprops:{mainprops}, // object pageid:{pageId}, // variable setpageid:{setPageId}, // state function makerefresh:{makeRefresh} // state function } <Na ...

Obtain the index by clicking on an element within an HTMLCollection

Within my HTML code, I have 9 <div> elements with the class ".square". I am looking to make these divs clickable in order to track how many times each one is clicked and store that information in an array. For example, if the fifth <div> is c ...

Issues with d3.js transition removal functionality not functioning as expected

Having an issue with a d3.js visualization that involves multiple small visualizations and a timeline. When the timeline changes, data points are supposed to be added or removed accordingly. Here is the code snippet responsible for updating: var channels ...

Ways to invoke a prop function from the setup method in Vue 3

I encountered the following code: HTML: <p @click="changeForm">Iniciar sesion</p> JS export default { name: "Register", props: { changeForm: Function, }, setup() { //How do I invoke the props change ...

Tips for How to Put a Delay on Ajax Requests and Display a Progress Bar During Loading

I am using checkboxes in the sidebar. When a user selects a checkbox from the sidebar, it displays the post normally. Is there a way to add a progress bar to delay the Ajax result? This is my Ajax code: <script> jQuery(document).ready(function($){ ...

Using the async.waterfall function in an Express application

Query: I'm encountering an issue with my express.js code where, upon running in Node.js, an empty array (ganttresult) is initially displayed. Only after refreshing the browser do I obtain the desired result. To address this problem, I have attempted ...

Could someone please provide clarification on this specific JavaScript syntax? I am unsure about the usage of `const {

Although I am not very familiar with javascript, I have come across this syntax and I would greatly appreciate it if someone could help me understand it! Regarding Node.js const { check, validationResult } = require('express-validator/check') ...

Implement Next.js deployment on NGINX server with a 403 forbidden error

I am currently utilizing Next.js for the frontend and Django for the backend. During development, everything is functioning properly. However, when transitioning to production, I am encountering a 403 Forbidden Error related to /_next/static/chunks. It app ...

Separate a single large table into two smaller tables based on the information found in the third column of every row

Looking for a Greasemonkey script that can split a single table on a page into two separate tables based on a specific column. For example, if we have the following table: <table> <tr> <td>Jill</td> <td>Smith</td ...

Dealing with AngularJS: Issue arises when attempting to inject $modal into a controller nested within a directive

Our team has implemented a custom directive that wraps around a checkbox and utilizes transclusion to inject content into it. Here is an example of the setup: somecheckbox.js angular.module('namespace.directives') .directive('someCheckbox& ...

Creating a dynamic directive in AngularJS: Learn how to add or remove it from the DOM based on specific conditions

Suppose I have a customized modal directive that looks like this: return { restrict: 'E', templateUrl: 'modal.html', scope: { modalContent: "&", modalOpen: "&" } } And in the HTML: <ng-modal modal-content="co ...

Refreshing information within a table using Ajax Prototype

For my current project, I am utilizing PrototypeJS. I have implemented Ajax.PeriodicalUpdater to insert real-time data as required. However, I am facing an issue where the data is not getting replaced inside a specific table. Below is the HTML code snippet ...

Issue with Materialize Sidenav: Not functional on iOS devices including iPhones, functions correctly on all other devices

My Materialize Sidenav is functioning on all devices except for iPad and iPhone. If you want to check out the code, here is the link to the repository: repo. Take a look at index.html (line 44 down) and js/onloadSetup.js. I attempted adding this in onload ...