Files
supabase/web/docs/library/get.mdx
T
Angelico 293587272d Update Documentation
The following changes were done:
- Add responses for subscribe.mdx, post.mdx, patch.mdx, delete.mdx, stored-procedures.mdx.
- Clean up of repeated information.
2020-01-28 15:44:39 +08:00

286 lines
5.5 KiB
Plaintext

---
id: get
title: 'Reading'
description: 'How to fetch records from your database with Supabase'
---
import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
import CommonFilters from '../common/CommonFilters'
import CommonResponses from '../common/CommonResponses.mdx'
import Collapsable from '../../src/components/Collapsable'
import FromFunction from '../common/FromFunction.mdx'
import DummyData from '../common/DummyData.mdx'
import CustomCodeBlock from '../../src/components/CustomCodeBlock'
import { getSimpleJs, getColumnsJs, getForeignJs, getForeignMultipleJs } from './getExamples'
<DummyData />
### Getting your data
<CustomCodeBlock
header="Shows how to get all countries from our public 'world' database"
js={getSimpleJs}
jsHighlight="{7-9}"
response={`{
body: [
{ "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 }
],
status: 200,
statusCode: 200,
statusText: "OK"
}`}
/>
### Getting specific columns
<CustomCodeBlock
header="Shows how to get all cities but only return the name"
js={getColumnsJs}
jsHighlight="{9}"
response={`{
body: [
{ "name": "Rio de Janeiro" },
{ "name": "Beijing" },
{ "name": "Paris" },
{ "name": "Auckland" },
{ "name": "Lagos" },
{ "name": "San Francisco" }
],
status: 200,
statusCode: 200,
statusText: "OK"
}`}
/>
### Query foreign tables
<CustomCodeBlock
header="Shows how to get all countries and the name of each of their cities"
js={getForeignJs}
jsHighlight="{9-14}"
response={`{
body: [
{
"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" }
]
}
],
status: 200,
statusCode: 200,
statusText: "OK"
}`}
/>
### 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:
<CustomCodeBlock
header="Shows how to reference a foreign table when it is referenced twice"
js={getForeignMultipleJs}
jsHighlight="{9-13}"
response={`{
body: [
{
"id": 1,
"purchaser": { "name": "John" },
"supplier": { "name": "Jane" }
},
{
"id": 2,
"purchaser": { "name": "Sue" },
"supplier": { "name": "Steve" }
}
],
status: 200,
statusCode: 200,
statusText: "OK"
}`}
/>
## Reference
### `from()`
<FromFunction verb='get' />
### `select()`
<!-- prettier-ignore -->
```js {3}
supabase
.from(tableName)
.select(columnQuery)
```
If not used, all columns in the table will be returned.
#### `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
.from('countries')
.select(`
name,
cities {
name,
population
}
`)
```
</Collapsable>
----
### `order()`
<!-- prettier-ignore -->
```js {3}
supabase
.from(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
.from(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
.from(tableName)
.single()
```
Returns the first row of the table as an object and **not** as an array.
----
## Filtering
### `filter()`
<CommonFilters filter='filter'/>
### `match()`
<CommonFilters filter='match'/>
### `eq()`
<CommonFilters filter='eq'/>
### `gt()`
<CommonFilters filter='gt'/>
### `lt()`
<CommonFilters filter='lt'/>
### `gte()`
<CommonFilters filter='gte'/>
### `lte()`
<CommonFilters filter='lte'/>
### `like()`
<CommonFilters filter='like'/>
### `ilike()`
<CommonFilters filter='ilike'/>
### `is()`
<CommonFilters filter='is'/>
### `in()`
<CommonFilters filter='in'/>
### `not()`
<CommonFilters filter='not'/>
## Responses
#### 200 OK
Successful request
<CommonResponses />