Back to all functions

Supabase: Find Rows w/ Filter

This function uses a filterValue, filter to fetch information from a table in supabase. The filterOperator can be one of the following: Equality and Inequality Operators: eq: Equal to neq: Not equal to gt: Greater than gte: Greater than or equal to lt: Less than lte: Less than or equal to like: Pattern match using % for wildcard characters ilike: Case-insensitive pattern match in: Matches any value in a list (e.g., in.(1,2,3)) cs: Contains (e.g., arrays containing specific elements) cd: Contained by ov: Overlaps with fts: Full-text search using PostgreSQL's text search functions plfts: Plain to full-text search phfts: Phrase to full-text search wfts: Web to full-text search not: Negate the filter condition or: Logical OR to combine multiple conditions ->: Access JSON object field as JSON ->>: Access JSON object field as text @>: JSON contains <@: JSON is contained by is: Checks for null, true, false isnot: Checks for not null, true, false

Created By
Muhammad Dabeer
Community
download-icon
INPUT VARIABLES
{
apikey
}
your supabase API key
{
subdomain
}
your supabase subdomain
{
tablename
}
the table being looked up
{
filterColumn
}
the column being filtered on
{
filterOperator
}
Any from: eq (equal),
{
filterValue
}
the value being filtered for
share-icon
OUTPUT VARIABLES
{
results
}
Returns the array of objects retrieved from the database against the given criteria
{
}
{
}
{
}
{
}
{
}
paths-icon
PATHS
{
success
}
Found Rows
{
error
}
Error
{
}
{
}
{
}
{
}

Function Code Snippet

 
export default async function main(args) {
  let { apikey, subdomain, tablename, filterColumn, filterOperator, filterValue } = args.inputVars; // Input variables

  // Check if the user has inputted the required variables
  if (!apikey || !subdomain || !tablename || !filterColumn || !filterOperator || !filterValue) {
    return {
      // Returns the error path so we can continue the design
      next: { path: 'error' },
      // Renders a debug message in Voiceflow
      trace: [{ type: "debug", payload: { message: "Missing required input variables for this function" } }]
    };
  }

  // Base URL with query parameters
  const url = `https://${subdomain}.supabase.co/rest/v1/${tablename}?${filterColumn}=${filterOperator}.${filterValue}&select=*`;

  // Setup the request options, including headers
  const config = {
    method: 'GET',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${apikey}`,
      'apikey': apikey,
      'Connection': 'keep-alive'
    }
  };

  // This is where we made the fetch request, we use try-catch for error handling
  try {
    // Make the fetch request
    const response = await fetch(url, config);

    // Check if the response status is OK (status in the range 200-299)
    if (!response.ok) {
      // If not OK, throw an error to be caught by the catch block
      throw new Error(`HTTP error! status: ${response.status}`);
    }

    // Map the fetch request response
    const responseBody = await response.json; // Use .json() to parse the response body

    // Checks if the fetch request returned a body
    if (!responseBody || !Array.isArray(responseBody)) {
      // If no body was returned, throw an error
      throw new Error(`Invalid or missing response body from the API`);
    }
    // Create the return objects if this is successful
    return {
      // Map our output variables
      outputVars: {
        results: JSON.stringify(responseBody)
      },
      // Map the success path so we can continue in our flow
      next: { path: 'success' }
    };
  }
  // Catches all the errors we threw and displays the debug message
  catch (error) {
    return {
      // Maps the error path so we can continue in our design
      next: { path: 'error' },
      // Renders a debug message in Voiceflow with the error
      trace: [{ type: "debug", payload: { message: "Error:" + error.message + ' ' + url } }]
    };
  }
}
copy-icon

Function Walkthrough

Explore More Functions

Build and submit a Function to have it featured in the community.

ghraphic