Skip to main content

Update the 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-entry update

Update a single record based on conditions.

models.modelName.update(options);
  • modelName: name of the data model
  • options: update parameters

options parameter description

For specific query parameter explanations, refer to the query parameter explanation document.

ParameterTypeRequiredDescription
dataobjectYesData object to be updated
filter.whereobjectYesQuery condition to identify the record to update

Sample Code

// Update a single record
const result = await models.todo.update({
data: {
title: "Tasks after update"
completed: true,
updatedAt: new Date(),
updatedBy: "admin",
},
filter: {
where: {
_id: {
$eq: "todo-id", // It is recommended to use _id for precise operations
},
},
},
});

console.log("update result:", result);

Batch Update

Batch update multiple records based on query conditions.

⚠️ Note: Batch update of association relationship fields is not supported.

models.modelName.updateMany(options);
  • modelName: name of the data model
  • options: update parameter

options parameter description

For specific query parameter explanations, refer to the query parameter explanation document.

ParameterTypeRequiredDescription
dataobjectYesData object to be updated
filter.whereobjectYesQuery condition to identify records to update

Sample Code

Batch update multiple records
const result = await models.todo.updateMany({
data: {
status: "archived",
archivedAt: new Date(),
},
filter: {
where: {
completed: {
$eq: true,
},
createdAt: {
$lt: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000), // 30 days ago
},
},
},
});

console.log("batch update result:", result);

Create or update data

Create or update records based on query conditions.

⚠️ Note: Batch queries not supported for adding new association relationship fields.

models.modelName.upsert(options);
  • modelName: name of the data model.
  • options: update parameters

options parameter description

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

ParameterTypeRequiredDescription
createobjectYesNew data object to add
updateobjectYesData object to be updated
filter.whereobjectYesQuery condition to identify the record to update

Sample Code

const post = {
title: "Hello World",
body: "Hello World",
_id: "foo",
};
const { data } = await models.post.upsert({
create: post,
update: post,
filter: {
where: {
_id: {
$eq: post._id,
},
},
},
});
console.log(data);