* 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 {@link Transaction} * * If you have [CLS](https://github.com/Jeff-Lewis/cls-hooked) enabled, the transaction will automatically b
(options, autoCallback)
| 1093 | * @returns {Promise} |
| 1094 | */ |
| 1095 | async transaction(options, autoCallback) { |
| 1096 | if (typeof options === class="st">'function') { |
| 1097 | autoCallback = options; |
| 1098 | options = undefined; |
| 1099 | } |
| 1100 | |
| 1101 | const transaction = new Transaction(this, options); |
| 1102 | |
| 1103 | if (!autoCallback) { |
| 1104 | await transaction.prepareEnvironment(false); |
| 1105 | return transaction; |
| 1106 | } |
| 1107 | |
| 1108 | class="cm">// autoCallback provided |
| 1109 | return Sequelize._clsRun(async () => { |
| 1110 | try { |
| 1111 | await transaction.prepareEnvironment(); |
| 1112 | const result = await autoCallback(transaction); |
| 1113 | await transaction.commit(); |
| 1114 | return await result; |
| 1115 | } catch (err) { |
| 1116 | try { |
| 1117 | if (!transaction.finished) { |
| 1118 | await transaction.rollback(); |
| 1119 | } else { |
| 1120 | class="cm">// release the connection, even if we don't need to rollback |
| 1121 | await transaction.cleanup(); |
| 1122 | } |
| 1123 | } catch (err0) { |
| 1124 | class="cm">// ignore |
| 1125 | } |
| 1126 | throw err; |
| 1127 | } |
| 1128 | }); |
| 1129 | } |
| 1130 | |
| 1131 | /** |
| 1132 | * Use CLS (Continuation Local Storage) with Sequelize. With Continuation |