modify method

  1. @override
Future<Map<String, dynamic>> modify(
  1. String id,
  2. Map<String, dynamic> data, [
  3. Map<String, dynamic>? params
])

Merges data into the document with id. A patch cannot create a document, so a missing id is a 404.

Implementation

@override
Future<Map<String, dynamic>> modify(
  String id,
  data, [
  Map<String, dynamic>? params,
]) async {
  var oid = _makeId(id);
  var current = await _findRaw(oid);

  // `id` is left out: it is only added for clients, and older versions
  // stored it by mistake.
  var updatedDoc = mergeMap([current, _removeSensitive(data)])
    ..remove('id')
    ..['createdAt'] = current['createdAt']
    ..['updatedAt'] = DateTime.now().toIso8601String();
  if (updatedDoc['createdAt'] == null) updatedDoc.remove('createdAt');

  try {
    var modified = await collection.findAndModify(
      query: where.id(oid),
      update: updatedDoc,
      returnNew: true,
    );
    if (modified == null) {
      throw AngelHttpException.notFound(
        message: 'No record found for ID $id',
      );
    }
    return _jsonify(modified, params);
  } on AngelHttpException {
    rethrow;
  } catch (e, st) {
    throw AngelHttpException(error: e, stackTrace: st);
  }
}