Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"cache": true
}
},
"nxCloudAccessToken": "YWExNTY0MWYtYWMzNy00ZWZkLWIzMWEtMGYzYWY4YWRmMDE1fHJlYWQtd3JpdGU=",
"neverConnectToCloud": true,
"useInferencePlugins": false,
"defaultBase": "master"
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('Cursor paging strategy QueryArgsType with manual options', (): void =>
}
const queryInstance = plainToClass(TestCursorQuery, queryObj)
const errors = validateSync(queryInstance)
expect(errors.length).toBe(1)
expect(errors).toHaveLength(1)
expect(errors[0].property).toBe('paging')
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,9 @@ describe('MikroOrmQueryService', () => {
})

it('should throw for withDeleted option', async () => {
await expect(
queryService.aggregate({}, { count: [{ field: 'id', args: {} }] }, { withDeleted: true })
).rejects.toThrow('MikroOrmQueryService does not support withDeleted on aggregate')
await expect(queryService.aggregate({}, { count: [{ field: 'id', args: {} }] }, { withDeleted: true })).rejects.toThrow(
'MikroOrmQueryService does not support withDeleted on aggregate'
)
})
})
})
48 changes: 30 additions & 18 deletions packages/query-mikro-orm/src/services/mikro-orm-query.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { Collection, EntityData, EntityKey, EntityRepository, FilterQuery, QueryOrder, QueryOrderMap, Reference, wrap } from '@mikro-orm/core'
import {
Collection,
EntityData,
EntityKey,
EntityRepository,
FilterQuery,
QueryOrder,
QueryOrderMap,
Reference,
wrap
} from '@mikro-orm/core'
import { OperatorMap } from '@mikro-orm/core/typings'
import {
AggregateOptions,
Expand Down Expand Up @@ -98,7 +108,7 @@ export class MikroOrmQueryService<DTO extends object, Entity extends object = DT

async createOne(record: DeepPartial<DTO>): Promise<DTO> {
const em = this.repo.getEntityManager()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
const entity = em.create(this.repo.getEntityName(), record as any)
await em.persistAndFlush(entity)

Expand All @@ -110,7 +120,7 @@ export class MikroOrmQueryService<DTO extends object, Entity extends object = DT

async createMany(records: DeepPartial<DTO>[]): Promise<DTO[]> {
const em = this.repo.getEntityManager()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
const entities = records.map((r) => em.create(this.repo.getEntityName(), r as any))
await em.persistAndFlush(entities)

Expand All @@ -135,7 +145,7 @@ export class MikroOrmQueryService<DTO extends object, Entity extends object = DT
async updateMany(update: DeepPartial<DTO>, filter: Filter<DTO>): Promise<UpdateManyResponse> {
const em = this.repo.getEntityManager()
const where = this.convertFilter(filter)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
const updatedCount = await em.nativeUpdate(this.repo.getEntityName(), where, update as any)
return { updatedCount }
}
Expand Down Expand Up @@ -225,10 +235,7 @@ export class MikroOrmQueryService<DTO extends object, Entity extends object = DT
return this.computeAggregateInMemory(entities, aggregateQuery)
}

private computeAggregateInMemory(
entities: Entity[],
aggregateQuery: AggregateQuery<DTO>
): AggregateResponse<DTO>[] {
private computeAggregateInMemory(entities: Entity[], aggregateQuery: AggregateQuery<DTO>): AggregateResponse<DTO>[] {
if (!aggregateQuery.groupBy || aggregateQuery.groupBy.length === 0) {
return [this.computeAggregateForGroup(entities, aggregateQuery)]
}
Expand Down Expand Up @@ -265,36 +272,39 @@ export class MikroOrmQueryService<DTO extends object, Entity extends object = DT
const response: AggregateResponse<DTO> = {}

if (aggregateQuery.count) {
response.count = {}
const count: Record<string, number> = {}
for (const { field } of aggregateQuery.count) {
;(response.count as Record<string, number>)[String(field)] = entities.length
count[String(field)] = entities.length
}
response.count = count as AggregateResponse<DTO>['count']
}

if (aggregateQuery.sum) {
response.sum = {}
const sumAcc: Record<string, number> = {}
for (const { field } of aggregateQuery.sum) {
const sum = entities.reduce((acc, e) => {
const val = (e as Record<string, unknown>)[String(field)]
return acc + (typeof val === 'number' ? val : 0)
}, 0)
;(response.sum as Record<string, number>)[String(field)] = sum
sumAcc[String(field)] = sum
}
response.sum = sumAcc as AggregateResponse<DTO>['sum']
}

if (aggregateQuery.avg) {
response.avg = {}
const avgAcc: Record<string, number> = {}
for (const { field } of aggregateQuery.avg) {
const sum = entities.reduce((acc, e) => {
const val = (e as Record<string, unknown>)[String(field)]
return acc + (typeof val === 'number' ? val : 0)
}, 0)
;(response.avg as Record<string, number>)[String(field)] = entities.length > 0 ? sum / entities.length : 0
avgAcc[String(field)] = entities.length > 0 ? sum / entities.length : 0
}
response.avg = avgAcc as AggregateResponse<DTO>['avg']
}

if (aggregateQuery.max) {
response.max = {}
const maxAcc: Record<string, unknown> = {}
for (const { field } of aggregateQuery.max) {
let max: unknown = undefined
for (const e of entities) {
Expand All @@ -303,12 +313,13 @@ export class MikroOrmQueryService<DTO extends object, Entity extends object = DT
max = val
}
}
;(response.max as Record<string, unknown>)[String(field)] = max
maxAcc[String(field)] = max
}
response.max = maxAcc as AggregateResponse<DTO>['max']
}

if (aggregateQuery.min) {
response.min = {}
const minAcc: Record<string, unknown> = {}
for (const { field } of aggregateQuery.min) {
let min: unknown = undefined
for (const e of entities) {
Expand All @@ -317,8 +328,9 @@ export class MikroOrmQueryService<DTO extends object, Entity extends object = DT
min = val
}
}
;(response.min as Record<string, unknown>)[String(field)] = min
minAcc[String(field)] = min
}
response.min = minAcc as AggregateResponse<DTO>['min']
}

return response
Expand Down
Loading