Aggregates: Finding the total sales of a product from an object array (with Addons)
In this step-by-step guide, we'll walk through how to use Xano to build a feature that aggregates the total sales for each product in a company's cart. This is a common requirement for e-commerce applications, and we'll show you how Xano's no-code capabilities can simplify this process.
Prerequisites
Before we begin, make sure you have the following:
- A Xano account (sign up for free at https://xano.io)
- A basic understanding of data models and relationships
Step 1: Set up the Data Model
For this example, we'll use a simplified data model with three tables:
- Company: Stores information about companies.
- Product: Stores information about products, with each product belonging to a company.
- Sales: Stores sales data, including an array of objects representing the cart, with each object containing the
product_idandamount.
Step 2: Create the API Endpoint
- In the Xano dashboard, navigate to the "APIs" section and create a new API.
- Add a path parameter for the
company_idto filter the results by company. - In the query builder, select the
Producttable and filter it by thecompany_idparameter.
At this point, you should be able to see the products belonging to a specific company when you test the API endpoint.
Step 3: Add the Sales Aggregation
To aggregate the sales data for each product, we'll need to use Xano's Add-on feature. Here's how:
In the query builder, click "Create New Add-on" and select the
Salestable.Select "Aggregation" as the add-on type.
In the next step, you'll notice that the
product_idandamountfields from the cart array are not available for aggregation. This is because we need to bind the array first.Click on the add-on in the query builder to open the advanced editor.
Remove the
sales_idfield from the query and replace it withbind cart.To make the
product_idandamountfields available for aggregation, create two new "evals" by clicking the "+" button next to "Evals." Name oneproduct_idand set it equal tocart.product_id, and name the otheramountand set it equal tocart.amount.Now you can group by
product_idand aggregate theamountusing thesumfunction.Give the add-on a name and save it.
Step 4: Test the API
You can now test the API endpoint by providing a company_id. The response should include the products belonging to that company, along with the total sales for each product aggregated from the cart data.
Conclusion
Congratulations! You've successfully built a feature that aggregates sales data for each product in a company's cart using Xano's no-code platform. This is just one example of how Xano can simplify backend development for a wide range of applications.
Whether you're a non-technical user, a citizen developer, or an experienced developer, Xano can help you build and deploy backend services faster and more efficiently. Give it a try and see how it can streamline your development process!
Sign up for Xano.
100,000+ people build on Xano.
Start today and scale to millions.