performDocumentRead method

  1. @override
Stream<Snapshot> performDocumentRead(
  1. DocumentReadRequest request
)
override

Implementation

@override
Stream<Snapshot> performDocumentRead(DocumentReadRequest request) async* {
  final document = request.document;
  final collection = document.parent;
  final schema = request.outputSchema ?? const ArbitraryTreeSchema();

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

  //
  // Send HTTP request
  //
  final response = await _httpRequest(
    'GET',
    '/$collectionId/_doc/$documentId',
  );

  //
  // Handle error
  //
  final error = response.error;
  if (error != null) {
    switch (error.type) {
      case 'index_not_found_exception':
        yield (null);
        return;
    }
    throw error;
  }

  switch (response.status) {
    case HttpStatus.ok:
      break;

    case HttpStatus.notFound:
      yield (Snapshot.notFound(request.document));
      return;

    default:
      throw DatabaseException.internal(
        message: 'Got HTTP status: ${response.status}',
      );
  }

  //
  // Return snapshot
  //
  final data = response.body['_source'];
  final decoder = JsonDecoder(database: collection.database);
  yield (Snapshot(
    document: request.document,
    versionId: response.body['_seq_no']?.toString(),
    data: schema.decodeWith(decoder, data),
  ));
}