get method

  1. @override
Future<List<T>> get()
override

Executes and returns all matching rows.

Implementation

@override
Future<List<T>> get() async {
  _applyDefaultWithCountIfNeeded();
  final sql = grammar.compileSelect(_getQueryComponents());
  final result = await connection.execute(sql, bindings);

  List<T> items;
  if (modelFactory != null) {
    items = (result.data as List).map((row) => modelFactory!(row)).toList();
  } else {
    items = (result.data as List).cast<T>().toList();
  }

  // Eager Loading
  if (items.isNotEmpty && items.first is KhademModel) {
    final models = items.cast<KhademModel>();
    final firstModel = models.first;

    final relations = <dynamic>[];

    if (_withOnly.isNotEmpty) {
      relations.addAll(_withOnly);
    } else {
      // Add default relations
      relations.addAll(firstModel.withRelations);
      // Add requested relations
      relations.addAll(_with);
      // Remove excluded relations
      relations.removeWhere((r) {
        if (r is String) {
          final key = r.split(':').first.split('.').first;
          return _without.contains(r) || _without.contains(key);
        }
        if (r is With) {
          return _without.contains(r.relation);
        }
        if (r is Map) {
          return r.keys.any((k) => _without.contains(k.toString()));
        }
        return _without.contains(r.toString());
      });
    }

    if (relations.isNotEmpty) {
      await EagerLoader.loadRelations(models, relations);
    }
  }

  return items;
}