The Supabase JavaScript/TypeScript client mirrors PostgreSQL's declarative query power using an expressive, chainable API that translates directly into HTTP requests handled by PostgREST.
Comprehensive Query Operators
import { supabase } from '@/lib/supabase';
// High-precision querying
const { data, error, count } = await supabase
.from('invoices')
.select('id, invoice_number, total_amount, status, due_date', { count: 'exact' })
.eq('status', 'PENDING') // WHERE status = 'PENDING'
.gte('total_amount', 1000.00) // AND total_amount >= 1000
.lte('due_date', new Date().toISOString()) // AND due_date <= NOW()
.order('total_amount', { ascending: false }) // ORDER BY total_amount DESC
.range(0, 19); // LIMIT 20 OFFSET 0
PostgREST Comparison Matrix
| SDK Method | SQL Equivalent | Description |
|---|---|---|
.eq('col', val) | col = val | Strict equality |
.neq('col', val) | col <> val | Not equal |
.gt() / .gte() | col > val / col >= val | Greater than / or equal |
.lt() / .lte() | col < val / col <= val | Less than / or equal |
.like('col', '%val%') | col LIKE '%val%' | Case-sensitive pattern match |
.ilike('col', '%val%') | col ILIKE '%val%' | Case-insensitive pattern match |
.is('col', null) | col IS NULL | Null-safe equality check |
.in('col', [a, b]) | col IN (a, b) | Matches any item in list |
.contains('col', val) | col @> val | Array contains or JSONB containment |
.containedBy('col', val) | col <@ val | Array or JSONB is contained by |
Single Row Handling & Result Modifiers
By default, .select() returns an array of records. Supabase provides modifiers to handle single-object retrieval:
// .single(): Expects exactly 1 row. Throws an error (PGRST116) if 0 or >1 rows return
const { data: profile, error } = await supabase
.from('profiles')
.select('*')
.eq('id', userId)
.single();
// .maybeSingle(): Returns null if 0 rows return, rather than throwing an error.
// Ideal for checking if a record exists!
const { data: invite, error: inviteErr } = await supabase
.from('team_invites')
.select('*')
.eq('token', inviteToken)
.maybeSingle();
Mutations: Atomic Upserts & Batch Inserts
// 1. Batch Insert
const { data: insertedRows, error: insertError } = await supabase
.from('audit_logs')
.insert([
{ event: 'LOGIN', user_id: '...', ip_address: '1.2.3.4' },
{ event: 'PAGE_VIEW', user_id: '...', ip_address: '1.2.3.4' }
])
.select(); // Calling .select() returns the newly created rows
// 2. Atomic UPSERT (Insert or Update on Conflict)
const { data: userPref, error: upsertError } = await supabase
.from('user_preferences')
.upsert(
{ user_id: 'user_uuid', theme: 'dark', notifications_enabled: true },
{ onConflict: 'user_id' } // Target column or constraint name
)
.select()
.single();
Exercises
- Write a query retrieving articles with tags containing both
'typescript'and'react'using the.contains()operator on an array column. - Build a paginated cursor query that uses
.gt('id', lastSeenId)instead of offset pagination to fetch the next 25 rows. - Compare the runtime behavior of
.single()versus.maybeSingle()when a query finds zero matching records.