Skip to content

Commit ee4e8f3

Browse files
committed
chore: add new aggregate functions and expression methods
1 parent 7dcf7bc commit ee4e8f3

File tree

9 files changed

+1471
-0
lines changed

9 files changed

+1471
-0
lines changed

packages/cloud_firestore/cloud_firestore/lib/src/pipeline_aggregate.dart

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,94 @@ class Maximum extends PipelineAggregateFunction {
170170
}
171171
}
172172

173+
/// Aggregate that picks the first value of [expression] across pipeline inputs.
174+
///
175+
/// Use with [Pipeline.aggregate] or [AggregateStageOptions]. Corresponds to the
176+
/// Firestore pipeline `first` accumulator.
177+
class First extends PipelineAggregateFunction {
178+
final Expression expression;
179+
180+
First(this.expression);
181+
182+
@override
183+
String get name => 'first';
184+
185+
@override
186+
Map<String, dynamic> toMap() {
187+
final map = super.toMap();
188+
map['args'] = {
189+
'expression': expression.toMap(),
190+
};
191+
return map;
192+
}
193+
}
194+
195+
/// Aggregate that picks the last value of [expression] across pipeline inputs.
196+
///
197+
/// Use with [Pipeline.aggregate] or [AggregateStageOptions]. Corresponds to the
198+
/// Firestore pipeline `last` accumulator.
199+
class Last extends PipelineAggregateFunction {
200+
final Expression expression;
201+
202+
Last(this.expression);
203+
204+
@override
205+
String get name => 'last';
206+
207+
@override
208+
Map<String, dynamic> toMap() {
209+
final map = super.toMap();
210+
map['args'] = {
211+
'expression': expression.toMap(),
212+
};
213+
return map;
214+
}
215+
}
216+
217+
/// Aggregate that collects all values of [expression] into an array.
218+
///
219+
/// Order of elements is not guaranteed. Absent values become `null` in the result.
220+
/// Corresponds to the Firestore pipeline `array_agg` accumulator.
221+
class ArrayAgg extends PipelineAggregateFunction {
222+
final Expression expression;
223+
224+
ArrayAgg(this.expression);
225+
226+
@override
227+
String get name => 'array_agg';
228+
229+
@override
230+
Map<String, dynamic> toMap() {
231+
final map = super.toMap();
232+
map['args'] = {
233+
'expression': expression.toMap(),
234+
};
235+
return map;
236+
}
237+
}
238+
239+
/// Aggregate that collects distinct values of [expression] into an array.
240+
///
241+
/// Order of elements is not guaranteed. Corresponds to the Firestore pipeline
242+
/// `array_agg_distinct` accumulator.
243+
class ArrayAggDistinct extends PipelineAggregateFunction {
244+
final Expression expression;
245+
246+
ArrayAggDistinct(this.expression);
247+
248+
@override
249+
String get name => 'array_agg_distinct';
250+
251+
@override
252+
Map<String, dynamic> toMap() {
253+
final map = super.toMap();
254+
map['args'] = {
255+
'expression': expression.toMap(),
256+
};
257+
return map;
258+
}
259+
}
260+
173261
/// Represents an aggregate stage with functions and optional grouping
174262
class AggregateStageOptions implements PipelineSerializable {
175263
final List<AliasedAggregateFunction> accumulators;

0 commit comments

Comments
 (0)