Search And Data Processing

Aggregates: Finding the total sales of a product from an object array (with Addons)

Summary

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:

  1. A Xano account (sign up for free at https://xano.io)
  2. 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:

  1. Company: Stores information about companies.
  2. Product: Stores information about products, with each product belonging to a company.
  3. Sales: Stores sales data, including an array of objects representing the cart, with each object containing the `product_id` and `amount`.

Step 2: Create the API Endpoint

  1. In the Xano dashboard, navigate to the "APIs" section and create a new API.
  2. Add a path parameter for the `company_id` to filter the results by company.
  3. In the query builder, select the `Product` table and filter it by the `company_id` parameter.

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:

  1. In the query builder, click "Create New Add-on" and select the `Sales` table.
  2. Select "Aggregation" as the add-on type.
  3. In the next step, you'll notice that the `product_id` and `amount` fields from the cart array are not available for aggregation. This is because we need to bind the array first.
  4. Click on the add-on in the query builder to open the advanced editor.
  5. Remove the `sales_id` field from the query and replace it with `bind cart`.
  6. To make the `product_id` and `amount` fields available for aggregation, create two new "evals" by clicking the "+" button next to "Evals." Name one `product_id` and set it equal to `cart.product_id`, and name the other `amount` and set it equal to `cart.amount`.
  7. Now you can group by `product_id` and aggregate the `amount` using the `sum` function.
  8. 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!

This transcript was AI generated to allow users to quickly answer technical questions about Xano.

Was this helpful?

I found it helpful

I need more support
Sign up for XanoSign up for Xano

Build without limits on a secure, scalable backend.

Unblock your team's progress and create a backend that will scale for free.

Start building for free