fix: batch updates in upsertMany_ (#12333)

This commit is contained in:
Pedro Guzman
2025-05-02 08:36:33 +02:00
committed by GitHub
parent 70a99a2434
commit ed55d17a36

View File

@@ -967,28 +967,24 @@ export function mikroOrmBaseRepositoryFactory<const T extends object>(
}
const promises: Promise<any>[] = []
const toInsert: unknown[] = []
let shouldInsert = false
const toInsert: any[] = []
const toUpdate: any[] = []
entries.map(async (data) => {
entries.forEach((data) => {
const existingEntity = existingEntitiesMap.get(data.id)
orderedEntities.push(data)
if (existingEntity) {
if (skipUpdate) {
return
}
const update = manager.nativeUpdate(entityName, { id: data.id }, data)
promises.push(update)
performedActions.updated[entityName] ??= []
performedActions.updated[entityName].push({ id: data.id })
toUpdate.push(data)
} else {
shouldInsert = true
toInsert.push(data)
}
})
if (shouldInsert) {
if (toInsert.length > 0) {
let insertQb = manager.qb(entityName).insert(toInsert).returning("id")
if (skipUpdate) {
@@ -1005,6 +1001,26 @@ export function mikroOrmBaseRepositoryFactory<const T extends object>(
)
}
if (toUpdate.length > 0) {
promises.push(
manager
.getDriver()
.nativeUpdateMany(
entityName,
toUpdate.map((d) => ({ id: d.id })),
toUpdate,
{ ctx: manager.getTransactionContext() }
)
.then((res) => {
performedActions.updated[entityName] ??= []
const updatedRows = res.rows ?? []
performedActions.updated[entityName].push(
...updatedRows.map((d) => ({ id: d.id }))
)
})
)
}
await promiseAll(promises)
return { orderedEntities, performedActions }