runsOnServer property

bool get runsOnServer

Whether the database runs this query as is, without a composite index: only equality-like filters, or filters and sorts that all name one field (sorting by id ascending is free). Combinations Firestore refuses (two array filters, not-in with !=, in or another not-in) count as not runnable, since the app can answer them.

Implementation

bool get runsOnServer {
  int count(WhereOperator operator) => filters.where((f) => f.operator == operator).length;
  final arrayFilters = count(WhereOperator.arrayContains) + count(WhereOperator.arrayContainsAny);
  final notIn = count(WhereOperator.notInArray);
  if (arrayFilters > 1 || notIn > 1) return false;
  if (notIn > 0 &&
      count(WhereOperator.notEqual) + count(WhereOperator.inArray) + count(WhereOperator.arrayContainsAny) > 0) {
    return false;
  }
  final sorts = [
    for (final order in orderBy)
      if (order.field != cloudDocumentIdField || order.descending) order,
  ];
  final hasInequality = filters.any((f) => f.isInequality);
  if (!hasInequality && sorts.isEmpty) return true;
  final fields = {for (final f in filters) f.field, for (final o in sorts) o.field};
  return fields.length <= 1;
}