@@ -37,6 +37,7 @@ import { getMetadataArgsStorage } from "../globals"
37
37
import { UpsertOptions } from "../repository/UpsertOptions"
38
38
import { InstanceChecker } from "../util/InstanceChecker"
39
39
import { ObjectLiteral } from "../common/ObjectLiteral"
40
+ import { PickKeysByType } from "../common/PickKeysByType"
40
41
41
42
/**
42
43
* Entity manager supposed to work with any entity, automatically find its repository and call its methods,
@@ -1001,6 +1002,69 @@ export class EntityManager {
1001
1002
. getCount ( )
1002
1003
}
1003
1004
1005
+ /**
1006
+ * Return the SUM of a column
1007
+ */
1008
+ sum < Entity extends ObjectLiteral > (
1009
+ entityClass : EntityTarget < Entity > ,
1010
+ columnName : PickKeysByType < Entity , number > ,
1011
+ where ?: FindOptionsWhere < Entity > | FindOptionsWhere < Entity > [ ] ,
1012
+ ) : Promise < number | null > {
1013
+ return this . callAggregateFun ( entityClass , "SUM" , columnName , where )
1014
+ }
1015
+
1016
+ /**
1017
+ * Return the AVG of a column
1018
+ */
1019
+ average < Entity extends ObjectLiteral > (
1020
+ entityClass : EntityTarget < Entity > ,
1021
+ columnName : PickKeysByType < Entity , number > ,
1022
+ where ?: FindOptionsWhere < Entity > | FindOptionsWhere < Entity > [ ] ,
1023
+ ) : Promise < number | null > {
1024
+ return this . callAggregateFun ( entityClass , "AVG" , columnName , where )
1025
+ }
1026
+
1027
+ /**
1028
+ * Return the MIN of a column
1029
+ */
1030
+ minimum < Entity extends ObjectLiteral > (
1031
+ entityClass : EntityTarget < Entity > ,
1032
+ columnName : PickKeysByType < Entity , number > ,
1033
+ where ?: FindOptionsWhere < Entity > | FindOptionsWhere < Entity > [ ] ,
1034
+ ) : Promise < number | null > {
1035
+ return this . callAggregateFun ( entityClass , "MIN" , columnName , where )
1036
+ }
1037
+
1038
+ /**
1039
+ * Return the MAX of a column
1040
+ */
1041
+ maximum < Entity extends ObjectLiteral > (
1042
+ entityClass : EntityTarget < Entity > ,
1043
+ columnName : PickKeysByType < Entity , number > ,
1044
+ where ?: FindOptionsWhere < Entity > | FindOptionsWhere < Entity > [ ] ,
1045
+ ) : Promise < number | null > {
1046
+ return this . callAggregateFun ( entityClass , "MAX" , columnName , where )
1047
+ }
1048
+
1049
+ private async callAggregateFun < Entity extends ObjectLiteral > (
1050
+ entityClass : EntityTarget < Entity > ,
1051
+ fnName : "SUM" | "AVG" | "MIN" | "MAX" ,
1052
+ columnName : PickKeysByType < Entity , number > ,
1053
+ where : FindOptionsWhere < Entity > | FindOptionsWhere < Entity > [ ] = { } ,
1054
+ ) : Promise < number | null > {
1055
+ const metadata = this . connection . getMetadata ( entityClass )
1056
+ const result = await this . createQueryBuilder ( entityClass , metadata . name )
1057
+ . setFindOptions ( { where } )
1058
+ . select (
1059
+ `${ fnName } (${ this . connection . driver . escape (
1060
+ String ( columnName ) ,
1061
+ ) } )`,
1062
+ fnName ,
1063
+ )
1064
+ . getRawOne ( )
1065
+ return result [ fnName ] === null ? null : parseFloat ( result [ fnName ] )
1066
+ }
1067
+
1004
1068
/**
1005
1069
* Finds entities that match given find options.
1006
1070
*/
0 commit comments