https://github.com/pekim/tedious/blob/master/test/integration/transactions-test.coffee includes some tests, that show one approach. (Sorry it's in coffeescript.)
Connection's beginTransaction and commitTransaction (and of course rollbackTransaction) work similarly to the execSql and execSql functions. They all perform an operation on the connection asynchronously, and call a callback when they have completed (or failed).
In the simplest scenario:
- call connection.beginTransaction
- in it's callback, call connection.execSql
- in it's callback, call connection.commitTransaction
This assumes all calls complete without errors, and you always want to commit (never rollback).
As in any node code with lots of asynchronous requests, you may want to use a helper library such as
async (or you may be more comfortable staying away from them).
Mike