Data Management

Creating an Addon when an ID is IN an array of IDs

In Xano, you can use add-ons to retrieve related data from other tables based on a specific condition. This is particularly useful when you have a one-to-many or many-to-many relationship between your data models. For example, let's say you have a companies table and a campaigns table, where a company can belong to multiple campaigns, and a campaign can have multiple companies.

Step 1: Understand Your Data Relationships

Before creating an add-on, it's essential to understand the relationship between your data models. In our example, the campaigns table has an array of table references to different companies. This means that a single campaign can be associated with multiple companies, and a company can belong to multiple campaigns.

Step 2: Create an Add-on

Xano provides a built-in wizard for creating simple add-ons, which is suitable for most use cases. However, when dealing with more complex data relationships, such as an array of table references, you'll need to create an add-on from the library to have more control over the query.

To create an add-on from the library, follow these steps:

  1. Go to the Library section in Xano.
  2. Click on New Add-on.
  3. Select the table you want to add the related data to (in our case, the companies table).
  4. Give your add-on a descriptive name (e.g., "campaigns_one").
  5. Click Save.

Step 3: Configure the Add-on

After creating the add-on, you'll be presented with the add-on configuration interface. Here, you can set up the input parameters and define the query to retrieve the related data.

  1. In the Inputs section, add a Table Reference input for the ID of the table you're adding the related data to (in our case, the company_id).
  2. In the Query All Records section, you'll define the query to retrieve the related data. Since we're dealing with an array of table references, we'll use the in operator to check if the company_id is present in the array of company IDs for each campaign.
input.company_id in company_id

This query will return all campaigns where the company_id matches the input company_id.

  1. (Optional) You can customize the response by selecting the fields you want to include from the related table (e.g., campaign_name and campaign_id).

Step 4: Use the Add-on in Your API

After configuring the add-on, you can use it in your API to retrieve the related data.

  1. Go to the table where you want to use the add-on (in our case, the companies table).
  2. In the Add-ons section, select the add-on you created (e.g., "campaigns_one").
  3. Map the input parameter (in our case, the company_id) to the appropriate field from your table.
  4. Run the API, and you'll see the related data from the campaigns table for each company.

By following these steps, you can easily retrieve related data from other tables in Xano using add-ons. This powerful feature allows you to build complex data models and relationships without writing any code.

Remember, Xano is designed to be user-friendly and accessible to both non-technical users and developers. With its visual interface and intuitive features like add-ons, you can quickly build and deploy backend services for your web and mobile applications.

Sign up for Xano

Join 100,000+ people already building with Xano.

Start today and scale to millions.

Start building for free