* Called to acquire a connection to use and set the correct options on the connection. * We should ensure all of the environment that's set up is cleaned up in `cleanup()` below. * * @param {boolean} useCLS Defaults to true: Use CLS (Continuation Local Storage) with Sequelize. With CLS, all
(useCLS = true)
| 110 | * @returns {Promise} |
| 111 | */ |
| 112 | async prepareEnvironment(useCLS = true) { |
| 113 | let connectionPromise; |
| 114 | |
| 115 | if (this.parent) { |
| 116 | connectionPromise = Promise.resolve(this.parent.connection); |
| 117 | } else { |
| 118 | const acquireOptions = { uuid: this.id }; |
| 119 | if (this.options.readOnly) { |
| 120 | acquireOptions.type = 'SELECT'; |
| 121 | } |
| 122 | connectionPromise = this.sequelize.connectionManager.getConnection(acquireOptions); |
| 123 | } |
| 124 | |
| 125 | let result; |
| 126 | const connection = await connectionPromise; |
| 127 | this.connection = connection; |
| 128 | this.connection.uuid = this.id; |
| 129 | |
| 130 | try { |
| 131 | await this.begin(); |
| 132 | result = await this.setDeferrable(); |
| 133 | } catch (setupErr) { |
| 134 | try { |
| 135 | result = await this.rollback(); |
| 136 | } finally { |
| 137 | throw setupErr; // eslint-disable-line no-unsafe-finally |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // TODO (@ephys) [>=7.0.0]: move this inside of sequelize.transaction, remove parameter. |
| 142 | if (useCLS && this.sequelize.constructor._cls) { |
| 143 | this.sequelize.constructor._cls.set('transaction', this); |
| 144 | } |
| 145 | |
| 146 | return result; |
| 147 | } |
| 148 | |
| 149 | async setDeferrable() { |
| 150 | if (this.options.deferrable) { |
no test coverage detected