Supabase Client Filtered Data Query
Query a Supabase table with typed client filters, range pagination, upsert conflicts, and realtime subscription cleanup in one flow.
Code
import { createClient } from '@supabase/supabase-js';
const supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_ANON_KEY,
{ persistSession: false },
);
const { data: active, error } = await supabase
.from('projects')
.select('id, name, owner_id, updated_at')
.eq('status', 'active')
.gte('updated_at', '2026-10-01')
.order('updated_at', { ascending: false })
.range(0, 19);
if (error) throw new Error(error.message);
const { data: upserted } = await supabase
.from('projects')
.upsert({ id: 77, name: 'pipeline', status: 'active' }, { onConflict: 'id' })
.select();
const channel = supabase
.channel('projects-changes')
.on('postgres_changes', { event: '*', schema: 'public', table: 'projects' },
(payload) => console.log('change:', payload.event))
.subscribe();
await new Promise((r) => setTimeout(r, 1000));
supabase.removeChannel(channel);
console.log('active projects:', active.length, 'upserted:', upserted.length);How it works
Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed. createClient are the only symbols borrowed from @supabase/supabase-js — the coupling is explicit and narrow. Instead of silently continuing, the code raises Error at the guard.
Top-level bindings supabase, channel hold the demo state — nothing sits inside a function scope.
Configuration comes from the environment — it reads SUPABASE_URL, SUPABASE_ANON_KEY, so the same code switches behavior between environments without edits.
What to watch for
- Requires
@supabase/supabase-jsas a package; the snippet will not run without it. - The guard
errorshort-circuits the flow with an earlythrowwhen its condition holds.
Putting it to use
The run prints change:, active projects: — each prefix names the step that produced it.
Core lines
await new Promise((r) => setTimeout(r, 1000));
const { data: active, error } = await supabase
.from('projects')
.select('id, name, owner_id, updated_at')
.eq('status', 'active')
.gte('updated_at', '2026-10-01')
.order('updated_at', { ascending: false })
.range(0, 19);
if (error) throw new Error(error.message);
Interactive Tools for This Topic
FAQ
- What happens if the preconditions do not hold?
- `Error` is raised at the guard (`error`), so invalid state never reaches the main logic.
- Do I need to install anything before running this?
- Yes: `@supabase/supabase-js` must be present in the project or the import fails at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.