update method

  1. @override
Future<Map<String, dynamic>> update(
  1. Map<String, dynamic> map,
  2. DbPrincipal principal, {
  3. UpdatePolicy? updatePolicy,
  4. RepositoryTransaction? transaction,
})

Update an entity in the collection contained in the map parameter.

This method expects a principal parameter, which identifies the user performing the request. Implementations are advised to confirm if the entity being updated belongs to a tenant related to the principal.

If a non-empty value is passed to the permission parameter, the implementation of this method is expected to validate if the user represented by the principal does have the permission before actually updating the entity. Otherwise, an error is thrown.

Implementation

@override
Future<Map<String, dynamic>> update(
  Map<String, dynamic> map,
  DbPrincipal principal, {
  UpdatePolicy? updatePolicy,
  RepositoryTransaction? transaction,
}) async {
  var key = map['_key'];
  var rev = map['_rev'];

  var existing = await _get(
    key,
    principal,
    filterByTenant: updatePolicy?.filterByTenant ?? true,
    transaction: transaction,
  );

  updatePolicy ??= this.updatePolicy;
  _authorize(existing, principal, updatePolicy);

  handleMetaForUpdate(existing, map, principal, updatePolicy);

  final trx = _getDriverTransaction(transaction);
  try {
    final result = await db.updateDocument(
      collectionName,
      key,
      map,
      ifMatchRevision: rev,
      transaction: trx,
    );
    key = result.identifier.key;
    map = await db.getDocumentByKey(
      collectionName,
      key,
      transaction: trx,
    );

    return map;
  } on DbError catch (error) {
    throw error.toDbException();
  }
}