Skip to main content

RPC Calls

Call custom functions (Stored Procedures / Functions) in a PostgreSQL database via the JS SDK.

💡 Tip: RPC calls are based on the PostgREST protocol and can invoke any custom function created in PostgreSQL.

Initialize SDK

import cloudbase from "@cloudbase/js-sdk";

const app = cloudbase.init({
env: "your-env-id", // Replace with your environment ID
});

const db = app.rdb();

Basic Syntax

db.rpc(functionName, params, options);

Parameters

ParameterTypeRequiredDescription
functionNamestringYesName of the database function to call
paramsobjectNoParameters to pass to the function as key-value pairs
optionsobjectNoOptions configuration

options Parameter Details

ParameterTypeRequiredDescription
headbooleanNoIf true, data is not returned, only the count value (must be used with the count option)
countstringNoCount algorithm, options are "exact", "planned", or "estimated"
getbooleanNoIf true, uses HTTP GET to call the function (only for IMMUTABLE or STABLE read-only functions)

Create Functions

Before calling RPC, you need to create functions in the PostgreSQL database. You can execute SQL to create functions through the DMC database management tool.

-- Create a simple addition function
CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer)
RETURNS integer AS $$
SELECT a + b;
$$ LANGUAGE sql;

-- Create a function to search articles
CREATE OR REPLACE FUNCTION search_articles(keyword text)
RETURNS SETOF articles AS $$
SELECT * FROM articles WHERE title ILIKE '%' || keyword || '%';
$$ LANGUAGE sql;

-- Create a statistics function
CREATE OR REPLACE FUNCTION get_article_stats()
RETURNS json AS $$
SELECT json_build_object(
'total', COUNT(*),
'latest', MAX(published_at)
) FROM articles;
$$ LANGUAGE sql;

Code Examples

Call with Parameters

// Call the addition function
const { data, error } = await db.rpc("add_numbers", { a: 1, b: 2 });
console.log(data); // 3

Call without Parameters

// Call the statistics function
const { data, error } = await db.rpc("get_article_stats");
console.log(data);
// { total: 15, latest: "2026-04-01T00:00:00Z" }

Call Functions that Return Table Data

When a function returns SETOF <table>, the return value is in array format and supports chaining filters and modifiers.

// Search articles containing a keyword
const { data, error } = await db.rpc("search_articles", {
keyword: "CloudBase",
});

Filter and Sort Return Results

When a function returns table data, you can use filters and modifiers just like regular queries.

// Search articles, sort by publication time in descending order, limit to 5 results
const { data, error } = await db
.rpc("search_articles", { keyword: "CloudBase" })
.select("title, published_at")
.order("published_at", { ascending: false })
.limit(5);

Return a Single Record

// Get a single record
const { data, error } = await db
.rpc("search_articles", { keyword: "CloudBase" })
.limit(1)
.single();

Use Filters

// Apply filter conditions to RPC-returned table data
const { data, error } = await db
.rpc("search_articles", { keyword: "CloudBase" })
.eq("status", "published")
.gte("published_at", "2026-01-01");

Use GET Request for Read-Only Functions

For functions marked as IMMUTABLE or STABLE, you can use GET requests for better caching.

// Use GET request to call a read-only function
const { data, error } = await db.rpc(
"add_numbers",
{ a: 1, b: 2 },
{ get: true }
);

Get Row Count Statistics

// Only get row count, without returning data
const { count, error } = await db.rpc(
"search_articles",
{ keyword: "CloudBase" },
{ count: "exact", head: true }
);
console.log(count); // Returns the number of matching records

Function Parameter Types

PostgREST supports various PostgreSQL function parameter types:

Parameter TypeDescriptionJS SDK Example
integerInteger{ id: 1 }
text / varcharText{ name: "test" }
booleanBoolean{ active: true }
json / jsonbJSON Object{ data: { key: "value" } }
integer[]Integer Array{ ids: [1, 2, 3] }
text[]Text Array{ names: ["a", "b"] }