-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(aggregations): Add aggregations interfaces
- Loading branch information
1 parent
7de81a7
commit d67e733
Showing
16 changed files
with
248 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { AggregateQuery, AggregateResponse, NumberAggregate } from '../interfaces'; | ||
import { QueryFieldMap } from './query.helpers'; | ||
|
||
const convertAggregateQueryFields = <From, To>( | ||
fieldMap: QueryFieldMap<From, To>, | ||
fields?: (keyof From)[], | ||
): (keyof To)[] | undefined => { | ||
if (!fields) { | ||
return fields; | ||
} | ||
return fields.map((fromField) => { | ||
const otherKey = fieldMap[fromField]; | ||
if (!otherKey) { | ||
throw new Error(`No corresponding field found for '${fromField as string}' when transforming aggregateQuery`); | ||
} | ||
return otherKey as keyof To; | ||
}); | ||
}; | ||
|
||
const convertAggregateNumberFields = <From, To>( | ||
fieldMap: QueryFieldMap<From, To>, | ||
response?: NumberAggregate<From>, | ||
): NumberAggregate<To> | undefined => { | ||
if (!response) { | ||
return response; | ||
} | ||
return Object.keys(response).reduce((toResponse, fromField) => { | ||
const otherKey = fieldMap[fromField as keyof From] as keyof To; | ||
if (!otherKey) { | ||
throw new Error(`No corresponding field found for '${fromField}' when transforming aggregateQuery`); | ||
} | ||
return { ...toResponse, [otherKey]: response[fromField as keyof From] }; | ||
}, {} as Record<keyof To, number>); | ||
}; | ||
|
||
const convertAggregateFields = <From, To>( | ||
fieldMap: QueryFieldMap<From, To>, | ||
response?: Partial<From>, | ||
): Partial<To> | undefined => { | ||
if (!response) { | ||
return response; | ||
} | ||
return Object.keys(response).reduce((toResponse, fromField) => { | ||
const otherKey = fieldMap[fromField as keyof From] as keyof To; | ||
if (!otherKey) { | ||
throw new Error(`No corresponding field found for '${fromField}' when transforming aggregateQuery`); | ||
} | ||
return { ...toResponse, [otherKey]: response[fromField as keyof From] }; | ||
}, {} as Partial<To>); | ||
}; | ||
|
||
export const transformAggregateQuery = <From, To>( | ||
query: AggregateQuery<From>, | ||
fieldMap: QueryFieldMap<From, To>, | ||
): AggregateQuery<To> => { | ||
return { | ||
count: convertAggregateQueryFields(fieldMap, query.count), | ||
sum: convertAggregateQueryFields(fieldMap, query.sum), | ||
avg: convertAggregateQueryFields(fieldMap, query.avg), | ||
max: convertAggregateQueryFields(fieldMap, query.max), | ||
min: convertAggregateQueryFields(fieldMap, query.min), | ||
}; | ||
}; | ||
|
||
export const transformAggregateResponse = <From, To>( | ||
response: AggregateResponse<From>, | ||
fieldMap: QueryFieldMap<From, To>, | ||
): AggregateResponse<To> => { | ||
return { | ||
count: convertAggregateNumberFields(fieldMap, response.count), | ||
sum: convertAggregateNumberFields(fieldMap, response.sum), | ||
avg: convertAggregateNumberFields(fieldMap, response.avg), | ||
max: convertAggregateFields(fieldMap, response.max), | ||
min: convertAggregateFields(fieldMap, response.min), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export type AggregateQuery<DTO> = { | ||
count?: (keyof DTO)[]; | ||
sum?: (keyof DTO)[]; | ||
avg?: (keyof DTO)[]; | ||
max?: (keyof DTO)[]; | ||
min?: (keyof DTO)[]; | ||
}; |
15 changes: 15 additions & 0 deletions
15
packages/core/src/interfaces/aggregate-response.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export type NumberAggregate<DTO> = { | ||
[K in keyof DTO]?: number; | ||
}; | ||
|
||
export type TypeAggregate<DTO> = { | ||
[K in keyof DTO]?: DTO[K]; | ||
}; | ||
|
||
export type AggregateResponse<DTO> = { | ||
count?: NumberAggregate<DTO>; | ||
sum?: NumberAggregate<DTO>; | ||
avg?: NumberAggregate<DTO>; | ||
max?: TypeAggregate<DTO>; | ||
min?: TypeAggregate<DTO>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.