performDocumentUpdate method

  1. @override
Future<void> performDocumentUpdate(
  1. DocumentUpdateRequest request
)
override

Updates by using read and upsert operations inside a transaction.

Implementation

@override
Future<void> performDocumentUpdate(DocumentUpdateRequest request) async {
  final document = request.document;
  final collection = document.parent;
  final schema = request.inputSchema ?? const ArbitraryTreeSchema();

  //
  // Validate IDs
  //
  final documentId = _validateDocumentId(document.documentId);
  final collectionId = _validateCollectionId(collection.collectionId);

  //
  // Check existence
  //
  final existsResponse = await _httpRequest(
    'HEAD',
    '/$collectionId/_doc/$documentId',
  );
  if (existsResponse.status != HttpStatus.ok) {
    throw DatabaseException.notFound(
      document,
      message: "can't update non-existing document",
      error: existsResponse.error,
    );
  }

  //
  // Send HTTP request
  //
  final json = schema.encodeWith(JsonEncoder(), request.data);
  final response = await _httpRequest(
    'PUT',
    '/$collectionId/_update/$documentId',
    queryParameters: {
      'if_primary_term': existsResponse.body['_primary_term'].toString(),
      'if_seq_no': existsResponse.body['_seq_no'].toString(),
    },
    json: json,
  );

  switch (response.status) {
    case HttpStatus.ok:
      return;
    default:
      throw response.error;
  }
}