Postgres 函数: rpc()
您可以将 Postgres 函数称为“远程过程调用”。
这是一种奇特的说法,您可以将一些逻辑放入数据库中,然后从任何地方调用它。当逻辑很少改变时,它特别有用——比如密码重置和更新。
const { data, error } = await supabase
.rpc('hello_world')
参数
-
fn required
string
要调用的函数名. -
params optional
undefined
|object
传递给函数调用的参数 -
已命名的参数 required
object
-
count required
null
|exact
|planned
|estimated
用于对表中的行进行计数的计数算法.
属性
例子
调用 Postgres 函数
这是调用 Postgres 函数的示例
const { data, error } = await supabase .rpc('hello_world')
带参数
const { data, error } = await supabase .rpc('echo_city', { name: 'The Shire' })
批量处理
您可以使用数组参数一次处理大型负载.
const { data, error } = await postgrest .rpc('echo_cities', { names: ['The Shire', 'Mordor'] })
带过滤器
返回表的 Postgres 函数也可以与 Modifiers 和 Filters结合使用.
const { data, error } = await supabase .rpc('echo_all_cities') .select('name, population') .eq('name', 'The Shire')
带 count 选项
您可以指定一个计数选项来获取行计数以及您的数据。对于count选项允许值是
null
,exact
,planned
andestimated
.const { data, error, count } = await supabase .rpc('hello_world', {}, { count: 'exact' })
-