jeremydaly / serverless-mysql

A module for managing MySQL connections at SERVERLESS scale
MIT License
1.21k stars 83 forks source link

How to create transaction? #114

Closed carlosfer02 closed 1 year ago

carlosfer02 commented 3 years ago

How can I commit a query only if the call to another method is successful?

I am trying something like this:

            const transaction = await mysql.connection.transaction();
            await transaction.query("UPDATE employees SET name=? WHERE id=3", [name]);
            let result = await CognitoAccessToken.signUp();
            if (result == true) {
                await transaction.commit();
            } else {
                await transaction.rollback();
            }
            await mysql.connection.end();

But for some reason, only after executing the query twice, it makes the employee update.

PS: I am using node.js

trasherdk commented 3 years ago

Maybe #85 is what you are looking for.