Fetch Data
Initialize the 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 Queries
Query table data through the select() method, supporting features such as conditional filtering and join queries.
db.from(tableName).select(columns, options)
- tableName: table name
- columns: columns to retrieve, separated by commas
- options: query options configuration
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| columns | string | No | Columns to retrieve, separated by commas. Supports using customName:aliasName to rename columns when returning |
| options | object | No | Query options configuration |
options Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| count | string | No | Counting algorithm, optional values: "exact" - underlying execution of COUNT(*) |
| head | boolean | No | When set to true, no data is returned. This is only useful for counting |
Code Examples
Query All Data
// Query all data from the articles table
const { data, error } = await db.from("articles").select();
console.log('Query result:', data);
Query Specific Columns
// Only query article titles and creation times
const { data, error } = await db.from("articles").select("title, created_at");
console.log('Query result:', data);
Return Result
{
data: [
{
id: 1,
title: "Article Title",
created_at: "2023-01-01T00:00:00Z"
},
// ... other records
],
error: null
}
Join Table Queries
Join queries allow you to retrieve data from multiple tables simultaneously, supporting one-to-one, one-to-many, and other relationships.
Basic Join Query
// Query article data while fetching associated category information
const { data, error } = await db.from("articles").select(`
title,
categories (
name
)
`);
console.log('Query result:', data);
Multiple Join Query
// Query articles while fetching creator and updater information
const { data, error } = await db.from("articles").select(`
title,
created_by:users!articles_created_by_fkey(name),
updated_by:users!articles_updated_by_fkey(name)
`);
console.log('Query result:', data);
Note: When the same table is associated multiple times through different foreign keys, you need to use the foreign key constraint name to distinguish different relationships.
Nested Join Query
// Query categories with all their articles, and the author information for each article
const { data, error } = await db.from("categories").select(`
name,
articles (
title,
users (
name
)
)
`);
console.log('Query result:', data);
Advanced Queries
Conditional Filtering Query
// Query all articles under a specific category
const { data, error } = await db
.from("articles")
.select("title, categories(*)")
.eq("categories.name", "Tech Articles");
console.log('Query result:', data);
Count Query
// Get each category and its article count
const { data, error } = await db
.from("categories")
.select(`*, articles(count)`);
console.log('Query result:', data);
Get Count Only
// Only get the total article count without returning actual data
const { count, error } = await db
.from("articles")
.select("*", { count: "exact", head: true });
console.log('Total count:', count);
Inner Join Query
// Only get articles that have a category, using inner join to ensure the category exists
const { data, error } = await db
.from("articles")
.select("title, categories!inner(name)")
.eq("categories.name", "Tutorial")
.limit(10);
console.log('Query result:', data);
Note:
!innerindicates an inner join, which only returns data that has matching records in the related table.