Fetch Data
Initialize the SDK
import cloudbase from "@cloudbase/node-sdk";
const app = cloudbase.init({
env: env: "your-env-id", // Replace with your environment id
});
const db = app.rdb();
// or specify the instance and database
// const db = app.rdb({
// instance: "<instance>",
// database: "<database>"
// });
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 search, separated by commas
- options: query options configuration
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| columns | string | No | Columns to search, separated by commas. You can rename columns in the output using customName:aliasName |
| 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 Example
Querying All Data
// Query all data from the articles table
const { data, error } = await db.from("articles").select();
console.log("Query result:", data);
Querying Specified Columns
// Query only the title and creation time of the articles
const { data, error } = await db.from("articles").select("title, created_at");
console.log("Query result:", data);
Response
{
data: [
{
id: 1,
title: title: "Article Title",
created_at: "2023-01-01T00:00:00Z"
},
// ... other records
],
error: null
}
Join Table Query
Join queries enable simultaneous data retrieval from multiple tables, supporting one-to-one, one-to-many, and other relationship types.
Basic Join Query
// Query article data and retrieve associated category information
const { data, error } = await db.from("articles").select(`
title,
categories (
name
)
`);
console.log("Query result:", data);
Multiple Join Query
// Query articles and obtain creator and modifier 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, use the foreign key constraint names to distinguish between the different associations.
Nested Join Query
// Query categories and all articles under them, along with the author information of the articles.
const { data, error } = await db.from("categories").select(`
name,
articles (
title,
users (
name
)
)
`);
console.log("Query result:", data);
Advanced Query
Conditional Filtering Query
// Query all articles under a specific category
const { data, error } = await db
.from("articles")
.select("title, categories(*)")
.eq("categories.name", "Technical Articles");
console.log("Query result:", data);
Count Query
// Obtain each category and its article count
const { data, error } = await db
.from("categories")
.select(`*, articles(count)`);
console.log("Query result:", data);
Obtain Only the Total Count
// Obtain only the total article count without returning specific data
const { count, error } = await db
.from("articles")
.select("*", { count: "exact", head: true });
console.log("Total:", count);
Inner Join Query
// Obtain only articles with categories, using inner join to ensure categories exist
const { data, error } = await db
.from("articles")
.select("title, categories!inner(name)")
.eq("categories.name", "Tutorial")
.limit(10);
console.log("Query result:", data);
💡 Note:
!innerrepresents an inner join, returning only data with matching records in the associated table.