findRelations method

Future<List?> findRelations({
  1. Database? database,
  2. List<ColumnRelation>? relations,
  3. dynamic data,
})

Implementation

Future<List?> findRelations(
    {Database? database,
    List<ColumnRelation>? relations,
    dynamic data}) async {
  if (relations == null || relations.length == 0 || data == null) {
    return null;
  }

  return Future.wait(relations.map((relation) async {
    final referencedValue = data[
        relation.relationType == RelationType.OneToMany
            ? relation.primaryKey
            : relation.attributeName];
    final Database db = database != null ? database : this.database;

    final whereParameters =
        '${relation.referencedColumn} = "$referencedValue"';

    final relationResult = (await db.query(
            relation.referencedEntity?.tableName as String,
            where: whereParameters))
        .toList();

    Map<String, dynamic> resultMap = {};
    resultMap['attribute'] = relation.attributeName;

    final dataResult = (relation.relationType == RelationType.ManyToOne
        ? relationResult.length > 0
            ? relationResult[0]
            : null
        : relationResult);

    resultMap['data'] = dataResult;

    return resultMap;
  }));
}