update method

Future<void> update(
  1. String collection, {
  2. required JsonMap update,
  3. required JsonMap filter,
  4. bool optimistic = false,
})

Updates documents in collection matching filter using MongoDB update operators from update.

Set optimistic to true to apply the update locally immediately while waiting for the server response.

Implementation

Future<void> update(
  String collection, {
  required JsonMap update,
  required JsonMap filter,
  bool optimistic = false,
}) async {
  final updatePayload = _deepCopyMap(update);
  final filterPayload = _deepCopyMap(filter);

  if (updatePayload.isEmpty) {
    throw ArgumentError('Update payload cannot be empty.');
  }

  if (optimistic) {
    _queryManager.optimisticUpdate(
      collection: collection,
      filter: filterPayload,
      update: updatePayload,
    );
  }

  await _webSocketService.send({
    'type': 'realtime:update',
    'collection': collection,
    'filter': filterPayload,
    'update': updatePayload,
  });
}