typeorm/typescript-express-example

How to set MySQL Transaction within the request action

roelzkie15 opened this issue · 1 comments

Hi,

Is there any approach how to set a mysql transaction inside a single request action method? So we may be able to rollback transaction whenever error occurs.

Code example:

export async function saveDepartmentAction(request: Request, response: Response) {
    // data
    let data = request.body

    // insert department data
    const result = await getEntityManager().query(`
        // Some department insert (parent data)
    `, [ some department data ])


    // add child data (department positions)
    data.positions.forEach(async position => {
         const add_position = await getEntityManager().query(`
             // Some position insert (child data)
         `, [ some department id and position id ])        
     });

    response.send(data)
}

I would like to achieved something like this https://github.com/mysqljs/mysql#transactions

Was able to solve by this typeorm/typeorm#185