Skip to main content

Fetch Data

Initialize the SDK

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

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

const models = app.models

Querying a Single Record

Query a single record by specified criteria.

models.modelName.get(options);
  • modelName: data model name
  • options: query parameters

options Parameter Description

For specific query parameter description, see the Query Parameter Details document.

ParameterTypeRequiredDescription
filter.whereobjectNoQuery conditions
selectobjectYesSpecifies the fields to return

Code Example

// Query the record with _id "todo-id-123" in the todoDataModel
const todo = await models.todo.get({
filter: {
where: {
_id: {
$eq: "todo-id-123",
},
},
},
select: {
$master: true, // Return all fields
},
});

console.log("Query result:", todo.data);

Response

{
data: {
records: [{
_id: "todo-id-123",
title: title: "Server-side Task",
completed: false,
// ... other fields
}],
total: 1
}
}

Querying Multiple Records

Querying multiple records supports conditional filtering, sorting, pagination, and more.

models.modelName.list(options);
  • modelName: data model name
  • options: query parameters

options Parameter Description

For specific query parameter description, see the Query Parameter Details document.

ParameterTypeRequiredDescription
filter.whereobjectNoQuery conditions
selectobjectYesSpecifies the fields to return

Code Example

// Query records where completed is false in the todo data model
const todos = await models.todo.list({
filter: {
where: {
completed: {
$eq: false,
},
},
},
});

console.log("Query result:", todos.data);

Response

{
data: {
records: [{
_id: "todo-id-1",
title: title: "Task 1",
completed: false
},
{
_id: "todo-id-2",
title: title: "Task 2",
completed: true
}
],
total: 2
}
}