Data Management

Auto-Incrementing ID for an Object List - Customer Corner

In this guide, we'll walk through how to automatically increment IDs when adding new objects to an existing array of objects using Xano's no-code platform. This can be useful when you need to maintain unique identifiers for elements within a collection, such as product attributes or order items.

Prerequisites

Before we begin, make sure you have the following:

  1. An existing table with a column containing an array of objects
  2. Each object in the array should have an id property

For this example, we'll use a products table with a column called attributes that stores an array of objects with id, color, and size properties.

Step 1: Create a New API Endpoint

First, let's create a new API endpoint in Xano:

  1. Go to the "APIs" section and click "Create API".
  2. Give your API a descriptive name, such as "Add New Product Attribute".
  3. Add the following input fields:
  • product: The name of the product you want to update
  • color: The color of the new attribute
  • size: The size of the new attribute

Step 2: Get the Existing Product Record

Next, we'll fetch the existing product record from the database:

  1. Add a new step and select the "Get Record" action.
  2. Set the field name to the column you're searching by (e.g., name).
  3. Set the field value to the product input.

This will retrieve the record containing the array of objects we want to modify.

Step 3: Increment the ID

To increment the ID, we'll need to:

  1. Get the last object in the array
  2. Retrieve its id value
  3. Increment the id by 1
  4. Create a new object with the incremented id and the provided color and size

Here's how to do it in a single step:

  1. Add a new "Edit Record" step.
  2. Set the field name to the column containing the array of objects (e.g., attributes).
  3. Set the value to the following expression:
push(
 {{products1.attributes}},
 set(
 set(
 {},
 "id",
 get(pop({{products1.attributes}}), "id") + 1
 ),
 "color",
 {{inputs.color}}
 ),
 "size",
 {{inputs.size}}
)

This expression does the following:

  • pop({{products1.attributes}}) gets the last object in the attributes array
  • get(..., "id") retrieves the id value from that object
  • ... + 1 increments the id by 1
  • set({}, "id", ...) creates a new object with the incremented id
  • set(..., "color", {{inputs.color}}) adds the color property from the input
  • set(..., "size", {{inputs.size}}) adds the size property from the input
  • push({{products1.attributes}}, ...) appends the new object to the existing attributes array
  1. Return the edited record in the response.

Step 4: Test and Deploy

You can now test your API endpoint by providing a product name, color, and size. The new attribute should be added to the attributes array with an incremented id.

Once you're satisfied with the results, you can deploy your API and start using it in your applications.

Conclusion

Xano's no-code platform makes it easy to perform complex operations like incrementing IDs in an array of objects without writing any code. By following this guide, you should now be able to automatically generate unique identifiers when adding new elements to existing collections in your data models.

Sign up for Xano

Join 100,000+ people already building with Xano.

Start today and scale to millions.

Start building for free