getListByField method

  1. @override
Future<List<E>> getListByField(
  1. String fieldName,
  2. String fieldValue, {
  3. bool like = false,
  4. int offset = 0,
  5. int limit = 20,
  6. String? orderBy,
  7. SortDirection sortDirection = SortDirection.asc,
})
override

Perform a query with a where clause fieldName = fieldValue unles the like is true in which case we perform fieldName like fieldValue.

You can do pagination by passing offset and limit.

Sort the results by passing orderBy and sortDirection

Implementation

@override
Future<List<E>> getListByField(String fieldName, String fieldValue,
    {bool like = false,
    int offset = 0,
    int limit = 20,
    String? orderBy,
    SortDirection sortDirection = SortDirection.asc}) async {
  var sql = 'select * from ${getTablename()} ';

  final values = [fieldValue];

  if (like) {
    sql += 'where `$fieldName` like ? ';
  } else {
    sql += 'where `$fieldName` = ? ';
  }

  sql = appendTenantWhere(sql);
  appendTenantValue(values);

  if (orderBy != null) {
    sql += 'order by $orderBy ${sortDirection.name} ';
  }

  sql += 'limit $offset, $limit ';

  return query(sql, values);
}