* Start a transaction. When using transactions, you should pass the transaction in the options argument in order for the query to happen under that transaction @see Transaction * * If you have [CLS](https://github.com/Jeff-Lewis/cls-hooked) enabled, the transaction will automatically b
(options, autoCallback)
| 1176 | * @returns {Promise} |
| 1177 | */ |
| 1178 | async transaction(options, autoCallback) { |
| 1179 | if (typeof options === 'function') { |
| 1180 | autoCallback = options; |
| 1181 | options = undefined; |
| 1182 | } |
| 1183 | |
| 1184 | const transaction = new Transaction(this, options); |
| 1185 | |
| 1186 | if (!autoCallback) { |
| 1187 | await transaction.prepareEnvironment(/* cls */ false); |
| 1188 | return transaction; |
| 1189 | } |
| 1190 | |
| 1191 | // autoCallback provided |
| 1192 | return Sequelize._clsRun(async () => { |
| 1193 | await transaction.prepareEnvironment(/* cls */ true); |
| 1194 | |
| 1195 | let result; |
| 1196 | try { |
| 1197 | result = await autoCallback(transaction); |
| 1198 | } catch (err) { |
| 1199 | try { |
| 1200 | await transaction.rollback(); |
| 1201 | } catch (ignore) { |
| 1202 | // ignore, because 'rollback' will already print the error before killing the connection |
| 1203 | } |
| 1204 | |
| 1205 | throw err; |
| 1206 | } |
| 1207 | |
| 1208 | await transaction.commit(); |
| 1209 | return result; |
| 1210 | }); |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * Use CLS (Continuation Local Storage) with Sequelize. With Continuation |