Search code examples
node.jstransactionsnestjstypeorm

Why TypeORM transactions only handle up to 10th value?


I have a patch method that sends an array containing 15 objects, I can print these 15 values perfectly but only get 10 if I put them in entityManager.transaction. When I send a request, the Nestjs server doesn't return anything, it crashes completely.

I send this to NestJs server

[
    {
        "id": 399,
        "is_addon": true,
    },
    {
        "id": 400,
        "is_addon": false,
    },
    {
        "id": 401,
        "is_addon": false,
    },
    {
        "id": 402,
        "is_addon": false,
    },
    {
        "id": 403,
        "is_addon": false,
    },
    {
        "id": 404,
        "is_addon": false,
    },
    {
        "id": 405,
        "is_addon": false,
    },
    {
        "id": 406,
        "is_addon": false,
    },
    {
        "id": 407,
        "is_addon": false,
    },
    {
        "id": 408,
        "is_addon": false,
    },
    {
        "id": 409,
        "is_addon": false,
    },
    {
        "id": 410,
        "is_addon": false,
    },
    {
        "id": 411,
        "is_addon": false,
    },
    {
        "id": 412,
        "is_addon": false,
    },
    {
        "id": 413,
        "is_addon": false,
    }
]

I print the indexes outside the transaction

const promises = body.body.map(async (bodyConfirm, id) => {
    console.log('id -> ', id);
    await this.entityManager.transaction(async transactionalEntityManager => {
        const incidentCurrent = await super.findOne({ id: bodyConfirm.id }, ['incident_management_users']);
        const proContactUser = await this.userService.findOne({
            user_name: incidentCurrent.incident_management_users.user_name,
            telegram: Not(IsNull())
        })
    })
})

Respond:

Can print all 15 values

When I put inside the transaction

const promises = body.body.map(async (bodyConfirm, id) => {
    await this.entityManager.transaction(async transactionalEntityManager => {
        --> console.log('id -> ', id);
        const incidentCurrent = await super.findOne({ id: bodyConfirm.id }, ['incident_management_users']);
        const proContactUser = await this.userService.findOne({
            user_name: incidentCurrent.incident_management_users.user_name,
            telegram: Not(IsNull())
        })
    })
})

Respond:

Only 10 values display

I don't know why it is like that, if you know the reason please share, thank you!


Solution

  • I finally solved the above problem, I placed the transaction outside the map method, then everything ran perfectly, I think the cause was because I created too many transactions

    await this.entityManager.transaction(async transactionalEntityManager => {
       await Promise.all(body.body.map(async (bodyConfirm) => { 
        // code here
       })
      )
     })