@@ -8,6 +8,16 @@ import 'package:ht_shared/ht_shared.dart';
8
8
9
9
import '../../../_middleware.dart' ; // Assuming RequestId is here
10
10
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
+
11
21
/// Handles requests for the /api/v1/data collection endpoint.
12
22
/// Dispatches requests to specific handlers based on the HTTP method.
13
23
Future <Response > onRequest (RequestContext context) async {
@@ -109,10 +119,15 @@ Future<Response> _handleGet(
109
119
final queryParams = context.request.uri.queryParameters;
110
120
final startAfterId = queryParams['startAfterId' ];
111
121
final limitParam = queryParams['limit' ];
112
- final sortBy = queryParams['sortBy' ];
122
+ final sortByParam = queryParams['sortBy' ];
113
123
final sortOrderRaw = queryParams['sortOrder' ]? .toLowerCase ();
114
124
final limit = limitParam != null ? int .tryParse (limitParam) : null ;
115
125
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
+
116
131
SortOrder ? sortOrder;
117
132
if (sortOrderRaw != null ) {
118
133
if (sortOrderRaw == 'asc' ) {
0 commit comments