queryPage method

  1. @override
Future<List> queryPage(
  1. String collectionId, {
  2. required Map<String, QueryValue> where,
  3. required Map<String, OrderDirection> orderBy,
  4. int? limit,
  5. int? offset,
  6. List<Object?>? startAfter,
})
override

Implementation

@override
Future<List> queryPage(String collectionId,
    {required Map<String, QueryValue> where,
    required Map<String, OrderDirection> orderBy,
    int? limit,
    int? offset,
    List<Object?>? startAfter}) async {
  logger.finest('queryPage');
  await initialize();
  assert(_db != null);
  CollectionReference collectionRef = _db!.collection(collectionId);
  Query<Object?> query = collectionRef;
  //Query<Object?> query = collectionRef.orderBy(orderBy, descending: descending);

  //for (var val in orderBy.entries) {
  orderBy.map((key, value) {
    query = query.orderBy(key, descending: (value == OrderDirection.descending));
    logger.finest('order by $key');
    return MapEntry(key, value);
  });

  where.map((mid, value) {
    //query = query.where(mid, isEqualTo: value);
    query = queryMaker(mid, value, query);
    return MapEntry(mid, value);
  });

  if (limit != null) query = query.limit(limit);
  if (startAfter != null && startAfter.isNotEmpty) query = query.startAfter(startAfter);

  return await query.get().then((snapshot) {
    if (snapshot.docs.isEmpty) {
      logger.finest('no data founded');
      return [];
    }
    return snapshot.docs.map((doc) {
      //logger.finest(doc.data()!.toString());
      if (doc.data() == null) {
        logger.finest('doc.data() is null');
        return [];
      }
      return doc.data()! as Map<String, dynamic>;
    }).toList();
  }, onError: (trace) {
    logger.severe("------------DATABASE ERROR -----------");
    logger.severe(trace.toString());
    return [];
  });
}