JavaScript: 调用Postgres函数

执行函数调用。

您可以将Postgres函数作为_远程过程调用_使用,这是可以在任何地方执行的数据库逻辑。 当逻辑很少变化时(如密码重置和更新),函数非常有用。

create or replace function hello_world() returns text as $$
  select 'Hello world';
$$ language sql;

要在读取副本上调用Postgres函数,请使用get: true选项。

Parameters

Examples

调用无参数的 Postgres 函数

const { data, error } = await supabase.rpc('hello_world')

调用带参数的 Postgres 函数

const { data, error } = await supabase.rpc('echo', { say: '👋' })

批量处理

const { data, error } = await supabase.rpc('add_one_each', { arr: [1, 2, 3] })

调用带过滤条件的Postgres函数

const { data, error } = await supabase
  .rpc('list_stored_countries')
  .eq('id', 1)
  .single()

调用只读的Postgres函数

const { data, error } = await supabase.rpc('hello_world', { get: true })