@@ -8,6 +8,16 @@ import 'package:ht_shared/ht_shared.dart';
88
99import '../../../_middleware.dart' ; // Assuming RequestId is here
1010
11+ /// Converts a camelCase string to snake_case.
12+ String _camelToSnake (String input) {
13+ return input
14+ .replaceAllMapped (
15+ RegExp (r'(?<!^)(?=[A-Z])' ),
16+ (match) => '_${match .group (0 )}' ,
17+ )
18+ .toLowerCase ();
19+ }
20+
1121/// Handles requests for the /api/v1/data collection endpoint.
1222/// Dispatches requests to specific handlers based on the HTTP method.
1323Future <Response > onRequest (RequestContext context) async {
@@ -109,10 +119,15 @@ Future<Response> _handleGet(
109119 final queryParams = context.request.uri.queryParameters;
110120 final startAfterId = queryParams['startAfterId' ];
111121 final limitParam = queryParams['limit' ];
112- final sortBy = queryParams['sortBy' ];
122+ final sortByParam = queryParams['sortBy' ];
113123 final sortOrderRaw = queryParams['sortOrder' ]? .toLowerCase ();
114124 final limit = limitParam != null ? int .tryParse (limitParam) : null ;
115125
126+ // Convert sortBy from camelCase to snake_case for the database query.
127+ // This prevents errors where the client sends 'createdAt' and the database
128+ // expects 'created_at'.
129+ final sortBy = sortByParam != null ? _camelToSnake (sortByParam) : null ;
130+
116131 SortOrder ? sortOrder;
117132 if (sortOrderRaw != null ) {
118133 if (sortOrderRaw == 'asc' ) {
0 commit comments