|
| 1 | +--- |
| 2 | +title: Supabase |
| 3 | +--- |
| 4 | + |
| 5 | +import Pipeline from '@site/src/components/usethispipeline' |
| 6 | + |
| 7 | +[Supabase](https://supabase.com/) is an open-source backend-as-a-service |
| 8 | +platform designed to simplify the development of web and mobile applications. It |
| 9 | +provides developers with a suite of tools including a Postgres database, |
| 10 | +real-time subscriptions, authentication, storage, and serverless functions, all |
| 11 | +managed through a single interface. Supabase provides multiple ways to consume |
| 12 | +data from Golioth. |
| 13 | + |
| 14 | +The first example is the simplest and uses the REST API. Assuming a database |
| 15 | +named `golioth_pipeline_basic` with the following columns: |
| 16 | + |
| 17 | +| id | created_at | temp | lat | long | |
| 18 | +| -- | ---------- | ---- | --- | ---- | |
| 19 | + |
| 20 | +Create the following Pipeline: |
| 21 | + |
| 22 | +<Pipeline link='https://console.golioth.dev/pipeline?name=Supabase%20Edge%20Function%20Example&pipeline=c3RlcHM6CiAgLSBuYW1lOiBzdGVwMAogICAgZGVzdGluYXRpb246CiAgICAgIHR5cGU6IHdlYmhvb2sKICAgICAgdmVyc2lvbjogdjEKICAgICAgcGFyYW1ldGVyczoKICAgICAgICB1cmw6IGh0dHBzOi8vcmdnY3Fvc2x0cW9wY3d0d3doa2Muc3VwYWJhc2UuY28vZnVuY3Rpb25zL3YxL2dvbGlvdGgtcGlwZWxpbmUKICAgICAgICBoZWFkZXJzOgogICAgICAgICAgQXV0aG9yaXphdGlvbjogJFNVUEFCQVNFX0FVVEhfSEVBREVSCiAgICAgICAgICBhcGlrZXk6ICRTVVBBQkFTRV9BUElfS0VZCg=='/> |
| 23 | + |
| 24 | +Create two [secrets](/data-routing/secrets) based on the |
| 25 | +[Service Role Key](https://supabase.com/docs/guides/api/api-keys#the-servicerole-key). |
| 26 | +`$SUPABASE_KEY` should be the the Server Role Key while `$SUPABASE_SERVICE_KEY` |
| 27 | +should take the form of 'Bearer |
| 28 | +$Service_Role_Key'. For example, if the `Service Role Key` is `12345` than the `$SUPABASE_SERVICE_KEY`should be set to`Bearer |
| 29 | +12345`. |
| 30 | + |
| 31 | +The second example uses Edge Functions. While |
| 32 | +[Transformers](/data-routing/transformers) can be used to modify data, Edge |
| 33 | +Functions can be utilized further in the context of Supabase. |
| 34 | + |
| 35 | +Assuming a database named `golioth_pipeline_advanced` with the following |
| 36 | +columns: |
| 37 | + |
| 38 | +| id | created_at | ce-time | ce-subject | ce-type | ce-source | temp | lat | long | |
| 39 | +| -- | ---------- | ------- | ---------- | ------- | --------- | ---- | --- | ---- | |
| 40 | + |
| 41 | +Create the following Pipeline: |
| 42 | + |
| 43 | +<Pipeline link='https://console.golioth.dev/pipeline?name=Supabase%20Edge%20Function%20Example&pipeline=c3RlcHM6CiAgLSBuYW1lOiBzdGVwMAogICAgZGVzdGluYXRpb246CiAgICAgIHR5cGU6IHdlYmhvb2sKICAgICAgdmVyc2lvbjogdjEKICAgICAgcGFyYW1ldGVyczoKICAgICAgICB1cmw6IGh0dHBzOi8vcmdnY3Fvc2x0cW9wY3d0d3doa2Muc3VwYWJhc2UuY28vZnVuY3Rpb25zL3YxL2dvbGlvdGgtcGlwZWxpbmUKICAgICAgICBoZWFkZXJzOgogICAgICAgICAgQXV0aG9yaXphdGlvbjogJFNVUEFCQVNFX0FVVEhfSEVBREVSCiAgICAgICAgICBhcGlrZXk6ICRTVVBBQkFTRV9BUElfS0VZCg=='/> |
| 44 | + |
| 45 | +Reuse or create two [secrets](/data-routing/secrets) based on the |
| 46 | +[Service Role Key](https://supabase.com/docs/guides/api/api-keys#the-servicerole-key). |
| 47 | +`$SUPABASE_KEY` should be the the Server Role Key while `$SUPABASE_SERVICE_KEY` |
| 48 | +should take the form of 'Bearer |
| 49 | +$Service_Role_Key'. For example, if the `Service Role Key` is `12345` than the `$SUPABASE_SERVICE_KEY`should be set to`Bearer |
| 50 | +12345`. |
| 51 | + |
| 52 | +Create a new Edge Function called `golioth-pipelines` with the following code: |
| 53 | + |
| 54 | +```ts |
| 55 | +import { createClient } from "jsr:@supabase/supabase-js@2"; |
| 56 | + |
| 57 | +Deno.serve(async (req) => { |
| 58 | + try { |
| 59 | + const supabase = createClient( |
| 60 | + Deno.env.get("SUPABASE_URL") ?? "", |
| 61 | + Deno.env.get("SUPABASE_ANON_KEY") ?? "", |
| 62 | + { |
| 63 | + global: { |
| 64 | + headers: { Authorization: req.headers.get("Authorization")! }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + ); |
| 68 | + |
| 69 | + const body = await req.json(); |
| 70 | + |
| 71 | + const { error } = await supabase |
| 72 | + .from("golioth_pipeline_advanced") |
| 73 | + .insert({ |
| 74 | + "ce-subject": req.headers.get("ce-subject"), |
| 75 | + "ce-time": req.headers.get("ce-time"), |
| 76 | + "ce-source": req.headers.get("ce-source"), |
| 77 | + "ce-type": req.headers.get("ce-type"), |
| 78 | + temp: body.temp, |
| 79 | + lat: body.lat, |
| 80 | + long: body.long, |
| 81 | + }); |
| 82 | + |
| 83 | + if (error) { |
| 84 | + throw error; |
| 85 | + } |
| 86 | + |
| 87 | + return new Response(body, { |
| 88 | + headers: { "Content-Type": "application/json" }, |
| 89 | + status: 200, |
| 90 | + }); |
| 91 | + } catch (err) { |
| 92 | + return new Response(String(err?.message ?? err), { status: 500 }); |
| 93 | + } |
| 94 | +}); |
| 95 | +``` |
0 commit comments