• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

typeorm / typeorm / 13638877668

03 Mar 2025 07:30PM UTC coverage: 72.357% (-0.02%) from 72.374%
13638877668

push

github

web-flow
Merge pull request #11264 from alumni/fix-transaction-issues

8654 of 12650 branches covered (68.41%)

Branch coverage included in aggregate %.

26 of 34 new or added lines in 9 files covered. (76.47%)

5 existing lines in 1 file now uncovered.

17878 of 24018 relevant lines covered (74.44%)

147954.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

7.14
/src/driver/aurora-postgres/AuroraPostgresQueryRunner.ts
1
import { QueryRunnerAlreadyReleasedError } from "../../error/QueryRunnerAlreadyReleasedError"
32✔
2
import { TransactionNotStartedError } from "../../error/TransactionNotStartedError"
32✔
3
import { QueryRunner } from "../../query-runner/QueryRunner"
4
import { IsolationLevel } from "../types/IsolationLevel"
5
import { AuroraPostgresDriver } from "./AuroraPostgresDriver"
6
import { PostgresQueryRunner } from "../postgres/PostgresQueryRunner"
32✔
7
import { ReplicationMode } from "../types/ReplicationMode"
8
import { QueryResult } from "../../query-runner/QueryResult"
32✔
9
import { Table } from "../../schema-builder/table/Table"
10
import { TypeORMError } from "../../error"
32✔
11

12
class PostgresQueryRunnerWrapper extends PostgresQueryRunner {
13
    driver: any
14

15
    constructor(driver: any, mode: ReplicationMode) {
16
        super(driver, mode)
×
17
    }
18
}
19

20
/**
21
 * Runs queries on a single postgres database connection.
22
 */
23
export class AuroraPostgresQueryRunner
32✔
24
    extends PostgresQueryRunnerWrapper
25
    implements QueryRunner
