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

typeorm / typeorm / 15219332477

23 May 2025 09:13PM UTC coverage: 17.216% (-59.1%) from 76.346%
15219332477

Pull #11332

github

naorpeled
cr comments - move if block
Pull Request #11332: feat: add new undefined and null behavior flags

1603 of 12759 branches covered (12.56%)

Branch coverage included in aggregate %.

0 of 31 new or added lines in 3 files covered. (0.0%)

14132 existing lines in 166 files now uncovered.

4731 of 24033 relevant lines covered (19.69%)

60.22 hits per line

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

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

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

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

23
    // -------------------------------------------------------------------------
24
    // Constructor
25
    // -------------------------------------------------------------------------
26

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

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

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

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

52
    private async flush() {
UNCOV
53
        if (this.isDirty) {
×
UNCOV
54
            await this.driver.autoSave()
×
UNCOV
55
            this.isDirty = false
×
56
        }
57
    }
58

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

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

75
    /**
76
     * Executes a given SQL query.
77
     */
78
    async query(
79
        query: string,
80
        parameters: any[] = [],
×
81
        useStructuredResult = false,
×
82
    ): Promise<any> {
UNCOV
83
        if (this.isReleased) throw new QueryRunnerAlreadyReleasedError()
×
84

UNCOV
85
        const command = query.trim().split(" ", 1)[0]
×
86

UNCOV
87
        const databaseConnection = this.driver.databaseConnection
×
88

UNCOV
89
        this.driver.connection.logger.logQuery(query, parameters, this)
×
UNCOV
90
        await this.broadcaster.broadcast("BeforeQuery", query, parameters)
×
91

UNCOV
92
        const broadcasterResult = new BroadcasterResult()
×
UNCOV
93
        const queryStartTime = Date.now()
×
94
        let statement: any
95

UNCOV
96
        try {
×
UNCOV
97
            statement = databaseConnection.prepare(query)
×
UNCOV
98
            if (parameters) {
×
UNCOV
99
                parameters = parameters.map((p) =>
×
UNCOV
100
                    typeof p !== "undefined" ? p : null,
×
101
                )
102

UNCOV
103
                statement.bind(parameters)
×
104
            }
105

106
            // log slow queries if maxQueryExecution time is set
107
            const maxQueryExecutionTime =
UNCOV
108
                this.driver.options.maxQueryExecutionTime
×
UNCOV
109
            const queryEndTime = Date.now()
×
UNCOV
110
            const queryExecutionTime = queryEndTime - queryStartTime
×
111

UNCOV
112
            if (
×
113
                maxQueryExecutionTime &&
×
114
                queryExecutionTime > maxQueryExecutionTime
115
            )
116
                this.driver.connection.logger.logQuerySlow(
×
117
                    queryExecutionTime,
118
                    query,
119
                    parameters,
120
                    this,
121
                )
122

UNCOV
123
            const records: any[] = []
×
124

UNCOV
125
            while (statement.step()) {
×
UNCOV
126
                records.push(statement.getAsObject())
×
127
            }
128

UNCOV
129
            this.broadcaster.broadcastAfterQueryEvent(
×
130
                broadcasterResult,
131
                query,
132
                parameters,
133
                true,
134
                queryExecutionTime,
135
                records,
136
                undefined,
137
            )
138

UNCOV
139
            const result = new QueryResult()
×
140

UNCOV
141
            result.affected = databaseConnection.getRowsModified()
×
UNCOV
142
            result.records = records
×
UNCOV
143
            result.raw = records
×
144

UNCOV
145
            statement.free()
×
146

UNCOV
147
            if (command !== "SELECT") {
×
UNCOV
148
                this.isDirty = true
×
149
            }
150

UNCOV
151
            if (useStructuredResult) {
×
UNCOV
152
                return result
×
153
            } else {
UNCOV
154
                return result.raw
×
155
            }
156
        } catch (err) {
UNCOV
157
            if (statement) {
×
UNCOV
158
                statement.free()
×
159
            }
160

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

UNCOV
177
            throw new QueryFailedError(query, parameters, err)
×
178
        } finally {
UNCOV
179
            await broadcasterResult.wait()
×
180
        }
181
    }
182
}
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

© 2025 Coveralls, Inc