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
| Parameter | Type | Required | Description |
|---|---|---|---|
| values | object | Yes | The value to be updated |
| options | object | No | Update options |
options parameter description
| Parameter | Type | Required | Description |
|---|---|---|---|
| count | string | No | Counting 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