fix: batch updates in upsertMany_ (#12333)
This commit is contained in:
@@ -967,28 +967,24 @@ export function mikroOrmBaseRepositoryFactory<const T extends object>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const promises: Promise<any>[] = []
|
const promises: Promise<any>[] = []
|
||||||
const toInsert: unknown[] = []
|
const toInsert: any[] = []
|
||||||
let shouldInsert = false
|
const toUpdate: any[] = []
|
||||||
|
|
||||||
entries.map(async (data) => {
|
entries.forEach((data) => {
|
||||||
const existingEntity = existingEntitiesMap.get(data.id)
|
const existingEntity = existingEntitiesMap.get(data.id)
|
||||||
orderedEntities.push(data)
|
orderedEntities.push(data)
|
||||||
if (existingEntity) {
|
if (existingEntity) {
|
||||||
if (skipUpdate) {
|
if (skipUpdate) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const update = manager.nativeUpdate(entityName, { id: data.id }, data)
|
|
||||||
promises.push(update)
|
|
||||||
|
|
||||||
performedActions.updated[entityName] ??= []
|
toUpdate.push(data)
|
||||||
performedActions.updated[entityName].push({ id: data.id })
|
|
||||||
} else {
|
} else {
|
||||||
shouldInsert = true
|
|
||||||
toInsert.push(data)
|
toInsert.push(data)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if (shouldInsert) {
|
if (toInsert.length > 0) {
|
||||||
let insertQb = manager.qb(entityName).insert(toInsert).returning("id")
|
let insertQb = manager.qb(entityName).insert(toInsert).returning("id")
|
||||||
|
|
||||||
if (skipUpdate) {
|
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)
|
await promiseAll(promises)
|
||||||
|
|
||||||
return { orderedEntities, performedActions }
|
return { orderedEntities, performedActions }
|
||||||
|
|||||||
Reference in New Issue
Block a user