update method

DocRefMemory update(
  1. Map<String, dynamic> newDoc
)

Implementation

DocRefMemory update(
  Map<String, dynamic> newDoc,
) {
  String collId = _collRef.id;
  if (_memoryDb[collId] == null) {
    _memoryDb[collId] = [];
  }
  int index =
      _memoryDb[collId]!.indexWhere((element) => element[DBRKeys.id] == _id);
  if (index == -1) {
    return _collRef.insertDoc(newDoc);
  }
  var updatedOldDoc = _memoryDb[collId]![index];
  // the id of a doc can't be changed
  newDoc[DBRKeys.id] = updatedOldDoc[DBRKeys.id];
  newDoc.forEach((key, value) {
    updatedOldDoc[key] = value;
  });
  return set(updatedOldDoc);
}