Skip to main content

Query data.

Initialize SDK

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

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

const db = app.rdb();

// Or specify an instance and database
// const db = app.rdb({
// instance: "<instance>",
// database: "<database>"
// });

Basic query

Query table data through the select() method, supporting conditional filtering, join queries, and other features.

db.from(tableName).select(columns, options)
  • tableName: Table Name
  • columns: Columns to retrieve, separated by commas
  • options: Query option configuration

Parameter Description

ParameterData typeRequiredDescription
columnsstringNoColumns to retrieve, separated by commas. Can rename column with customName:aliasName on return
optionsobjectNoQuery option configuration

options parameter description

ParameterData typeRequiredDescription
countstringNoCounting algorithm, available values: "exact" - underlying layer executes COUNT(*)
headbooleanNoSet to true to disable returned data, useful only when counting is required

Sample Code

Query All Data

// Query all data in the articles table
const { data, error } = await db.from("articles").select();

console.log('query result:', data);

Query specified columns

// Query only the title and createTime of the article
const { data, error } = await db.from("articles").select("title, created_at");

console.log('query result:', data);

Returned result

{
data: [
{
id: 1,
title: "Article title",
created_at: "2023-01-01T00:00:00Z"
},
// ... other records
],
error: null
}

Associated Table Query

Through join queries, you can obtain data from multiple tables simultaneously, supporting one-to-one and one-to-many association relationships.

Basic Join Query

Querying article data and getting associated classification information at the same time
const { data, error } = await db.from("articles").select(`
title,
categories (
name
)
`);

console.log('query result:', data);

Multi-layer Join Query

Query culture and entertainment, get creator and modifier info at the same time
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 the relationship between them.

Nested Association Query

Query category and ALL culture and entertainment, and author information
const { data, error } = await db.from("categories").select(`
name,
articles (
title,
users (
name
)
)
`);

console.log('query result:', data);

Advanced query

Conditional filtering query

Query specific category and ALL culture and entertainment
const { data, error } = await db
.from("articles")
.select("title, categories(*)")
.eq("categories.name", "technical article");

console.log('query result:', data);

Count queries

Retrieve each category and its number of articles
const { data, error } = await db
.from("categories")
.select(`*, articles(count)`);

console.log('query result:', data);

Get total count only

// Get total count only, do not return specific data
const { count, error } = await db
.from("articles")
.select("*", { count: "exact", head: true });

console.log('total count:', count);

Inner join query

// Only get articles with categories, use inner join to ensure category existence
const { data, error } = await db
.from("articles")
.select("title, categories!inner(name)")
.eq("categories.name", "tutorial")
.limit(10);

console.log('query result:', data);

💡 Note: !inner means inner join, only return data that has matching records in the associated table.

Documentation

  • Filter - Learn how to use filter conditions
  • Modifier - Learn how to use query modifiers