queryDocuments method
Queries documents from the specified collection based on criteria
Implementation
@override
Future<List<Map<String, dynamic>>> queryDocuments(
String collection, {
required Map<String, dynamic> where,
int? limit,
String? orderBy,
bool descending = false,
}) async {
final connection = _requireConnection();
await _ensureCollection(collection);
final buffer = StringBuffer('SELECT id, data FROM ${_tableName(collection)}');
final parameters = <Object?>[];
if (where.isNotEmpty) {
parameters.add(TypedValue(Type.jsonb, where));
buffer.write(' WHERE data @> \$${parameters.length}');
}
final safeOrderBy = _sanitizeField(orderBy);
if (safeOrderBy != null) {
buffer.write(" ORDER BY data->>'$safeOrderBy' ${descending ? 'DESC' : 'ASC'}");
}
if (limit != null && limit > 0) {
buffer.write(' LIMIT $limit');
}
final result = await connection.execute(
Sql(buffer.toString()),
parameters: parameters,
);
return result.map((row) {
final id = row[0]?.toString() ?? '';
final data = _coerceMap(row[1]);
return {
...data,
'id': id,
};
}).toList();
}