Previously i used flutter sqflite package to build this kind of query where the user can search by name and also sort by balance or most recent (createdAt/updated at) like this
Future<List<CustomerModel>> fetchAllCustomer(String query, int sortID) async {
try {
final db = await DatabaseServices().database;
String orderByClause = '';
switch (sortID) {
case 0:
orderByClause = 'ORDER BY updated_at DESC';
break;
case 1:
orderByClause = 'ORDER BY CASE WHEN balance = 0 THEN 1 ELSE 0 END, balance DESC';
break;
case 2:
orderByClause = 'ORDER BY LOWER(name) ASC';
break;
case 3:
orderByClause = 'ORDER BY updated_at ASC';
break;
case 4:
orderByClause = 'ORDER BY CASE WHEN balance = 0 THEN 0 ELSE 1 END, balance ASC';
break;
default:
orderByClause = 'ORDER BY updated_at DESC';
}
final coss = await db.rawQuery('''
SELECT * FROM $tableName WHERE type = 0 AND branch_id = $branchID AND name LIKE "%$query%" $orderByClause
''');
return coss.map((e) => CustomerModel.fromMap(e)).toList();
} catch (e, st) {
LoggerManager.red('fetchAllCustomer err $e $st');
throw Exception(e);
}
}
for isar how can I achieve this if I have all the same fields for the isar db collection?