Skip to main content

Update 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

Update Single Record

Update a single record by specified conditions.

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

options Parameter Description

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

ParameterTypeRequiredDescription
dataobjectRequiredData object to update
filter.whereobjectRequiredQuery condition that specifies the records to update

Code Example

// Update a single record
const result = await models.todo.update({
data: {
title: title: "Updated Task",
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 updating relationship fields is currently not supported.

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

options Parameter Description

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

ParameterTypeRequiredDescription
dataobjectRequiredData object to update
filter.whereobjectRequiredQuery condition that specifies the records to update

Code Example

// 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 adding or updating relationship fields is currently not supported.

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

options Parameter Description

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

ParameterTypeRequiredDescription
createobjectRequiredData object to be added
updateobjectRequiredData object to update
filter.whereobjectRequiredQuery condition that specifies the records to update

Code Example

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);