selectFirstByQuery method

  1. @override
FutureOr<O?> selectFirstByQuery(
  1. String query, {
  2. Object? parameters,
  3. List? positionalParameters,
  4. Map<String, Object?>? namedParameters,
  5. Transaction? transaction,
  6. int? offset,
  7. int? page,
  8. bool? orderByID,
  9. OrderDirection? orderDirection,
  10. EntityResolutionRules? resolutionRules,
})
override

Ordering and pagination:

  • limit: the maximum number of returned entities. Also the page size of page.
  • offset: the return offset, for pagination.
  • page: the 1-based page to return, an ergonomic alternative to offset: it resolves to (page - 1) * limit. Requires a positive limit, and can't be combined with offset. See resolveSelectOffset.
  • orderByID: if true the result is ordered by the table ID column, automatically resolved from the table scheme (TableScheme.idFieldName) or from EntityHandler.idFieldName. Defaults to true when offset is defined, since a paginated select needs a stable order to be correct. Pass false to opt out.
  • orderDirection: the OrderDirection of the ordering, OrderDirection.ascending by default. Ignored while the ordering is not active (see orderByID).

Note that a query over a to-many relationship generates a JOIN without a DISTINCT, so it can return the same entity more than once. Paginating such a query is best-effort.

Implementation

@override
FutureOr<O?> selectFirstByQuery(
  String query, {
  Object? parameters,
  List? positionalParameters,
  Map<String, Object?>? namedParameters,
  Transaction? transaction,
  int? offset,
  int? page,
  bool? orderByID,
  OrderDirection? orderDirection,
  EntityResolutionRules? resolutionRules,
}) => selectByQuery(
  query,
  parameters: parameters,
  namedParameters: namedParameters,
  transaction: transaction,
  limit: 1,
  // The page size of a "first" select is 1, so `page: n` is the Nth entity:
  offset: resolveSelectOffset(page: page, offset: offset, limit: 1),
  orderByID: orderByID,
  orderDirection: orderDirection,
  resolutionRules: resolutionRules,
).resolveMapped((result) => result.firstOrNull);