getFirstWhere<T extends Table, D> method

  1. @override
Future<D?> getFirstWhere<T extends Table, D>(
  1. List<Expression<bool>> conditions, {
  2. bool andLogic = true,
})
override

Implementation

@override
Future<D?> getFirstWhere<T extends Table, D>(
  List<Expression<bool>> conditions, {
  bool andLogic = true,
}) async {
  try {
    final table = _getTable<T, D>();
    Expression<bool>? combinedCondition;

    for (final condition in conditions) {
      if (combinedCondition == null) {
        combinedCondition = condition;
      } else {
        combinedCondition = andLogic
            ? combinedCondition & condition
            : combinedCondition | condition;
      }
    }

    if (combinedCondition == null) {
      return await (_database.select(table)..limit(1)).getSingleOrNull();
    }

    return await (_database.select(table)
          ..where((_) => combinedCondition!)
          ..limit(1))
        .getSingleOrNull();
  } catch (e) {
    throw DatabaseBridgeException(error: e);
  }
}