Begins a new transaction or creates a savepoint if a transaction context already exists.
(
options: {
isolationLevel?: IsolationLevel;
readOnly?: boolean;
ctx?: ControlledTransaction<any, any>;
eventBroadcaster?: TransactionEventBroadcaster;
loggerContext?: LogContext;
} = {},
)
| 169 | |
| 170 | /** Begins a new transaction or creates a savepoint if a transaction context already exists. */ |
| 171 | override async begin( |
| 172 | options: { |
| 173 | isolationLevel?: IsolationLevel; |
| 174 | readOnly?: boolean; |
| 175 | ctx?: ControlledTransaction<any, any>; |
| 176 | eventBroadcaster?: TransactionEventBroadcaster; |
| 177 | loggerContext?: LogContext; |
| 178 | } = {}, |
| 179 | ): Promise<ControlledTransaction<any, any>> { |
| 180 | if (options.ctx) { |
| 181 | const ctx = options.ctx as Dictionary; |
| 182 | await options.eventBroadcaster?.dispatchEvent(EventType.beforeTransactionStart, ctx); |
| 183 | ctx.index ??= 0; |
| 184 | const savepointName = `trx${ctx.index + 1}`; |
| 185 | const trx = await options.ctx.savepoint(savepointName as never).execute(); |
| 186 | Reflect.defineProperty(trx, 'index', { value: ctx.index + 1 }); |
| 187 | Reflect.defineProperty(trx, 'savepointName', { value: savepointName }); |
| 188 | this.logQuery(this.platform.getSavepointSQL(savepointName), options.loggerContext); |
| 189 | await options.eventBroadcaster?.dispatchEvent(EventType.afterTransactionStart, trx); |
| 190 | |
| 191 | return trx; |
| 192 | } |
| 193 | |
| 194 | await this.ensureConnection(); |
| 195 | await options.eventBroadcaster?.dispatchEvent(EventType.beforeTransactionStart); |
| 196 | let trxBuilder = this.getClient().startTransaction(); |
| 197 | |
| 198 | if (options.isolationLevel) { |
| 199 | trxBuilder = trxBuilder.setIsolationLevel(options.isolationLevel); |
| 200 | } |
| 201 | |
| 202 | if (options.readOnly) { |
| 203 | trxBuilder = trxBuilder.setAccessMode('read only'); |
| 204 | } |
| 205 | |
| 206 | const trx = await trxBuilder.execute(); |
| 207 | |
| 208 | if (options.ctx) { |
| 209 | const ctx = options.ctx as Dictionary; |
| 210 | ctx.index ??= 0; |
| 211 | const savepointName = `trx${ctx.index + 1}`; |
| 212 | Reflect.defineProperty(trx, 'index', { value: ctx.index + 1 }); |
| 213 | Reflect.defineProperty(trx, 'savepointName', { value: savepointName }); |
| 214 | this.logQuery(this.platform.getSavepointSQL(savepointName), options.loggerContext); |
| 215 | } else { |
| 216 | for (const query of this.platform.getBeginTransactionSQL(options)) { |
| 217 | this.logQuery(query, options.loggerContext); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | await options.eventBroadcaster?.dispatchEvent(EventType.afterTransactionStart, trx); |
| 222 | |
| 223 | return trx; |
| 224 | } |
| 225 | |
| 226 | /** Commits the transaction or releases the savepoint. */ |
| 227 | override async commit( |
nothing calls this directly
no test coverage detected