Deleting a single object¶
Important
This operation deletes a single object in the database. To delete more than one object, use the bulkDelete operation documented in the Deleting multiple objects chapter of this guide.
To add this operation to a Unit Of Work instance, use the following methods. The methods are organized by how the object to be deleted is identified:
// Delete the object represented as a Map, objectId, OpResult,
// OpResultValueReference or custom class instance.
unitOfWorkInstance.delete<T>(T value, [String tableName]);
Return Value¶
All methods return an instance of the OpResult
class which represents the timestamp when the object was deleted from the database. The 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:
// create unit of work - it represents the transaction
final unitOfWork = UnitOfWork();
// create a map-based object.
// the only required property is objectId.
// in your application the object may come from
// an API used outside of the transaction, or you
// can compose it the same way as shown below
final orderObject = {
"objectId": "EB68A9E1-DC24-C961-FFC4-2EF9227BF700",
};
// add delete operation to the transaction
unitOfWork.delete(orderObject, "Order");
// run the transaction
unitOfWork.execute().then((result) => print("transaction complete - $result"));
// create unit of work - it represents the transaction
final unitOfWork = UnitOfWork();
// create a class-based object.
// the only required property is objectId.
// in your application the object may come from
// an API used outside of the transaction, or you
// can compose it the same way as shown below
final order = Order()
..objectId = "996E2034-4CC6-0BC4-FF01-E92077819700";
// add delete operation to the transaction
unitOfWork.delete(order);
// run the transaction
unitOfWork.execute().then((result) => print("transaction complete - $result"));
@reflector
class Order {
String orderStatus;
DateTime deliveryDate;
String objectId;
}
// create unit of work - it represents the transaction
final unitOfWork = UnitOfWork();
// objectId of the object to delete.
final objectId = "37ADFFA8-39E2-73A5-FF44-651EF5C4E900";
// add delete operation to the transaction
unitOfWork.delete(objectId, "Order");
// run the transaction
unitOfWork.execute().then((result) => print("transaction complete - $result"));
// create unit of work - it represents the transaction
final unitOfWork = UnitOfWork();
final queryBuilder = DataQueryBuilder();
queryBuilder.whereClause = "orderStatus = 'invalid'";
final invalidOrders = unitOfWork.find("Order", queryBuilder);
final firstInvalidOrder = invalidOrders.resolveTo(resultIndex: 0);
// add delete operation to the transaction
unitOfWork.delete(firstInvalidOrder);
// run the transaction
unitOfWork.execute().then((result) => print("transaction complete - $result"));