updateById method

Future<Response<T>> updateById(
  1. String id,
  2. Map<String, dynamic> data, {
  3. DataFieldParams? params,
  4. bool? resolveRefs,
  5. Ignore? ignore,
  6. bool updateRefs = false,
})

Method to update data by ID with optional data source builder.

Example:

repository.updateById(
  'userId123',
  {'status': 'inactive'},
  params: Params({"field1": "value1", "field2": "value2"}),
);

Implementation

Future<Response<T>> updateById(
  String id,
  Map<String, dynamic> data, {
  DataFieldParams? params,
  bool? resolveRefs,
  Ignore? ignore,
  bool updateRefs = false,
}) async {
  if (id.isEmpty || data.isEmpty) return Response(status: Status.invalid);
  return execute(() {
    final path = ref(params, DataModifiers.updateById, id);
    data = data.map((k, v) => MapEntry(k, delegate.updatingFieldValue(v)));
    if (!isEncryptor) {
      return operation
          .update(path, data, updateRefs: updateRefs)
          .then((value) => Response(status: Status.ok));
    }
    return getById(
      id,
      params: params,
      countable: false,
      resolveRefs: resolveRefs ?? updateRefs,
      ignore: ignore,
    ).then((value) {
      final x = value.data?.filtered ?? {};
      x.addAll(data);
      return encryptor.input(x).then((v) {
        if (v.isEmpty) return Response(status: Status.nullable);
        return operation
            .update(path, v, updateRefs: updateRefs)
            .then((value) => Response(status: Status.ok));
      });
    });
  });
}