• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

typeorm / typeorm / 23390157208

21 Mar 2026 10:26PM UTC coverage: 56.678% (-16.6%) from 73.277%
23390157208

Pull #12252

github

web-flow
Merge 5b60ba41c into 7038fa166
Pull Request #12252: fix: unskip cascade soft remove test

17767 of 26580 branches covered (66.84%)

Branch coverage included in aggregate %.

64033 of 117744 relevant lines covered (54.38%)

1514.83 hits per line

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

83.74
/src/driver/sqljs/SqljsQueryRunner.ts
1
import { QueryFailedError } from "../../error/QueryFailedError"
28✔
2
import { QueryRunnerAlreadyReleasedError } from "../../error/QueryRunnerAlreadyReleasedError"
28✔
3
import { QueryResult } from "../../query-runner/QueryResult"
28✔
4
import { Broadcaster } from "../../subscriber/Broadcaster"
28✔
5
import { BroadcasterResult } from "../../subscriber/BroadcasterResult"
28✔
6
import { AbstractSqliteQueryRunner } from "../sqlite-abstract/AbstractSqliteQueryRunner"
28✔
7
import type { SqljsDriver } from "./SqljsDriver"
28✔
8

28✔
9
/**
28✔
10
 * Runs queries on a single sqlite database connection.
28✔
11
 */
28✔
12
export class SqljsQueryRunner extends AbstractSqliteQueryRunner {
28✔
13
    /**
28✔
14
     * Flag to determine if a modification has happened since the last time this query runner has requested a save.
28✔
15
     */
28✔
16
    private isDirty = false
28✔
17

28✔
18
    /**
28✔
19
     * Database driver used by connection.
28✔
20
     */
28✔
21
    driver: SqljsDriver
28✔
22

28✔
23
    // -------------------------------------------------------------------------
28✔
24
    // Constructor
28✔
25
    // -------------------------------------------------------------------------
28✔
26

28✔
27
    constructor(driver: SqljsDriver) {
28✔
28
        super()
3✔
29
        this.driver = driver
3✔
30
        this.connection = driver.connection
3✔
31
        this.broadcaster = new Broadcaster(this)
3✔
32
    }
3✔
33

28✔
34
    // -------------------------------------------------------------------------
28✔
35
    // Public methods
28✔
36
    // -------------------------------------------------------------------------
28✔
37

28✔
38
    /**
28✔
39
     * Called before migrations are run.
28✔
40
     */
28✔
41
    async beforeMigration(): Promise<void> {
28✔
42
        await this.query(`PRAGMA foreign_keys = OFF`)
6✔
43
    }
6✔
44

28✔
45
    /**
28✔
46
     * Called after migrations are run.
28✔
47
     */
28✔
48
    async afterMigration(): Promise<void> {
28✔
49
        await this.query(`PRAGMA foreign_keys = ON`)
6✔
50
    }
6✔
51

28✔
52
    private async flush() {
28✔
53
        if (this.isDirty) {
78✔
54
            await this.driver.autoSave()
42✔
55
            this.isDirty = false
42✔
56
        }
42✔
57
    }
78✔
58

28✔
59
    async release(): Promise<void> {
28✔
60
        await this.flush()
48✔
61
        return super.release()
48✔
62
    }
48✔
63

28✔
64
    /**
28✔
65
     * Commits transaction.
28✔
66
     * Error will be thrown if transaction was not started.
28✔
67
     */
28✔
68
    async commitTransaction(): Promise<void> {
28✔
69
        await super.commitTransaction()
30✔
70
        if (!this.isTransactionActive) {
30✔
71
            await this.flush()
30✔
72
        }
30✔
73
    }
30✔
74

28✔
75
    /**
28✔
76
     * Executes a given SQL query.
28✔
77
     * @param query
28✔
78
     * @param parameters
28✔
79
     * @param useStructuredResult
28✔
80
     */
28✔
81
    async query(
28✔
82
        query: string,
411✔
83
        parameters: any[] = [],
411✔
84
        useStructuredResult = false,
411✔
85
    ): Promise<any> {
411✔
86
        if (this.isReleased) throw new QueryRunnerAlreadyReleasedError()
411!
87

411✔
88
        const command = query.trim().split(" ", 1)[0]
411✔
89

411✔
90
        const databaseConnection = this.driver.databaseConnection
411✔
91

411✔
92
        this.driver.connection.logger.logQuery(query, parameters, this)
411✔
93
        await this.broadcaster.broadcast("BeforeQuery", query, parameters)
411✔
94

411✔
95
        const broadcasterResult = new BroadcasterResult()
411✔
96
        const queryStartTime = Date.now()
411✔
97
        let statement: any
411✔
98

411✔
99
        try {
411✔
100
            statement = databaseConnection.prepare(query)
411✔
101
            if (parameters) {
411✔
102
                parameters = parameters.map((p) =>
411✔
103
                    typeof p !== "undefined" ? p : null,
411!
104
                )
411✔
105

411✔
106
                statement.bind(parameters)
411✔
107
            }
411✔
108

411✔
109
            // log slow queries if maxQueryExecution time is set
411✔
110
            const maxQueryExecutionTime =
411✔
111
                this.driver.options.maxQueryExecutionTime
411✔
112
            const queryEndTime = Date.now()
411✔
113
            const queryExecutionTime = queryEndTime - queryStartTime
411✔
114

411✔
115
            if (
411✔
116
                maxQueryExecutionTime &&
411!
117
                queryExecutionTime > maxQueryExecutionTime
×
118
            )
411✔
119
                this.driver.connection.logger.logQuerySlow(
411!
120
                    queryExecutionTime,
×
121
                    query,
×
122
                    parameters,
×
123
                    this,
×
124
                )
×
125

411✔
126
            const records: any[] = []
411✔
127

411✔
128
            while (statement.step()) {
411✔
129
                records.push(statement.getAsObject())
165✔
130
            }
165✔
131

411✔
132
            this.broadcaster.broadcastAfterQueryEvent(
411✔
133
                broadcasterResult,
411✔
134
                query,
411✔
135
                parameters,
411✔
136
                true,
411✔
137
                queryExecutionTime,
411✔
138
                records,
411✔
139
                undefined,
411✔
140
            )
411✔
141

411✔
142
            const result = new QueryResult()
411✔
143

411✔
144
            result.affected = databaseConnection.getRowsModified()
411✔
145
            result.records = records
411✔
146
            result.raw = records
411✔
147

411✔
148
            statement.free()
411✔
149

411✔
150
            if (command !== "SELECT") {
411✔
151
                this.isDirty = true
264✔
152
            }
264✔
153

411✔
154
            if (useStructuredResult) {
411✔
155
                return result
186✔
156
            } else {
411✔
157
                return result.raw
225✔
158
            }
225✔
159
        } catch (err) {
411!
160
            if (statement) {
×
161
                statement.free()
×
162
            }
×
163

×
164
            this.driver.connection.logger.logQueryError(
×
165
                err,
×
166
                query,
×
167
                parameters,
×
168
                this,
×
169
            )
×
170
            this.broadcaster.broadcastAfterQueryEvent(
×
171
                broadcasterResult,
×
172
                query,
×
173
                parameters,
×
174
                false,
×
175
                undefined,
×
176
                undefined,
×
177
                err,
×
178
            )
×
179

×
180
            throw new QueryFailedError(query, parameters, err)
×
181
        } finally {
×
182
            await broadcasterResult.wait()
411✔
183
        }
411✔
184
    }
411✔
185
}
28✔
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