|
1 | 1 | part of '../../parse_server_sdk.dart';
|
2 | 2 |
|
| 3 | +/// A class that allows aggregation queries on a Parse Server class using a pipeline. |
| 4 | +/// |
| 5 | +/// Example usage: |
| 6 | +/// ```dart |
| 7 | +/// final aggregate = ParseAggregate('GameScore', pipeline: { |
| 8 | +/// '\$group': { |
| 9 | +/// '_id': '\$userId', |
| 10 | +/// 'totalScore': {'\$sum': '\$score'} |
| 11 | +/// } |
| 12 | +/// }); |
| 13 | +/// final response = await aggregate.execute(); |
| 14 | +/// ``` |
3 | 15 | class ParseAggregate {
|
| 16 | + /// The name of the Parse class to perform the aggregation on. |
4 | 17 | final String className;
|
| 18 | + |
| 19 | + /// The aggregation pipeline operations. |
| 20 | + /// |
| 21 | + /// Each operation should follow MongoDB-like syntax. |
| 22 | + /// Example: |
| 23 | + /// ```dart |
| 24 | + /// { |
| 25 | + /// '\$group': { |
| 26 | + /// '_id': '\$userId', |
| 27 | + /// 'totalScore': {'\$sum': '\$score'} |
| 28 | + /// } |
| 29 | + /// } |
| 30 | + /// ``` |
5 | 31 | Map<String, dynamic> pipeline;
|
| 32 | + |
| 33 | + /// Whether to enable debug mode for this request. |
6 | 34 | final bool? debug;
|
| 35 | + |
| 36 | + /// The custom ParseClient to use for the request (optional). |
7 | 37 | final ParseClient? client;
|
| 38 | + |
| 39 | + /// If true, includes the session ID automatically in the request (optional). |
8 | 40 | final bool? autoSendSessionId;
|
| 41 | + |
| 42 | + /// Optional override for the Parse class name used in response handling. |
9 | 43 | final String? parseClassName;
|
10 | 44 |
|
11 |
| - ParseAggregate(this.className,{required this.pipeline,this.debug, this.client, this.autoSendSessionId, this.parseClassName}); |
| 45 | + /// Creates a new [ParseAggregate] instance to perform aggregation queries. |
| 46 | + /// |
| 47 | + /// [className] is required and specifies the target Parse class. |
| 48 | + /// [pipeline] must contain at least one aggregation operation. |
| 49 | + ParseAggregate( |
| 50 | + this.className, { |
| 51 | + required this.pipeline, |
| 52 | + this.debug, |
| 53 | + this.client, |
| 54 | + this.autoSendSessionId, |
| 55 | + this.parseClassName, |
| 56 | + }); |
12 | 57 |
|
| 58 | + /// Executes the aggregation query using the configured pipeline. |
| 59 | + /// |
| 60 | + /// Returns a [ParseResponse] containing the results of the aggregation. |
| 61 | + /// Throws [ArgumentError] if the pipeline is empty. |
13 | 62 | Future<ParseResponse> execute() async {
|
14 |
| - Map<String,String> _pipeline={}; |
15 |
| - if(pipeline.isEmpty){ |
16 |
| - throw ArgumentError('pipeline must not be empty. Please add pipeline operations to aggregate data. Example: {"\$group": {"_id": "\$userId", "totalScore": {"\$sum": "\$score"}}} '); |
17 |
| - } |
18 |
| - else{ |
19 |
| - _pipeline.addAll({'pipeline':jsonEncode(pipeline.entries.map((e) => {e.key: e.value}).toList())}); |
| 63 | + Map<String, String> _pipeline = {}; |
| 64 | + |
| 65 | + if (pipeline.isEmpty) { |
| 66 | + throw ArgumentError( |
| 67 | + 'pipeline must not be empty. Please add pipeline operations to aggregate data. ' |
| 68 | + 'Example: {"\$group": {"_id": "\$userId", "totalScore": {"\$sum": "\$score"}}}', |
| 69 | + ); |
| 70 | + } else { |
| 71 | + _pipeline.addAll({ |
| 72 | + 'pipeline': jsonEncode(pipeline.entries.map((e) => {e.key: e.value}).toList()) |
| 73 | + }); |
20 | 74 | }
|
| 75 | + |
21 | 76 | final debugBool = isDebugEnabled(objectLevelDebug: debug);
|
22 | 77 | final result = await ParseObject(className)._client.get(
|
23 |
| - Uri.parse('${ParseCoreData().serverUrl}$keyEndPointAggregate$className').replace(queryParameters: _pipeline).toString(), |
24 |
| - ); |
25 |
| - print('result >>> ${result.data}'); |
| 78 | + Uri.parse('${ParseCoreData().serverUrl}$keyEndPointAggregate$className').replace( |
| 79 | + queryParameters: {'pipeline': jsonEncode(pipeline.entries.map((e) => {e.key: e.value}).toList())} |
| 80 | + ).toString(), |
| 81 | + ); |
| 82 | + |
| 83 | + |
26 | 84 | return handleResponse<ParseObject>(
|
27 | 85 | ParseObject(className),
|
28 | 86 | result,
|
|
0 commit comments