Skip to content

Updating multiple objects

This operation updates one or more objects in a database table. The objects to be updated can be identified with:

  1. objectId values
  2. A query string (a where clause)
  3. Result of a previously ran bulkCreate or object retrieval operation in the same transaction.

To add this operation to a Unit Of Work instance, use the following methods. The methods are split into categories based on how the the objects are identified in the API:

// Update the objects identified by the "identifier" argument. 
// The identifier can be either a whereClause, OpResult or list of the objectId values.
// The "changes" argument is a collection of key/value pairs.
// Each key in the collection must correspond to a column/property of the 
// updated objects.
unitOfWorkkInstance.bulkUpdate(Map changes, 
                               dynamic identifier, 
                               [String tableName])

Return Value

The operation returns an OpResult object which represents the result of this operation - number of objects updated in the database.

Example

Consider the example below. The example adds an operation to update multiple objects in the database. The change is to set the orderStatus property to "completed" for all orders which were delivered. This is the what the data table looks like before the transaction runs:

data-before-bulkupdate.zoom70

// compose a list of objects to be updated
// here we hard-code the objectId values,
// but in your code these values may be
// coming either from a query or from your
// data model on the client side.
final objectsForChanges = [
 "37ADFFA8-39E2-73A5-FF44-651EF5C4E900",
 "996E2034-4CC6-0BC4-FF01-E92077819700",
 "EB68A9E1-DC24-C961-FFC4-2EF9227BF700"];

// create a unit of work object
final unitOfWork = UnitOfWork();

// compose the changes object. The change it
// has it to update the "orderStatus" column
// with the value "completed"
Map changes = {
 "orderStatus": "completed",
};

// add the "bulkUpdate" operation to the transaction
unitOfWork.bulkUpdate(changes, objectsForChanges, "Order");

// run the transaction
unitOfWork.execute().then((result) => print("transaction complete - $result"));
// create a unit of work object
final unitOfWork = UnitOfWork();

// set the where clause. It will identify all
// objects where the "orderStatus" column has the
// value of "delivery pending".
final whereClause = "orderStatus = 'delivery pending'";

// compose the changes object. The change it
// has it to update the "orderStatus" column
// with the value "completed"
Map changes = {
 "orderStatus": "completed",
};
// add the "bulkUpdate" operation to the transaction
unitOfWork.bulkUpdate(changes, whereClause, "Order");

// run the transaction
unitOfWork.execute().then((result) => print("transaction complete - $result"));
// create a unit of work object
final unitOfWork = UnitOfWork();

// compose a query. It will identify all Order
// objects where the "orderStatus" column has the
// value of "delivery pending".
final queryBuilder = DataQueryBuilder();
queryBuilder.whereClause = "orderStatus = 'delivery pending'";
queryBuilder.pageSize = 100;
// fetch the order objects with the query.
final pendingOrdersResult = unitOfWork.find("Order", queryBuilder);

// compose the changes object. The change it
// has it to update the "orderStatus" column
// with the value "completed"
Map changes = {
 "orderStatus": "completed",
};

// add the "bulkUpdate" operation to the transaction
// apply the "changes" to the result of the "find" operation
unitOfWork.bulkUpdate(changes, pendingOrdersResult);

// run the transaction
unitOfWork.execute().then((result) => print("transaction complete - $result"));