Skip to content

Commit

Permalink
Fix and update supabase tutorial.
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch committed Feb 24, 2025
1 parent 8a3941b commit fcde615
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 35 deletions.
40 changes: 24 additions & 16 deletions docs/tutorials/network/supabase.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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/<project-id>/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
`<device-id>` with the device ID):
In the first case the SQL statement would look like this:

``` sql
INSERT INTO devices (id) VALUES ('<device-id>');
```
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 `<device-id>` in the following
sections.

## Toit program
Now we can write the Toit program that will send sensor values to Supabase.
Expand All @@ -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
```

<Note>
Expand Down Expand Up @@ -167,30 +177,28 @@ import supabase
TABLE ::= "temperatures"
PROJECT-ID ::= "<project-id>"
ANON-KEY ::= "<anon-key>"
URI ::= "https://$(PROJECT-ID).supabase.co"

DEVICE-ID ::= "<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,
}

client.close
```

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
Expand Down
4 changes: 2 additions & 2 deletions tools/package.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tools/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions tutorial_code/supabase/package.lock
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions tutorial_code/supabase/package.yaml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions tutorial_code/supabase/supabase.toit
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import supabase
TABLE ::= "temperatures"
PROJECT-ID ::= "<project-id>"
ANON-KEY ::= "<anon-key>"
URI ::= "https://$(PROJECT-ID).supabase.co"

DEVICE-ID ::= "<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
Expand Down

0 comments on commit fcde615

Please sign in to comment.