update method

  1. @override
Future<T?> update(
  1. String? correlationId,
  2. T? item
)

Updates a data item.

  • correlation_id (optional) transaction id to trace execution through call chain.
  • item an item to be updated. Return Future that receives updated item Throws error.

Implementation

@override
Future<T?> update(String? correlationId, T? item) async {
  if (item == null || item.id == null) {
    return null;
  }

  var jsonMap = convertFromPublic(item, createUid: false);
  jsonMap?.remove('_id');
  var filter = {'_id': item.id};
  var update = {r'$set': jsonMap};
  var result = await collection?.findAndModify(
      query: filter, update: update, returnNew: true, upsert: false);

  if (result != null) {
    logger.trace(correlationId, 'Updated in %s with id = %s',
        [collectionName, item.id]);

    return convertToPublic(result);
  }
  return null;
}