Update Rows
You can update a single row or multiple rows in a table in the Catalyst Data Store, and update one more column values. The table reference used in the below code snippets can either be a table instance or the table meta.
Update a Single Row
This particular method allows you to update a single row by constructing a object with altered values in the required column. Refer the unique ROWID and pass the newly constructed Object to the updateRow() method. Here ROWID is a mandatory attribute. The promise returned here will be resolved to a JSON row object.
copy//Construct a JSON Object with the updated row details let updatedRowData = { Name: `Mathew Jones`, Age: 31, ROWID: 1510000000109474 }; //Use Table Meta Object to update a single row using ROWID which returns a promise let datastore = app.datastore(); let table = datastore.table('SampleTable'); let rowPromise = table.updateRow(updatedRowData); rowPromise.then((row) => { console.log(row); });
A sample response that you will receive is shown below. The response is the same for both versions of Node.js.
Node.js
copy{ CREATORID: "2136000000006003", MODIFIEDTIME: "2021-08-17 13:02:11:184", CREATEDTIME: "2021-08-16 16:29:10:499", Name: "Mathew Jones", Age: 31, ROWID: "2136000000011011" }
Update Multiple Rows
To update multiple rows, an array of objects is constructed containing modified values which is passed as an argument to the updateRows() method. ROWIDs are used in corresponding array objects to refer the specific rows which requires modification.
The promise returned here will be resolved to an array of row objects.
copy//Data to be updated along with the ROWID let updatedRowsData = [{ Name: `Mathew Jones`, Age: 31, ROWID: 1510000000113298 }, { Name: `Rhonda Watson`, Age: 28, ROWID: 1510000000109474 }]; //Use Table Meta Object to update a multiple rows using ROWIDs which returns a promise let datastore = app.datastore(); let table = datastore.table('SampleTable'); let rowPromise = table.updateRows(updatedRowsData); rowPromise.then((rows) => { console.log(rows); });
A sample response that you will receive is shown below. The response is the same for both versions of Node.js.
Node.js
copy[ { CREATORID: "2136000000006003", MODIFIEDTIME: "2021-08-24 13:22:14:718", CREATEDTIME: "2021-08-24 13:12:55:999", Name: "Mathew Jones", Age: 31, ROWID: "2136000000034043" }, { CREATORID: "2136000000006003", MODIFIEDTIME: "2021-08-24 13:22:14:728", CREATEDTIME: "2021-08-24 13:12:56:001", Name: "Rhonda Watson", Age: 28, ROWID: "2136000000034045" } ]
Last Updated 2023-09-03 01:06:41 +0530 +0530
Yes
No
Send your feedback to us