Skip to content

Latest commit

 

History

History
214 lines (152 loc) · 11.7 KB

supabase-crud.mdx

File metadata and controls

214 lines (152 loc) · 11.7 KB

Integrate Supabase DB with BuildShip

import { Callout } from 'nextra/components'; import Image from 'next/image'; import supabase1 from '/public/tutorial/supabase1.png'; import supabaseCreate from '/public/tutorial/create-row.png'; import supabaseRead from '/public/tutorial/get-row.png'; import supabaseUpdate from '/public/tutorial/update-row.png'; import supabaseDelete from '/public/tutorial/delete-row.png'; import supabase2 from '/public/tutorial/supabase2.png'; import supabase3 from '/public/tutorial/supabase3.png'; import rls from '/public/tutorial/rls.png'; import supabaseRls from '/public/tutorial/supabase-rls.png';

BuildShip offers a powerful integration with Supabase allowing you to perform Create, Read, Update, and Delete (CRUD) operations on your data with ease. This integration is achieved through the Supabase Realtime API.

Supabase Integration

Prerequisites ✅

Your Supabase Table

A Supabase Table you want to integrate with BuildShip. If you don't have a Supabase project, you can create one at supabase.io.

API Key and URL

Supabase provides two default API keys when you create a project: an anon key and a service_role key. These keys can be found in the API Settings section of the dashboard.

Before you begin, make sure you have your Supabase API key and Table URL ready. You can obtain these keys from your Supabase project dashboard settings:

  • Log in to your Supabase account.
  • Navigate to your project's dashboard.
  • In the dashboard settings, find and copy your API key and Table URL. These keys will be used to connect BuildShip with your Supabase project.

Supabase API Key and URL

Supabase API Key and URL

The anon Key

The anon key has very limited privileges and is designed to be used in Row Level Security (RLS) policies to allow unauthenticated access to specific database tables or rows. For example: a policy can be created to allow public access to the profiles table using the anon key.

The service_role Key

The service_role key has elevated privileges and can bypass RLS policies. This key should only be used on a private server and never exposed to users or client-side applications.

Supabase Row-Level Security (RLS)

The BuildShip Supabase nodes use the Supabase Data APIs which are designed to work with Postgres Row Level Security (RLS).

Enabling Row Level Security


Supabase RLS

Any table you create in the public schema will be accessible via the Supabase Data API.

To restrict access, enable Row Level Security (RLS) on all tables, views, and functions in the public schema. You can then write RLS policies to grant users access to specific database rows or functions based on their authentication token.

To learn how to set up the Supabase Row Level Security, read the Supabase Docs [here](https://supabase.com/docs/guides/api/securing-your-api?queryGroups=database-method&database-method=dashboard#enabling-row-level-security).

API Endpoints (Optional - For creating your own custom nodes)

You can get access to API endpoints directly from your table by clicking on the "API" button located at the top right corner of the Supabase table view. This is useful if you wish to create your own custom nodes for specific endpoints.

To perform CRUD operations on your Supabase database, you'll be required to create a custom RLS Policy (Row Level Security). By default, Supabase RLS is enabled to restrict all access using the anon key, this will prevent any operation and might result in an empty array.

Supabase RLS

Note: You can also disable the RLS, but its not recommended. You can securely store your Supabase API key using BuildShip's secret manager, learn more here.

When to use Tokens?

BuildShip offers two sets of Supabase nodes, one with Authorization Token and one without. The with Token nodes are a slightly adapted version to support an Authorization token being passed in from your client. This allows Supabase Row Level Security to be utilised as a passthrough with Buildship.

What this means is that you can use the with Token nodes when you are using BuildShip along with FlutterFlow or other no-code frontend tools, and authenticating with Supabase using their out-of-the-box functionality. This will allow you to securely pass the Authorization token from your client to BuildShip.

If you're interesting in knowing more about passing the authorization token from your FlutterFlow app to BuildShip, you can read the detailed guide [here](https://docs.flutterflow.io/data-and-backend/custom-authentication).

If your use case does not require passing the Authorization token from your client to BuildShip, you can use the nodes without the Authorization Token.

Creating Data

Supabase Create Node

With BuildShip's integration, you can easily create new records in your Supabase PostgreSQL database. Follow these steps:

  • In your BuildShip Workflow, add a Supabase Create Node from the Node Explorer. You can find the Supabase Create Node under the "Supabase" integration category or you could simply enter Supabase on the search bar.
  • Configure the node by providing your Supabase API Key, Bearer Token, Table URL, and Table Name.
  • Open the Node Editor to test your node.
  • Add the data you want to create in the "Data" field in the form of a JSON object. Here's an example of a JSON object you can use to create a new record in your Supabase table:
{
  "breed_name": "Birman",
  "country_of_origin": "Burma"
}
  • Test your node by clicking on the "Test" button. If the node is configured correctly, you should see a new record in your Supabase table.
  • Optionally, you could modify and customize the node according to your needs. Once you're done, click on the "Save" button to save your node.
  • Add more nodes or simply add a Return node to complete your workflow.
BuildShip offers two versions of Supabase nodes, with and without Authorization Token. The `with Token` nodes are a slightly adapted version to **support an Authorization token being passed in from your client**. This allows **Supabase Row Level Security** to be utilised as a passthrough with Buildship.

This is useful for users using BuildShip along with FlutterFlow or other no-code frontend tools, and authenticating with Supabase using their out-of-the-box functionality.

Note: If you do not have an Access Token, use the Supabase "Create Row" node instead.

Reading Data

Supabase Read Node

To retrieve data from your Supabase database, use the Read Node:

  • In your BuildShip workflow, add a Supabase Read Node from the Node Explorer. You can find the Supabase Create Node under the "Supabase" integration category or you could simply enter Supabase on the search bar.
  • Configure the node by providing your Supabase API Key, Bearer Token, Table URL, and Table Name.
  • Specify the columns you want to retrieve from your table in the "Columns" field. You can also leave it empty which will by default retrieve all columns.
  • Optionally you could customize, test and save the node.
  • Execute the workflow, and BuildShip will fetch the data from your Supabase database in real-time.
  • Now you can use this data anywhere in your workflow via the Expressions menu.

Updating Data

Supabase Update Node

Updating records in your Supabase database is straightforward with the Update Node:

  • In your BuildShip workflow, add a Supabase Update Node from the Node Explorer.
  • Configure the node by providing your Supabase API Key, Bearer Token, Table URL, and Table Name.
  • You need to add conditions to specify which records should be updated using filters like eq, lt, and is.
Refer to the [Filters section below](#filters) for more information on how to use filters.
  • You can optionally customize, test and save the node.
  • Deploy your workflow, and everytime your workflow triggers, your data will be updated in your Supabase database in real-time.

Deleting Data

Supabase Delete Node

To delete records from your Supabase database, use the Delete Node:

  • In your BuildShip workflow, add a Supabase Delete Node from the Node Explorer.
  • Configure the node by providing your Supabase API Key, Bearer Token, Table URL, and Table Name.
  • Add filters to specify which records should be deleted using filters like eq, lt, and is. Refer to the Filters section below for more information on how to use filters.
  • Deploy your workflow and the specified records will be deleted from your Supabase database in real-time.

Filters

Filters are used to specify which records should be retrieved, updated, or deleted. You can add multiple filters to a node. Here's a list of all the filters you can use:

Operator Description Example Example Explanation
eq Filter records where a specific field is equal to a given value name=eq.siamese Match records where the name field is equal to siamese
neq Filters records where a specific field is not equal to a given value age=neq.5 Match records where the age field is not equal to 5
gt Filter records where a specific field is greater than a given value age=gt.30 Match records where the age field is greater than 30
lt Filters records where a specific field is less than a given value price=lt.10 Match records where the price field is less than 10
gte Filters records where a specific field is greater than or equal to a given value quantity=gte.100 Match records where the quantity field is greater than or equal to 100
lte Filters records where a specific field is less than or equal to a given value rating=lte.4.5 Match records where the rating field is less than or equal to 4.5
is Filters records via comparing fields with specific values or conditions, such as checking if a field is null email=is.null Match records where the email field is null
like Filters records via partial string matching name=like.%John% Match records where the name field contains John anywhere in the string
To use multiple filters, use the `&` operator to separate them. For example, to filter records where the `origin` field equals `Germany` and the `lifespan` field is greater than `15`, you would use the following filter: `origin=eq.Germany&lifespan=gte.15`.