Fetch data: select()
Performs vertical filtering with SELECT.
const { data, error } = await supabase
.from('cities')
.select()
Parameters
columnsrequired
string
The columns to retrieve, separated by commas.
__namedParametersrequired
object
No description provided.
headrequired
boolean
When set to true, select will void data.
countrequired
null
|exact
|planned
|estimated
Count algorithm to use to count rows in a table.
Properties
Notes
- By default, Supabase projects will return a maximum of 1,000 rows. This setting can be changed in Project API Settings. It's recommended that you keep it low to limit the payload size of accidental or malicious requests. You can use
range()
queries to paginate through your data. select()
can be combined with Modifiersselect()
can be combined with Filters- If using the Supabase hosted platform
apikey
is technically a reserved keyword, since the API gateway will pluck it out for authentication. It should be avoided as a column name.
Examples
Getting your data
const { data, error } = await supabase
.from('cities')
.select()
Selecting specific columns
You can select specific fields from your tables.
const { data, error } = await supabase
.from('cities')
.select('name')
Query foreign tables
If your database has foreign key relationships, you can query related tables too.
const { data, error } = await supabase
.from('countries')
.select(`
name,
cities (
name
)
`)
What about join tables
If you're in a situation where your tables are NOT directly related, but instead are joined by a join table,
you can still use the select()
method to query the related data. The PostgREST engine detects the relationship automatically.
For more details, follow the link.
Query the same foreign table multiple times
Sometimes you will need to query the same foreign table twice. In this case, you can use the name of the joined column to identify which join you intend to use. For convenience, you can also give an alias for each column. For example, if we had a shop of products, and we wanted to get the supplier and the purchaser at the same time (both in the users) table:
const { data, error } = await supabase
.from('products')
.select(`
id,
supplier:supplier_id ( name ),
purchaser:purchaser_id ( name )
`)
Filtering with inner joins
If you want to filter a table based on a child table's values you can use the !inner()
function. For example, if you wanted
to select all rows in a message
table which belong to a user with the username
"Jane":
const { data, error } = await supabase
.from('messages')
.select('*, users!inner(*)')
.eq('users.username', 'Jane')
Querying with count option
You can get the number of rows by using the count option.
Allowed values for count option are null
, exact, planned and estimated.
const { data, error, count } = await supabase
.from('cities')
.select('name', { count: 'exact' }) // if you don't want to return any rows, you can use { count: 'exact', head: true }
Querying JSON data
If you have data inside of a JSONB column, you can apply select and query filters to the data values. Postgres offers a number of operators for querying JSON data. Also see PostgREST docs for more details.
const { data, error } = await supabase
.from('users')
.select(`
id, name,
address->street
`)
.eq('address->postcode', 90210)
Return data as CSV
By default the data is returned in JSON format, however you can also request for it to be returned as Comma Separated Values.
const { data, error } = await supabase
.from('users')
.select()
.csv()
Aborting requests in-flight
You can use an AbortController
to abort requests. Note that status
and statusText
doesn't mean anything for aborted requests, since the request wasn't actually fulfilled.
const ac = new AbortController()
supabase
.from('very_big_table')
.select()
.abortSignal(ac.signal)
.then(console.log)
ac.abort()
// {
// error: {
// message: 'FetchError: The user aborted a request.',
// details: '',
// hint: '',
// code: ''
// },
// data: null,
// body: null,
// count: null,
// status: 400,
// statusText: 'Bad Request'
// }