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 db = app.rdb();
// or specify the instance and database
// const db = app.rdb({
// instance: "<instance>",
// database: "<database>"
// });
Update Data
To update data in the table using the update() method, you need to use a filter to locate the rows to be updated.
db.from(tableName).update(values, options).filter();
- tableName: table name
- values: The data to be updated
- options: update options configuration
Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| values | object | Required | Values to update |
| options | object | No | update options |
options Parameter Description
| Parameter | Type | Required | Description |
|---|---|---|---|
| count | string | No | Counting algorithm used to calculate the number of updated rows: "exact" - underlying execution of COUNT(*) |
Code Example
Update Data
// Update the record with id 1 in the articles table, changing the title field to "new title"
const { error } = await db
.from("articles")
.update({ title: "New Title" })
.eq("id", 1);
console.log("Update result:", error ? "failed" : "succeeded");
Batch Update
// Update all articles where status is 'draft' to set status to 'published'
const { error } = await db
.from("articles")
.update({ status: "published" })
.eq("status", "draft");
console.log("Batch update result:", error ? "failed" : "succeeded");
Response
{
data: null,
error: null
}
💡 Note: The
update()method must be used in conjunction with filters to locate the rows you wish to update.
Related Documentation
- Filter - Learn how to use filter conditions