Skip to content

Commit

Permalink
fix: bug on aggregate operation on memory driver
Browse files Browse the repository at this point in the history
  • Loading branch information
edobrb committed Nov 13, 2023
1 parent 811aae1 commit dd97351
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/dal/drivers/in-memory/dao.memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ export class AbstractInMemoryDAO<T extends InMemoryDAOGenerics> extends Abstract

protected async _aggregate<A extends AggregateParams<T>>(params: A, args?: AggregatePostProcessing<T, A>): Promise<AggregateResults<T, A>> {
const groupBy = <O, K extends string>(list: O[], getKey: (item: O) => K) =>
list.reduce((previous, currentItem) => {
const group = getKey(currentItem)
if (!previous[group]) previous[group] = []
previous[group].push(currentItem)
return previous
}, {} as Record<K, O[]>)
list.reduce(
(previous, currentItem) => {
const group = getKey(currentItem)
if (!previous[group]) previous[group] = []
previous[group].push(currentItem)
return previous
},
{} as Record<K, O[]>,
)

const records = await this.entitiesArray(params.filter)

Expand Down Expand Up @@ -120,10 +123,10 @@ export class AbstractInMemoryDAO<T extends InMemoryDAOGenerics> extends Abstract
return { ...p, [k]: records.map((v) => getByPath(v, aggregation.field as string) as number).reduce((p, c) => p + c, 0) / records.length }
}
if (aggregation.operation === 'max') {
return { ...p, [k]: records.map((v) => getByPath(v, aggregation.field as string)).reduce((p, c) => (compare(p, c) > 0 ? p : c), records[0]) }
return { ...p, [k]: records.map((v) => getByPath(v, aggregation.field as string)).reduce((p, c) => (compare(p, c) > 0 ? p : c), getByPath(records[0], aggregation.field as string)) }
}
if (aggregation.operation === 'min') {
return { ...p, [k]: records.map((v) => getByPath(v, aggregation.field as string)).reduce((p, c) => (compare(p, c) < 0 ? p : c), records[0]) }
return { ...p, [k]: records.map((v) => getByPath(v, aggregation.field as string)).reduce((p, c) => (compare(p, c) < 0 ? p : c), getByPath(records[0], aggregation.field as string)) }
}
return p
}
Expand Down

0 comments on commit dd97351

Please sign in to comment.