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 models = app.models;

Single Query​

Query a single record by specified condition.

models.modelName.get(options);

-modelName: name of the data model -options: query parameter

options parameter description​

For specific query parameter explanations, refer to the Query Parameter Explanation document.

ParameterTypeRequiredDescription
filter.whereobjectNoQuery condition
selectobjectYesSpecify return fields

Sample Code​

Query the record with _id todo-id-123 in the todo data model
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);

Returned result​

{
data: {
_id: "todo-id-123",
title: "Server task",
completed: false,
// ... other field
}
}

Multiple Queries​

Query multiple records with support for filtering, sorting, and pagination.

models.modelName.list(options);
  • modelName: name of the data model -options: query parameter

options parameter description​

For specific query parameter explanations, refer to the Query Parameter Explanation document.

ParameterTypeRequiredDescription
filter.whereobjectNoQuery condition
selectobjectYesSpecify return fields

Sample Code​

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

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

Returned result​

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