findWhere method

Future<List<T>> findWhere({
  1. Database? database,
  2. String? whereParameters,
  3. String? orderParameters,
  4. List<String>? fields,
  5. List<ColumnRelation>? relations,
})

Implementation

Future<List<T>> findWhere(
    {Database? database,
    String? whereParameters,
    String? orderParameters,
    List<String>? fields,
    List<ColumnRelation>? relations}) async {
  final Database db = database != null ? database : this.database;

  final dataResults = (await db.query(this.entity.tableName,
      where: whereParameters, orderBy: orderParameters, columns: fields));
  final List<String> dataIds =
      dataResults.map((dataResult) => dataResult['id'].toString()).toList();

  final relationData = relations != null
      ? await this.findRelationByParent(
          relations: relations,
          dataIds: dataIds,
          orderParameters: orderParameters,
          database: db)
      : [];

  return dataResults.map((e) {
    Map<String, dynamic> resultMap = {...e};

    (relationData ?? []).forEach((relationDataItem) {
      final availableRelationData = (relationDataItem['data'] ?? [])
          .where((relationItem) =>
              relationItem[relationDataItem['referencedColumn']] ==
              resultMap['id'])
          .toList();

      resultMap[relationDataItem['relation']] = availableRelationData;
    });
    return getObject<T>(resultMap);
  }).toList();
}