typeorm / typescript-express-example

Example how to use Express and TypeORM with TypeScript
361 stars 100 forks source link

How to set MySQL Transaction within the request action #1

Closed roelzkie15 closed 7 years ago

roelzkie15 commented 7 years ago

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

roelzkie15 commented 7 years ago

Was able to solve by this https://github.com/typeorm/typeorm/issues/185