DEV Community

Cover image for Supabase Quick Docs
xiaoguang_
xiaoguang_

Posted on

Supabase Quick Docs

I keep a small note when learning a new tool.

Not a full guide. Not polished documentation.

Just the syntax I actually use.

After a few projects with Supabase, I noticed the same pattern. I kept opening the docs, searching for the same things:

  • How to insert a row
  • How to query data
  • How to update something
  • How to call an RPC

So I wrote a small “quick syntax sheet”.

This is the version I wish I had when starting.


Supabase is simple once you learn the core pattern.

Almost everything follows this shape:

supabase
  .from("table")
  .operation(...)
Enter fullscreen mode Exit fullscreen mode

If you remember that, the rest becomes predictable.

Setup

Create a client first.

import { createClient } from "@supabase/supabase-js"

const supabase = createClient(
  process.env.NEXT_PUBLIC_SUPABASE_URL,
  process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
)
Enter fullscreen mode Exit fullscreen mode

That’s it.

Now every database call starts with supabase.

Select (Read Data)

The most common query.

const { data, error } = await supabase
  .from("posts")
  .select("*")
Enter fullscreen mode Exit fullscreen mode

You’ll probably write this line hundreds of times.

Select specific columns

.select("id, title, created_at")
Enter fullscreen mode Exit fullscreen mode

Example:

const { data } = await supabase
  .from("posts")
  .select("id, title")
Enter fullscreen mode Exit fullscreen mode

Filter rows

.eq(column, value)
Enter fullscreen mode Exit fullscreen mode

Example:

const { data } = await supabase
  .from("posts")
  .select("*")
  .eq("author_id", user.id)
Enter fullscreen mode Exit fullscreen mode

Other common filters:

.eq()   equal
.neq()  not equal
.gt()   greater than
.lt()   less than
.like() pattern match
Enter fullscreen mode Exit fullscreen mode

Get one row

.single()
Enter fullscreen mode Exit fullscreen mode

Example:

const { data } = await supabase
  .from("posts")
  .select("*")
  .eq("id", postId)
  .single()
Enter fullscreen mode Exit fullscreen mode

Insert (Create Data)

Insert is straightforward.

const { data, error } = await supabase
  .from("posts")
  .insert({
    title: "\"My post\","
    content: "Hello world"
  })
Enter fullscreen mode Exit fullscreen mode

Multiple rows:

.insert([
  { title: "\"Post 1\" },"
  { title: "\"Post 2\" }"
])
Enter fullscreen mode Exit fullscreen mode

Return inserted data

Add .select().

const { data } = await supabase
  .from("posts")
  .insert({ title: "\"Hello\" })"
  .select()
Enter fullscreen mode Exit fullscreen mode

Useful when you need the new id.

Update

Update rows with a filter.

const { data, error } = await supabase
  .from("posts")
  .update({ title: "\"Updated title\" })"
  .eq("id", postId)
Enter fullscreen mode Exit fullscreen mode

Without a filter, it updates every row.

That’s a mistake most people make once.

Delete

Delete also requires a filter.

await supabase
  .from("posts")
  .delete()
  .eq("id", postId)
Enter fullscreen mode Exit fullscreen mode

Again, no filter means deleting everything.

Ordering

Sort results.

.order("created_at", { ascending: false })
Enter fullscreen mode Exit fullscreen mode

Example:

const { data } = await supabase
  .from("posts")
  .select("*")
  .order("created_at", { ascending: false })
Enter fullscreen mode Exit fullscreen mode

Limit

Limit rows returned.

.limit(10)
Enter fullscreen mode Exit fullscreen mode

Example:

const { data } = await supabase
  .from("posts")
  .select("*")
  .limit(10)
Enter fullscreen mode Exit fullscreen mode

Pagination

Use .range().

.range(0, 9)
Enter fullscreen mode Exit fullscreen mode

Example:

const { data } = await supabase
  .from("posts")
  .select("*")
  .range(0, 9)
Enter fullscreen mode Exit fullscreen mode

This returns rows 0–9.

Joins

Supabase uses PostgREST syntax.

Example:

const { data } = await supabase
  .from("posts")
  .select("*, profiles(username)")
Enter fullscreen mode Exit fullscreen mode

This joins the profiles table.

Example result:

post
  └ profile.username
Enter fullscreen mode Exit fullscreen mode

Auth (Quick Syntax)

Get the current user:

const { data } = await supabase.auth.getUser()
Enter fullscreen mode Exit fullscreen mode

Sign in:

await supabase.auth.signInWithPassword({
  email,
  password
})
Enter fullscreen mode Exit fullscreen mode

Sign up:

await supabase.auth.signUp({
  email,
  password
})
Enter fullscreen mode Exit fullscreen mode

Sign out:

await supabase.auth.signOut()
Enter fullscreen mode Exit fullscreen mode

Storage

Upload a file.

await supabase.storage
  .from("avatars")
  .upload("user.png", file)
Enter fullscreen mode Exit fullscreen mode

Get public URL:

supabase.storage
  .from("avatars")
  .getPublicUrl("user.png")
Enter fullscreen mode Exit fullscreen mode

RPC (Calling Database Functions)

If you define a Postgres function, call it like this:

const { data } = await supabase
  .rpc("get_popular_posts")
Enter fullscreen mode Exit fullscreen mode

With parameters:

.rpc("get_posts_by_user", {
  user_id: userId
})
Enter fullscreen mode Exit fullscreen mode

The Pattern Behind Everything

After using Supabase for a while, you notice something simple.

Almost every query follows the same structure.

supabase
  .from(table)
  .action()
  .filter()
  .modifier()
Enter fullscreen mode Exit fullscreen mode

Examples:

.from().select()
.from().insert()
.from().update()
.from().delete()
Enter fullscreen mode Exit fullscreen mode

Once that clicks, the API feels predictable.

What I Actually Remember

When working quickly, I don’t remember the whole API.

I remember a few patterns:

  • .from("table")
  • .select("*")
  • .insert({})
  • .update({}).eq()
  • .delete().eq()
  • .order()
  • .limit()

Everything else can be looked up.

A Small Lesson About Tools

Most tools feel complex at first.

But good tools have a small core idea.

For Supabase, it’s this:

Pick a table.
Choose an operation.
Add filters.
Enter fullscreen mode Exit fullscreen mode

That’s most of the API.

Once you see the pattern, the documentation becomes easier to navigate.


Read on my site: https://kimkorngmao.com/notes/supabase-quick-docs

Top comments (0)