queryCollection static method

Future<CollectionFrameResponse> queryCollection(
  1. P2PStream stream, {
  2. required PeerId ownerPeerId,
  3. required String path,
  4. Map<String, dynamic>? filter,
  5. String? sortField,
  6. bool? sortAsc,
  7. int? limit,
  8. int? offset,
  9. String? cursor,
  10. bool wantTotal = false,
})

Query a collection with JSONB filter.

The response body is {"items": [...]}. A filtered query reports CollectionFrameResponse.totalCount only when wantTotal is true, because the server must scan all matches to count them. To fetch the next page, pass the response's CollectionFrameResponse.nextCursor as cursor; a cursor takes precedence over offset.

Implementation

static Future<CollectionFrameResponse> queryCollection(
  P2PStream stream, {
  required PeerId ownerPeerId,
  required String path,
  Map<String, dynamic>? filter,
  String? sortField,
  bool? sortAsc,
  int? limit,
  int? offset,
  String? cursor,
  bool wantTotal = false,
}) async {
  try {
    final requestBytes = CollectionFrame.encodeRequest(
      operation: 'QUERY',
      ownerPeerId: ownerPeerId,
      path: path,
      filter: filter,
      sortField: sortField,
      sortAsc: sortAsc,
      limit: limit,
      offset: offset,
      cursor: cursor,
      wantTotal: wantTotal,
    );

    await _writeFrameStatic(stream, requestBytes);
    final responseBytes = await _readFrameStatic(stream);
    return CollectionFrame.decodeResponse(responseBytes);
  } catch (e, stackTrace) {
    _logger.severe('Error querying collection: $e', e, stackTrace);
    rethrow;
  }
}