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 db = app.rdb();

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

Update the data

Update the data in the table through the update() method, and combine it with a filter to locate the rows to be updated.

db.from(tableName).update(values, options).filter()
  • tableName: table name.
  • values: data to be updated
  • options: Update option configuration.

Parameter description

ParameterTypeRequiredDescription
valuesobjectYesThe value to be updated
optionsobjectNoUpdate options

options parameter description

ParameterTypeRequiredDescription
countstringNoCounting algorithm used to calculate number of rows: "exact" - underlying layer executes COUNT(*)

Sample Code

Update data

// Update the record with id 1 in the articles table, change the title field to "new title"
const { error } = await db
.from("articles")
.update({ title: "new title" })
.eq("id", 1);

console.log('update result:', error ? 'fail' : 'success');

Batch Update

// Update all articles with status draft to status published
const { error } = await db
.from("articles")
.update({ status: "published" })
.eq("status", "draft");

console.log('Batch update result:', error ? 'fail' : 'success');

Returned result

{
data: null,
error: null
}

💡 Note: The update() method must be used in conjunction with the filter to locate the rows you want to update.

Documentation

-Filter - Learn how to use filter criteria