Files
supabase/apps/reference/_supabase_js/generated/rpc.mdx
2022-08-18 15:54:30 +08:00

143 lines
3.1 KiB
Plaintext

---
id: rpc
title: 'Postgres functions: rpc()'
slug: /rpc
custom_edit_url: https://github.com/supabase/supabase/edit/master/spec/supabase_js_v2_legacy.yml
---
import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
You can call Postgres functions as a "Remote Procedure Call".
That's a fancy way of saying that you can put some logic into your database then call it from anywhere.
It's especially useful when the logic rarely changes - like password resets and updates.
```sql
create or replace function hello_world() returns text as $$
select 'Hello world';
$$ language sql;
```
```js
const { data, error } = await supabase.rpc('hello_world')
```
## Parameters
<ul className="method-list-group">
<li className="method-list-item">
<h4 className="method-list-item-label">
<span className="method-list-item-label-name">
fn
</span>
<span className="method-list-item-label-badge required">
required
</span>
<span className="method-list-item-validation">
<code>FunctionName</code>
</span>
</h4>
<div class="method-list-item-description">
The function name to call.
</div>
</li>
<li className="method-list-item">
<h4 className="method-list-item-label">
<span className="method-list-item-label-name">
args
</span>
<span className="method-list-item-label-badge required">
required
</span>
<span className="method-list-item-validation">
<code>object</code>
</span>
</h4>
<div class="method-list-item-description">
The parameters to pass to the function call.
</div>
</li>
<li className="method-list-item">
<h4 className="method-list-item-label">
<span className="method-list-item-label-name">
options
</span>
<span className="method-list-item-label-badge required">
required
</span>
<span className="method-list-item-validation">
<code>object</code>
</span>
</h4>
<div class="method-list-item-description">
Named parameters.
</div>
</li>
</ul>
## Examples
### Call a Postgres function
This is an example of invoking a Postgres function with no parameters.
```js
const { data, error } = await supabase.rpc('hello_world')
```
### With Parameters
```js
const { data, error } = await supabase.rpc('echo_city', { name: 'The Shire' })
```
### Bulk processing
You can process large payloads at once using [array parameters](https://postgrest.org/en/stable/api.html#calling-functions-with-array-parameters).
```js
const { data, error } = await postgrest.rpc('echo_cities', {
names: ['The Shire', 'Mordor'],
})
```
### With filters
Postgres functions that return tables can also be combined with
[Modifiers](/docs/reference/javascript/using-modifiers) and
[Filters](/docs/reference/javascript/using-filters).
```js
const { data, error } = await supabase
.rpc('echo_all_cities')
.select('name, population')
.eq('name', 'The Shire')
```
### With count option
You can specify a count option to get the row count along with your data.
Allowed values for count option are `null`, `exact`, `planned` and `estimated`.
```js
const { data, error, count } = await supabase.rpc(
'hello_world',
{},
{ count: 'exact' }
)
```