| 4 | import { ITransaction } from '../@types/database' |
| 5 | |
| 6 | export class Transaction implements ITransaction { |
| 7 | private trx: Knex.Transaction<any, any[]> |
| 8 | |
| 9 | public constructor(private readonly dbClient: DatabaseClient) {} |
| 10 | |
| 11 | public async begin(): Promise<void> { |
| 12 | this.trx = await this.dbClient.transaction(null, { isolationLevel: 'serializable' }) |
| 13 | } |
| 14 | |
| 15 | public get transaction(): DatabaseTransaction { |
| 16 | if (!this.trx) { |
| 17 | throw new Error('Unable to get transaction: transaction not started.') |
| 18 | } |
| 19 | return this.trx |
| 20 | } |
| 21 | |
| 22 | public async commit(): Promise<any[]> { |
| 23 | if (!this.trx) { |
| 24 | throw new Error('Unable to get transaction: transaction not started.') |
| 25 | } |
| 26 | return this.trx.commit() |
| 27 | } |
| 28 | |
| 29 | public async rollback(): Promise<any[]> { |
| 30 | if (!this.trx) { |
| 31 | throw new Error('Unable to get transaction: transaction not started.') |
| 32 | } |
| 33 | return this.trx.rollback() |
| 34 | } |
| 35 | } |
nothing calls this directly
no outgoing calls
no test coverage detected