From 86302b48e645f149e52c3a34a94c420e96f8993d Mon Sep 17 00:00:00 2001 From: Florian Loitsch Date: Mon, 24 Feb 2025 10:09:58 +0100 Subject: [PATCH] Fix and update supabase tutorial. (#512) --- docs/tutorials/network/supabase.mdx | 40 +++++++++++++++++----------- tools/package.lock | 4 +-- tools/package.yaml | 2 +- tutorial_code/supabase/package.lock | 22 +++++++-------- tutorial_code/supabase/package.yaml | 6 ++--- tutorial_code/supabase/supabase.toit | 5 ++-- 6 files changed, 44 insertions(+), 35 deletions(-) diff --git a/docs/tutorials/network/supabase.mdx b/docs/tutorials/network/supabase.mdx index b211098c..a2faf755 100644 --- a/docs/tutorials/network/supabase.mdx +++ b/docs/tutorials/network/supabase.mdx @@ -33,7 +33,7 @@ necessary, and click on "Create new project". Wait until the project is completely set up (this may take a few minutes), then go to the "Project Settings" (gear icon) tab. In the "General" section, you will find -the "Reference ID" (also known as "project id"). In the "API" section, +the "Reference ID" (also known as "project id"). In the "Data API" section, you will find anon key. You will need both of these values later. @@ -82,7 +82,7 @@ Click on "+ New query" (blank query) and enter the following SQL statements. ``` sql CREATE TABLE devices ( - id uuid PRIMARY KEY + id uuid PRIMARY KEY DEFAULT gen_random_uuid() ); ALTER TABLE devices ENABLE ROW LEVEL SECURITY; @@ -106,20 +106,30 @@ CREATE POLICY "Enable insert to everyone" Click on `RUN` (or press `Ctrl+Enter`) to execute the statements. -You should now have two tables in your project. +You should now have two tables in your project. You can see them in +the Table Editor (under `https://supabase.com/dashboard/project//editor`). ## Preparation -Create a fresh UUID for the device ID. You can generate one -[online](https://www.uuidgenerator.net/) or with the `uuidgen` command -on Linux. +Let's add a new device into the `devices` table. You can either +specify a device ID yourself, or let the DB generate one for you. -Add this device ID to the `devices` table. You can do this with the -online table editor, or with the following SQL statement (replace -`` with the device ID): +In the first case the SQL statement would look like this: ``` sql INSERT INTO devices (id) VALUES (''); ``` +You can, of course, also use the table editor to add a new row with +that value. + +In the second case, you can just use the default action which will +generate a new UUID for you. The SQL statement would look like this: + +``` sql +INSERT INTO devices DEFAULT VALUES; +``` + +We will refer to the new device ID as `` in the following +sections. ## Toit program Now we can write the Toit program that will send sensor values to Supabase. @@ -130,7 +140,7 @@ it with the following command. See the [packages](../../setup/packages) tutorial for details. ```shell -jag pkg install github.com/toitware/toit-supabase@0.2 +jag pkg install github.com/toitware/toit-supabase@0.3 ``` @@ -167,20 +177,21 @@ import supabase TABLE ::= "temperatures" PROJECT-ID ::= "" ANON-KEY ::= "" +URI ::= "https://$(PROJECT-ID).supabase.co" DEVICE-ID ::= "" main: certificate-roots.install-common-trusted-roots host := "$(PROJECT-ID).supabase.co" - client := supabase.Client.tls - --host=host + client := supabase.Client + --uri=URI --anon=ANON-KEY // We don't want to receive the inserted row, because we don't have // read access to the table. client.rest.insert TABLE --no-return-inserted { - "device-id": DEVICE-ID, + "device_id": DEVICE-ID, "temperature": 42.0, } @@ -188,9 +199,6 @@ main: ``` The program uses the `supabase.Client` class to connect to Supabase. -Since the project needs to be accessed with TLS (https), we need to provide -the root certificate for the connection. We do this with the -`certificate-roots.BALTIMORE-CYBERTRUST-ROOT` constant. This program will send a sensor value of 42.0 to the `temperatures` table in your Supabase project. It will use the device ID that you entered into diff --git a/tools/package.lock b/tools/package.lock index 0950c896..8e1f7445 100644 --- a/tools/package.lock +++ b/tools/package.lock @@ -151,8 +151,8 @@ packages: toit-supabase: url: github.com/toitware/toit-supabase name: supabase - version: 0.2.8 - hash: 168ddb448e71e91431e71121958c32aaf4c1ef02 + version: 0.3.1 + hash: bcf10850b0fae0592bb0af6563fb994cececb8f9 prefixes: host: pkg-host http: pkg-http diff --git a/tools/package.yaml b/tools/package.yaml index 649f826e..c72b2335 100644 --- a/tools/package.yaml +++ b/tools/package.yaml @@ -58,7 +58,7 @@ dependencies: version: ^2.0.1 supabase: url: github.com/toitware/toit-supabase - version: ^0.2.8 + version: ^0.3.1 telegram: url: github.com/floitsch/toit-telegram version: ^0.5.3 diff --git a/tutorial_code/supabase/package.lock b/tutorial_code/supabase/package.lock index 823d78ed..ccbe0965 100644 --- a/tutorial_code/supabase/package.lock +++ b/tutorial_code/supabase/package.lock @@ -1,28 +1,28 @@ -sdk: ^2.0.0-alpha.144 +sdk: ^2.0.0-alpha.150 prefixes: - certificate_roots: toit-cert-roots + certificate-roots: toit-cert-roots supabase: toit-supabase packages: pkg-host: url: github.com/toitlang/pkg-host name: host - version: 1.15.1 - hash: ff187c2c19d695e66c3dc1d9c09b4dc6bec09088 + version: 1.15.3 + hash: 62393e8522b77eafbafe60b9817935266117daf6 pkg-http: url: github.com/toitlang/pkg-http name: http - version: 2.7.1 - hash: 54a5bb458d9fc9305e839b878e6de3b456f5c4a6 + version: 2.8.0 + hash: 81754d64fe466cd00cc494ec515da8749bf04975 toit-cert-roots: url: github.com/toitware/toit-cert-roots - name: certificate_roots - version: 1.6.1 - hash: 55d3be82ed53d8d332338b2de931865cf69fe48b + name: certificate-roots + version: 1.8.0 + hash: 369a0b36e813e37d6248990de59964020644ab50 toit-supabase: url: github.com/toitware/toit-supabase name: supabase - version: 0.2.8 - hash: 168ddb448e71e91431e71121958c32aaf4c1ef02 + version: 0.3.1 + hash: bcf10850b0fae0592bb0af6563fb994cececb8f9 prefixes: host: pkg-host http: pkg-http diff --git a/tutorial_code/supabase/package.yaml b/tutorial_code/supabase/package.yaml index 79fd9348..fe0960d0 100644 --- a/tutorial_code/supabase/package.yaml +++ b/tutorial_code/supabase/package.yaml @@ -1,7 +1,7 @@ dependencies: - certificate_roots: + certificate-roots: url: github.com/toitware/toit-cert-roots - version: ^1.6.1 + version: ^1.8.0 supabase: url: github.com/toitware/toit-supabase - version: ^0.2.8 + version: ^0.3.1 diff --git a/tutorial_code/supabase/supabase.toit b/tutorial_code/supabase/supabase.toit index 49b68921..b8a9652d 100644 --- a/tutorial_code/supabase/supabase.toit +++ b/tutorial_code/supabase/supabase.toit @@ -8,14 +8,15 @@ import supabase TABLE ::= "temperatures" PROJECT-ID ::= "" ANON-KEY ::= "" +URI ::= "https://$(PROJECT-ID).supabase.co" DEVICE-ID ::= "" main: certificate-roots.install-common-trusted-roots host := "$(PROJECT-ID).supabase.co" - client := supabase.Client.tls - --host=host + client := supabase.Client + --uri=URI --anon=ANON-KEY // We don't want to receive the inserted row, because we don't have