order method

PostgrestTransformBuilder order(
  1. String column, {
  2. bool ascending = false,
  3. bool nullsFirst = false,
  4. String? foreignTable,
})

Orders the result with the specified column.

When options has ascending value true, the result will be in ascending order. When options has nullsFirst value true, nulls appear first. If column is a foreign column, the options need to have foreignTable value provided

postgrest.from('users').select().order('username', ascending: false)
postgrest.from('users').select('messages(*)').order('channel_id', foreignTable: 'messages', ascending: false)

Implementation

PostgrestTransformBuilder order(
  String column, {
  bool ascending = false,
  bool nullsFirst = false,
  String? foreignTable,
}) {
  final key = foreignTable == null ? 'order' : '$foreignTable.order';
  final existingOrder = url.queryParameters[key];
  final value = '${existingOrder == null ? '' : '$existingOrder,'}'
      '$column.${ascending ? 'asc' : 'desc'}.${nullsFirst ? 'nullsfirst' : 'nullslast'}';

  overrideSearchParams(key, value);
  return this;
}