diff --git a/Sources/CoreDataRepository/CoreDataRepository+Aggregate.swift b/Sources/CoreDataRepository/CoreDataRepository+Aggregate.swift index c24b650..9c2bb20 100644 --- a/Sources/CoreDataRepository/CoreDataRepository+Aggregate.swift +++ b/Sources/CoreDataRepository/CoreDataRepository+Aggregate.swift @@ -12,6 +12,38 @@ import Foundation // swiftlint:disable file_length extension CoreDataRepository { + public enum AggregateFunction: String { + case count + case sum + case average + case min + case max + } + + @inlinable + public func aggregate( + function: AggregateFunction, + predicate: NSPredicate, + entityDesc: NSEntityDescription, + attributeDesc: NSAttributeDescription, + groupBy: NSAttributeDescription? = nil, + as valueType: Value.Type + ) async -> Result { + switch function { + case .count: + return await count(predicate: predicate, entityDesc: entityDesc, as: valueType) + default: + return await Self.send( + function: .sum, + context: context, + predicate: predicate, + entityDesc: entityDesc, + attributeDesc: attributeDesc, + groupBy: groupBy + ) + } + } + // MARK: Count /// Get the count or quantity of managed object instances that satisfy the predicate. @@ -375,15 +407,6 @@ extension CoreDataRepository { // MARK: Internals - @usableFromInline - enum AggregateFunction: String { - case count - case sum - case average - case min - case max - } - private static func aggregate( context: NSManagedObjectContext, request: NSFetchRequest