Deleting multiple objects¶
This operation deletes multiple objects from the database. To add this operation to a Unit Of Work instance, use the following methods. The methods are organized by how the objects to be deleted are identified:
// Delete multiple objects identified by the "value" argument.
// The value can be either a whereClause, OpResult or list of the objectId/Map/Custom class object values.
unitOfWorkInstance.bulkDelete<T>(T value, [String tableName]);
Return Value¶
All methods listed above return an instance of the OpResult
class which represents the number of objects deleted in the database. The returned OpResult
object can be used as an argument in other operations in the same transaction (same UnitOfWork
instance). For more information see the Operation Result chapter of this guide.
Example¶
The examples below demonstrate the usage of the operation for all possible ways to reference the object that should be deleted:
final unitOfWork = UnitOfWork();
// compose the array of objectId's to delete
final objectsToDeleteArray = [
"5220681E-AB99-D53C-FF6C-C97ED0C65D00",
"C2937EA2-182A-49BC-FFDD-3A34B41CF200",
"B4E120E5-1990-22B2-FF80-AD0AE9ACB100"];
// add the bulkDelete operation to transaction
unitOfWork.bulkDelete(objectsToDeleteArray, "Order");
// run the transaction
unitOfWork.execute().then((result) => print("transaction complete - $result"));
final unitOfWork = UnitOfWork();
// here we construct map objects and populate them with
// the objectId values. It is the only required property
// for the deletion operation. Instead of creating these
// objects, they could be coming from the server from
// another API call.
final order1 = {
"objectId": "D426CE1C-ACAF-B352-FF7D-4881EF280700"
};
final order2 = {
"objectId": "81556273-55A4-0C8C-FF85-6A32EC017B00"
};
final order3 = {
"objectId": "0442109E-15A8-3609-FFFE-D53ECF2A6300"
};
// create a collection of map objects
final ordersToDelete = [order1, order2, order3];
// add the bulkDelete operation to the transaction
unitOfWork.bulkDelete(ordersToDelete, "Order");
// run the transaction
unitOfWork.execute().then((result) => print("transaction complete - $result"));
final unitOfWork = UnitOfWork();
// here we construct Order objects and populate them with
// the objectId values. It is the only required property
// for the deletion operation. Instead of creating these
// objects, they could be coming from the server from
// another API call.
final order1 = Order()
..objectId = "1D0A453F-BE4E-ADE0-FFDB-8815368EB400";
final order2 = Order()
..objectId = "F176163C-9F1D-BFEB-FF0C-99F311735400";
final order3 = Order()
..objectId = "668BF86B-14F6-B6A9-FF5E-BE999C497900";
// create a collection of Order objects
final ordersToDelete = [order1, order2, order3];
// add the bulkDelete operation to the transaction
unitOfWork.bulkDelete(ordersToDelete);
// run the transaction
unitOfWork.execute().then((result) => print("transaction complete - $result"));
// create unit of work - it represents the transaction
final unitOfWork = UnitOfWork();
// add delete operation to the transaction.
// The first argument is the name of the table - "Order"
// The second argument is a query used to identify the
// order which should be deleted. Any order that has
// the orderStatus set to "invalid", will be deleted
// by this operation
unitOfWork.bulkDelete("orderStatus = 'invalid'", "Order");
// run the transaction
unitOfWork.execute().then((result) => print("transaction complete - $result"));
// create unit of work - it represents the transaction
final unitOfWork = UnitOfWork();
// compose a query. It will fetch all objects which
// have orderStatus set to "invalid".
final queryBuilder = DataQueryBuilder();
queryBuilder.whereClause = "orderStatus = 'invalid'";
// add the find operation to the transaction.
// Notice the result is stored in the "invalidOrder" variable
final invalidOrders = unitOfWork.find("Order", queryBuilder);
// add delete operation to the transaction.
// Notice the argument is the result from the previous operation
// in the transaction
unitOfWork.bulkDelete(invalidOrders);
// run the transaction
unitOfWork.execute().then((result) => print("transaction complete - $result"));