Files
supabase/web/docs/library/get.mdx
T

285 lines
5.0 KiB
Plaintext

---
id: get
title: 'Reading'
---
import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
import DummyData from '../common/DummyData.mdx'
import CommonResponses from '../common/CommonResponses.mdx'
import CommonFilters from '../common/CommonFilters.mdx'
import Collapsable from '../../src/components/Collapsable'
We will be using these tables as reference for our examples:
<DummyData />
### Getting your data
This example shows how to get all the countries in our database:
<!-- prettier-ignore -->
```js {7-8}
import { createClient } from '@supabase/supabase-js'
const supabase = createClient('https://world.supabase.io', '1a2b-3c4d-5e6f-7g8h')
const getCities = async () => {
try {
let cities = await supabase
.get('cities')
return cities
} catch (error) {
console.log('Error: ', error)
}
}
```
<Collapsable title="Toggle Example Response">
Calling `getCities()` will return the following response:
```json
[
{ "name": "Rio de Janeiro", "country_id": 76 },
{ "name": "Beijing", "country_id": 156 },
{ "name": "Paris", "country_id": 250 },
{ "name": "Auckland", "country_id": 554 },
{ "name": "Lagos", "country_id": 556 },
{ "name": "San Francisco", "country_id": 840 }
]
```
</Collapsable>
### Getting specific columns
This example shows how to get all cities but only return the name:
<!-- prettier-ignore -->
```js {9}
import { createClient } from '@supabase/supabase-js'
const supabase = createClient('https://world.supabase.io', '1a2b-3c4d-5e6f-7g8h')
const getCities = async () => {
try {
let cities = await supabase
.get('cities')
.select('name')
return cities
} catch (error) {
console.log('Error: ', error)
}
}
```
<Collapsable title="Toggle Example Response">
Calling `getCities()` will return the following response:
```json
[
{ "name": "Rio de Janeiro" },
{ "name": "Beijing" },
{ "name": "Paris" },
{ "name": "Auckland" },
{ "name": "Lagos" },
{ "name": "San Francisco" }
]
```
</Collapsable>
### Query foreign tables
This example shows how to get all countries and the cities their cities:
<!-- prettier-ignore -->
```js {9-14}
import { createClient } from '@supabase/supabase-js'
const supabase = createClient('https://world.supabase.io', '1a2b-3c4d-5e6f-7g8h')
const getCountries = async () => {
try {
let countries = await supabase
.get('countries')
.select(`
name,
cities {
name
}
`)
return countries
} catch (error) {
console.log('Error: ', error)
}
}
```
<Collapsable title="Toggle Example Response">
Calling `getCountries()` will return the following response:
```json
[
{
"name": "Brazil",
"cities": [
{ "name": "Rio de Janeiro" }
]
},
{
"name": "China",
"cities": [
{ "name": "Beijing" }
]
},
{
"name": "France",
"cities": [
{ "name": "Paris" }
]
},
{
"name": "New Zealand",
"cities": [
{ "name": "Auckland" }
]
},
{
"name": "Nigeria",
"cities": [
{ "name": "Lagos" }
]
},
{
"name": "United States",
"cities": [
{ "name": "San Francisco" }
]
}
]
```
</Collapsable>
## Reference
### `get()`
<!-- prettier-ignore -->
```js {2}
supabase
.get(tableName)
```
##### tableName `:string`
Name of table in the database that will be read from.
----
### `select()`
<!-- prettier-ignore -->
```js {3}
supabase
.get(tableName)
.select(columnQuery)
```
##### columnQuery `:string`
A comma separated list of columns. For example `select('id, name')`
If a foreign key constraint exists between this table
and another, you can also query the columns of the related table. For example `select('name, cities {name}')`
<Collapsable title="Show detailed example">
<!-- prettier-ignore -->
```js
supabase
.get('countries')
.select(`
name,
cities {
name,
population
}
`)
```
</Collapsable>
----
### `order()`
<!-- prettier-ignore -->
```js {3}
supabase
.get(tableName)
.order(columnName, sortAscending, nullsFirst)
```
Orders your data before fetching.
##### columnName `:string`
Name of chosen column to base the order on.
##### sortAscending `:boolean? | Default is false`
Specifies whether the order will be ascending or descending.
##### nullsFirst `:boolean? | Default is false`
Specifies whether null values will be displayed first.
----
### `range()`
<!-- prettier-ignore -->
```js {3}
supabase
.get(tableName)
.range(fromIndex, toIndex)
```
Paginates your request.
##### fromIndex `:integer`
Index or position of the start of the specified range.
##### toIndex `:integer?`
Index or position of the end of the specified range. If not stated, all remaining rows after the starting index will be returned.
----
### `single()`
<!-- prettier-ignore -->
```js {3}
supabase
.get(tableName)
.single()
```
Returns the first row of the table as an object and **not** as an array.
----
## Filtering
<CommonFilters verb="get" />
## Responses
#### 200 OK
Successful request
<CommonResponses />