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:
- An existing table with a column containing an array of objects
- 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:
- Go to the "APIs" section and click "Create API".
- Give your API a descriptive name, such as "Add New Product Attribute".
- 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:
- Add a new step and select the "Get Record" action.
- Set the `field name` to the column you're searching by (e.g., `name`).
- 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:
- Get the last object in the array
- Retrieve its `id` value
- Increment the `id` by 1
- Create a new object with the incremented `id` and the provided `color` and `size`
Here's how to do it in a single step:
- Add a new "Edit Record" step.
- Set the `field name` to the column containing the array of objects (e.g., `attributes`).
- 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
- 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.