PostgrestQueryBuilder
import { PostgrestQueryBuilder } from "https://esm.sh/@supabase/postgrest-js@2.99.2/dist/index.d.mts";§Type Parameters
§Constructors
Creates a query builder scoped to a Postgres table or view.
import PostgrestQueryBuilder from '@supabase/postgrest-js'
const query = new PostgrestQueryBuilder(
new URL('https://xyzcompany.supabase.co/rest/v1/users'),
{ headers: { apikey: 'public-anon-key' } }
)
§Properties
§Methods
Perform a DELETE on the table or view.
By default, deleted rows are not returned. To return it, chain the call
with .select() after filters.
- Named parameters
- Count algorithm to use to count deleted rows.
"exact": Exact but slow count algorithm. Performs a COUNT(*) under the
hood.
"planned": Approximated but fast count algorithm. Uses the Postgres
statistics under the hood.
"estimated": Uses exact count for low numbers and planned count for high
numbers.
Perform a SELECT query on the table or view.
- The columns to retrieve, separated by commas. Columns can be renamed when returned with
customName:columnName
- Named parameters
- When set to
true,datawill not be returned. Useful if you only need the count.
- Count algorithm to use to count rows in the table or view.
"exact": Exact but slow count algorithm. Performs a COUNT(*) under the
hood.
"planned": Approximated but fast count algorithm. Uses the Postgres
statistics under the hood.
"estimated": Uses exact count for low numbers and planned count for high
numbers.
Getting your data
const { data, error } = await supabase
.from('characters')
.select()
Selecting specific columns
const { data, error } = await supabase
.from('characters')
.select('name')
Query referenced tables
const { data, error } = await supabase
.from('orchestral_sections')
.select(`
name,
instruments (
name
)
`)
Query referenced tables with spaces in their names
const { data, error } = await supabase
.from('orchestral sections')
.select(`
name,
"musical instruments" (
name
)
`)
Query referenced tables through a join table
const { data, error } = await supabase
.from('users')
.select(`
name,
teams (
name
)
`)
Query the same referenced table multiple times
const { data, error } = await supabase
.from('messages')
.select(`
content,
from:sender_id(name),
to:receiver_id(name)
`)
// To infer types, use the name of the table (in this case `users`) and
// the name of the foreign key constraint.
const { data, error } = await supabase
.from('messages')
.select(`
content,
from:users!messages_sender_id_fkey(name),
to:users!messages_receiver_id_fkey(name)
`)
Query nested foreign tables through a join table
const { data, error } = await supabase
.from('games')
.select(`
game_id:id,
away_team:teams!games_away_team_fkey (
users (
id,
name
)
)
`)
Filtering through referenced tables
const { data, error } = await supabase
.from('instruments')
.select('name, orchestral_sections(*)')
.eq('orchestral_sections.name', 'percussion')
Querying referenced table with count
const { data, error } = await supabase
.from('orchestral_sections')
.select(`*, instruments(count)`)
Querying with count option
const { count, error } = await supabase
.from('characters')
.select('*', { count: 'exact', head: true })
Querying JSON data
const { data, error } = await supabase
.from('users')
.select(`
id, name,
address->city
`)
Querying referenced table with inner join
const { data, error } = await supabase
.from('instruments')
.select('name, orchestral_sections!inner(name)')
.eq('orchestral_sections.name', 'woodwinds')
.limit(1)
Switching schemas per query
const { data, error } = await supabase
.schema('myschema')
.from('mytable')
.select()
Perform an UPDATE on the table or view.
By default, updated rows are not returned. To return it, chain the call
with .select() after filters.
- The values to update with
- Named parameters
- Count algorithm to use to count updated rows.
"exact": Exact but slow count algorithm. Performs a COUNT(*) under the
hood.
"planned": Approximated but fast count algorithm. Uses the Postgres
statistics under the hood.
"estimated": Uses exact count for low numbers and planned count for high
numbers.
Updating your data
const { error } = await supabase
.from('instruments')
.update({ name: 'piano' })
.eq('id', 1)
Update a record and return it
const { data, error } = await supabase
.from('instruments')
.update({ name: 'piano' })
.eq('id', 1)
.select()
Updating JSON data
const { data, error } = await supabase
.from('users')
.update({
address: {
street: 'Melrose Place',
postcode: 90210
}
})
.eq('address->postcode', 90210)
.select()