Skip to content

Commit 742aa43

Browse files
authored
Merge pull request #28 from roanutil/feature/make-internal-aggregate-endpoint-public
Add a unified aggregate endpoint
2 parents ca4d404 + 06df31f commit 742aa43

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

Sources/CoreDataRepository/CoreDataRepository+Aggregate.swift

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,38 @@ import Foundation
1212
// swiftlint:disable file_length
1313

1414
extension CoreDataRepository {
15+
public enum AggregateFunction: String {
16+
case count
17+
case sum
18+
case average
19+
case min
20+
case max
21+
}
22+
23+
@inlinable
24+
public func aggregate<Value: Numeric>(
25+
function: AggregateFunction,
26+
predicate: NSPredicate,
27+
entityDesc: NSEntityDescription,
28+
attributeDesc: NSAttributeDescription,
29+
groupBy: NSAttributeDescription? = nil,
30+
as valueType: Value.Type
31+
) async -> Result<Value, CoreDataError> {
32+
switch function {
33+
case .count:
34+
return await count(predicate: predicate, entityDesc: entityDesc, as: valueType)
35+
default:
36+
return await Self.send(
37+
function: .sum,
38+
context: context,
39+
predicate: predicate,
40+
entityDesc: entityDesc,
41+
attributeDesc: attributeDesc,
42+
groupBy: groupBy
43+
)
44+
}
45+
}
46+
1547
// MARK: Count
1648

1749
/// Get the count or quantity of managed object instances that satisfy the predicate.
@@ -375,15 +407,6 @@ extension CoreDataRepository {
375407

376408
// MARK: Internals
377409

378-
@usableFromInline
379-
enum AggregateFunction: String {
380-
case count
381-
case sum
382-
case average
383-
case min
384-
case max
385-
}
386-
387410
private static func aggregate<Value: Numeric>(
388411
context: NSManagedObjectContext,
389412
request: NSFetchRequest<NSDictionary>

0 commit comments

Comments
 (0)