Repository
A typed repository per table, compiled to one PostgREST request.
const betterSupabase = createSupabase(schema);
const db = betterSupabase.connect(supabase); // any SupabaseClient, or an Executordb.<table> is created lazily for every table and view in the schema. Views
get the read methods only.
Reading
| Method | Returns |
|---|---|
list(args?) | Row[] |
page(args) | A page; see pagination |
get(key, args?) | Row, or a not_found error; Row | null with optional: true |
count({ where? }) | number (a HEAD request) |
aggregate(args) | Sums, averages and groups; see aggregates |
get takes the primary key, an object with any unique key (see
unique keys), or { where } for a filter
that should match at most one row but has no unique key behind it, such as
the active subscription of a customer. With a filter it asks for two rows and
returns a multiple_rows error (status 409) when it gets both. Pass
optional: true to get null instead of not_found, like supabase-js
maybeSingle(). For "the first row in some order", use list with
limit: 1. For "is there a row", use count and compare with zero.
Every read takes select, where, include, orderBy, limit, offset
and signal; get takes them as its second argument. limit and offset must be non-negative integers; anything
else returns invalid_request before a request is sent. The result type follows select and include:
const row = await db.customers
.get(id, {
select: ["name"],
include: { notes: { select: ["body"], limit: 3 } },
})
.orThrow();
// { name: string; notes: { body: string }[] }Composite keys take an object: db.customerTags.get({ customerId, tagId }).
Timeouts and retries
Every read, write and $rpc call also takes timeout and retry:
const rows = await db.customers.list({
where: { status: "lead" },
timeout: 2000,
retry: false,
});timeout is in milliseconds. It is combined with signal (either one aborts
the request), and a call that runs past it fails with timeout (504) instead
of aborted. retry turns supabase-js retries on or off for the call: they
repeat GET and HEAD requests after a network error, a 503 or a 520, and
never repeat a write. Set both for a whole connection with
betterSupabase.connect(client, context, { timeout, retry }); a call's own
values win.
On the Postgres executor, timeout only stops a call
that has not started its statement yet, so use statement_timeout for long
queries, and retry does nothing.
Escape hatches
db.$clientis the client you passed in.db.$storageis Storage as the same caller: thestorageoption ofconnect(client, context, { storage }), or the client'sstorage. Pass it tobucket.connect()and to blocks that takestorage, so Storage runs as whoever theDbruns as. It throws on a connection with neither, such as a bare executor.db.$rpc('fn', args, { returns })calls a typed Postgres function, and can validate the result with a Standard Schema. Rows of a table andreturns table (...)records come back in the configured casing;{ raw: true }keeps database names. An argument with a default is optional in the generated types and acceptsnullandundefined;$rpcleaves anundefinedargument out of the call, so the function default applies.db.$with({ signal })returns a Db bound to a new request context.db.$table(name)returns the repository for a table key known only at runtime, and throws aTypeErrorfor unknown names.db.$withoutPlugins()returns the same connection without any plugins, for migrations and admin scripts.db.$withoutPlugins({ keep: ['otel'] })keeps the named plugins, such as tracing.db.$run(spec)runs a serializable query spec.db.$many([specA, specB])runs specs together and returns a tuple;db.$many(readSet, params)runs a read set as one request.db.$stats()returns{ calls, waves, tables, ms }for everything thisdb(and its$withcopies) ran.betterSupabase.connect(client, context, { stats })also reports into a sharedStatsRecorder. See Budget.
The db of the browser client (createClient from better-supabase/client)
has the repositories, $rpc, $run, $table, $with, $withoutPlugins,
$storage and $buckets, and leaves out $many, $search, $stats,
$decode and $decodeReadSet, which keeps read-set batching and vector search
out of the bundle. The server, the adapters, createNativeClient and the testing
helpers have all of them.
explainPostgrest(op)shows the compiled request for debugging.
How queries compile
A call compiles to an intermediate representation, which a compiler turns
into a PostgREST request. Nothing runs through string templates you write:
values are quoted, and LIKE input used by contains, startsWith and
endsWith is escaped. A filter that can never match ({ in: [] }) skips
the request.
Last updated on