Wes Hatch

front-end developer

Supabase Cheatsheet

tl;dr Dumping some commonly-used things from my explorations with Supabase.

Quick Setup

Pull it from the cloud if you want to quickly get up and running. This’ll set a supabase on the window.

  <script src="https://unpkg.com/@supabase/supabase-js"></script>

Alternatively, using Skypack is an easy way to quickly spin up a build-less POC.

<script type="module">
import supabase from 'https://cdn.skypack.dev/supabase';
// ...

Initialize some things:

  // ...
const DB_URL = 'https://<your-database-url>.supabase.co';
const PUBLIC_KEY = 'abcdef1234xxxx....';
const { createClient } = supabase;
const client = createClient(DB_URL, PUBLIC_KEY);

Search

Pagination

First, a helper function to managing paging:

function paginate(page, size) {
const limit = size ? size : 50;
const from = page * limit;
const to = from + size - 1;
return { from, to }
}

Use it with the range filter:

const { from, to } = paginate(page, 50);
let query = client
.from('books')
.select('title, id, tags')
.range(from, to);

JOIN (or, getting things from multiple tables at once)

First, you’ll need a “foreign key” set up.
That is, a key that points to a record in a different table.
You can set this in the Supabase table editor.

  .select(`
title,
synopsis,
author ( name ), // will get the authors NAME*
tags ( name ) // will get the tag NAME*
`
)

* NOTE: it doesn’t say here where/what table authors live in. Nor the tags. We just have to know that those relationships were previously set up in the database.