26
{
27
    // -------------------------------------------------------------------------
28
    // Public Implemented Properties
29
    // -------------------------------------------------------------------------
30

31
    /**
32
     * Database driver used by connection.
33
     */
34
    driver: AuroraPostgresDriver
35

36
    protected client: any
37

38
    // -------------------------------------------------------------------------
39
    // Protected Properties
40
    // -------------------------------------------------------------------------
41

42
    /**
43
     * Promise used to obtain a database connection for a first time.
44
     */
45
    protected databaseConnectionPromise: Promise<any>
46

47
    // -------------------------------------------------------------------------
48
    // Constructor
49
    // -------------------------------------------------------------------------
50

51
    constructor(
52
        driver: AuroraPostgresDriver,
53
        client: any,
54
        mode: ReplicationMode,
55
    ) {
56
        super(driver, mode)
×
57

58
        this.client = client
×
59
    }
60

61
    // -------------------------------------------------------------------------
62
    // Public Methods
63
    // -------------------------------------------------------------------------
64

65
    /**
66
     * Creates/uses database connection from the connection pool to perform further operations.
67
     * Returns obtained database connection.
68
     */
69
    connect(): Promise<any> {
70
        if (this.databaseConnection)
×
71
            return Promise.resolve(this.databaseConnection)
×
72

73
        if (this.databaseConnectionPromise)
×
74
            return this.databaseConnectionPromise
×
75

76
        if (this.mode === "slave" && this.driver.isReplicated) {
×
77
            this.databaseConnectionPromise = this.driver
×
78
                .obtainSlaveConnection()
79
                .then(([connection, release]: any[]) => {
80
                    this.driver.connectedQueryRunners.push(this)
×
81
                    this.databaseConnection = connection
×
82
                    this.releaseCallback = release
×
83
                    return this.databaseConnection
×
84
                })
85
        } else {
86
            // master
87
            this.databaseConnectionPromise = this.driver
×
88
                .obtainMasterConnection()
89
                .then(([connection, release]: any[]) => {
90
                    this.driver.connectedQueryRunners.push(this)
×
91
                    this.databaseConnection = connection
×
92
                    this.releaseCallback = release
×
93
                    return this.databaseConnection
×
94
                })
95
        }
96

97
        return this.databaseConnectionPromise
×
98
    }
99

100
    /**
101
     * Starts transaction on the current connection.
102
     */
103
    async startTransaction(isolationLevel?: IsolationLevel): Promise<void> {
104
        this.isTransactionActive = true
×
105
        try {
×
106
            await this.broadcaster.broadcast("BeforeTransactionStart")
×
107
        } catch (err) {
108
            this.isTransactionActive = false
×
109
            throw err
×
110
        }
111

112
        if (this.transactionDepth === 0) {
×
113
            await this.client.startTransaction()
×
114
        } else {
NEW
115
            await this.query(`SAVEPOINT typeorm_${this.transactionDepth}`)
×
116
        }
NEW
117
        this.transactionDepth += 1
×
118

119
        await this.broadcaster.broadcast("AfterTransactionStart")
×
120
    }
121

122
    /**
123
     * Commits transaction.
124
     * Error will be thrown if transaction was not started.
125
     */
126
    async commitTransaction(): Promise<void> {
127
        if (!this.isTransactionActive) throw new TransactionNotStartedError()
×
128

129
        await this.broadcaster.broadcast("BeforeTransactionCommit")
×
130

131
        if (this.transactionDepth > 1) {
×
132
            await this.query(
×
133
                `RELEASE SAVEPOINT typeorm_${this.transactionDepth - 1}`,
134
            )
135
        } else {
136
            await this.client.commitTransaction()
×
137
            this.isTransactionActive = false
×
138
        }
NEW
139
        this.transactionDepth -= 1
×
140

141
        await this.broadcaster.broadcast("AfterTransactionCommit")
×
142
    }
143

144
    /**
145
     * Rollbacks transaction.
146
     * Error will be thrown if transaction was not started.
147
     */
148
    async rollbackTransaction(): Promise<void> {
149
        if (!this.isTransactionActive) throw new TransactionNotStartedError()
×
150

151
        await this.broadcaster.broadcast("BeforeTransactionRollback")
×
152

153
        if (this.transactionDepth > 1) {
×
154
            await this.query(
×
155
                `ROLLBACK TO SAVEPOINT typeorm_${this.transactionDepth - 1}`,
156
            )
157
        } else {
158
            await this.client.rollbackTransaction()
×
159
            this.isTransactionActive = false
×
160
        }
NEW
161
        this.transactionDepth -= 1
×
162

163
        await this.broadcaster.broadcast("AfterTransactionRollback")
×
164
    }
165

166
    /**
167
     * Executes a given SQL query.
168
     */
169
    async query(
170
        query: string,
171
        parameters?: any[],
172
        useStructuredResult = false,
×
173
    ): Promise<any> {
174
        if (this.isReleased) throw new QueryRunnerAlreadyReleasedError()
×
175

176
        const raw = await this.client.query(query, parameters)
×
177

178
        const result = new QueryResult()
×
179

180
        result.raw = raw
×
181

182
        if (raw?.hasOwnProperty("records") && Array.isArray(raw.records)) {
×
183
            result.records = raw.records
×
184
        }
185

186
        if (raw?.hasOwnProperty("numberOfRecordsUpdated")) {
×
187
            result.affected = raw.numberOfRecordsUpdated
×
188
        }
189

190
        if (!useStructuredResult) {
×
191
            return result.raw
×
192
        }
193

194
        return result
×
195
    }
196

197
    /**
198
     * Change table comment.
199
     */
200
    changeTableComment(
201
        tableOrName: Table | string,
202
        comment?: string,
203
    ): Promise<void> {
204
        throw new TypeORMError(
×
205
            `aurora-postgres driver does not support change comment.`,
206
        )
207
    }
208
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc