Skip to main content

Supabase

Supabase is an open-source backend-as-a-service platform designed to simplify the development of web and mobile applications. It provides developers with a suite of tools including a Postgres database, real-time subscriptions, authentication, storage, and serverless functions, all managed through a single interface. Supabase provides multiple ways to consume data from Golioth.

The following examples will need credentials from Supabase. Create two secrets based on the Service Role Key. $SUPABASE_API_KEY should be the the Server Role Key while $SUPABASE_AUTH_HEADER should take the form of Bearer $Service_Role_Key. For example, if the Service Role Key is 12345 than the $SUPABASE_AUTH_HEADER should be set toBearer 12345.

The first example is the simplest and uses the REST API. Assuming a table named golioth_pipeline_basic with the following columns:

idcreated_attemplatlong

Create the following Pipeline:

Pipeline: Supabase REST API Example
filter:
path: "*"
content_type: application/json
steps:
- name: step0
destination:
type: webhook
version: v1
parameters:
url: https://rggcqosltqopcwtwwhkc.supabase.co/rest/v1/golioth_pipeline_basic
headers:
Authorization: $SUPABASE_AUTH_HEADER
apikey: $SUPABASE_API_KEY

The second example uses Edge Functions. While Transformers can be used to modify data, Edge Functions can be utilized further in the context of Supabase.

Assuming a table named golioth_pipeline_advanced with the following columns:

idcreated_atce-timece-subjectce-typece-sourcetemplatlong

Create the following Pipeline:

Pipeline: Supabase Edge Function Example
filter:
path: "*"
content_type: application/json
steps:
- name: step0
destination:
type: webhook
version: v1
parameters:
url: https://rggcqosltqopcwtwwhkc.supabase.co/functions/v1/golioth-pipeline
headers:
Authorization: $SUPABASE_AUTH_HEADER
apikey: $SUPABASE_API_KEY

Create a new Edge Function called golioth-pipelines with the following code:

import { createClient } from "jsr:@supabase/supabase-js@2";

Deno.serve(async (req) => {
try {
const supabase = createClient(
Deno.env.get("SUPABASE_URL") ?? "",
Deno.env.get("SUPABASE_ANON_KEY") ?? "",
{
global: {
headers: { Authorization: req.headers.get("Authorization")! },
},
},
);

const body = await req.json();

const { error } = await supabase
.from("golioth_pipeline_advanced")
.insert({
"ce-subject": req.headers.get("ce-subject"),
"ce-time": req.headers.get("ce-time"),
"ce-source": req.headers.get("ce-source"),
"ce-type": req.headers.get("ce-type"),
temp: body.temp,
lat: body.lat,
long: body.long,
});

if (error) {
throw error;
}

return new Response(body, {
headers: { "Content-Type": "application/json" },
status: 200,
});
} catch (err) {
return new Response(String(err?.message ?? err), { status: 500 });
}
});