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

typeorm / typeorm / 14764529297

30 Apr 2025 09:16PM UTC coverage: 76.309% (+0.002%) from 76.307%
14764529297

push

github

web-flow
fix: beforeQuery promises not awaited before query execution (#11086)

* fix: beforeQuery promises not awaited before query execution

Closes: #11085

* fix: run format

Closes: #11085

* fix: apply same beforeQuery & afterQuery logic to all drivers

* fix: use a different broadcaster for BeforeQuery / AfterQuery

* fix: BeforeQuery / AfterQuery event types

* fix: move broadCasterResult.wait in finally block

* fix: remove duplicated broadcasterResult.wait in ReactNativeQueryRunner

* fix: fix prettier issue

* fix: implemented requested changes

* fix: broken sqlite tests

* Revert "fix: broken sqlite tests"

This reverts commit 4bacd5f4b.

* Revert "fix: implemented requested changes"

This reverts commit 1d2f59bf2.

* review: undefined type at the end

* fix: move database connection logic outside of the promise bloc

---------

Co-authored-by: Lucian Mocanu <alumni@users.noreply.github.com>

9201 of 12761 branches covered (72.1%)

Branch coverage included in aggregate %.

100 of 142 new or added lines in 14 files covered. (70.42%)

4 existing lines in 3 files now uncovered.

18802 of 23936 relevant lines covered (78.55%)

197469.14 hits per line

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

11.11
/src/driver/nativescript/NativescriptQueryRunner.ts
1
import { ObjectLiteral } from "../../common/ObjectLiteral"
2
import { QueryFailedError } from "../../error/QueryFailedError"
40✔
3
import { QueryRunnerAlreadyReleasedError } from "../../error/QueryRunnerAlreadyReleasedError"
40✔
4
import { QueryResult } from "../../query-runner/QueryResult"
40✔
5
import { Broadcaster } from "../../subscriber/Broadcaster"
40✔
6
import { AbstractSqliteQueryRunner } from "../sqlite-abstract/AbstractSqliteQueryRunner"
40✔
7
import { NativescriptDriver } from "./NativescriptDriver"
8

9
/**
10
 * Runs queries on a single sqlite database connection.
11
 */
12
export class NativescriptQueryRunner extends AbstractSqliteQueryRunner {
40✔
13
    /**
14
     * Database driver used by connection.
15
     */
16
    driver: NativescriptDriver
17

18
    // -------------------------------------------------------------------------
19
    // Constructor
20
    // -------------------------------------------------------------------------
21

22
    constructor(driver: NativescriptDriver) {
23
        super()
×
24
        this.driver = driver
×
25
        this.connection = driver.connection
×
26
        this.broadcaster = new Broadcaster(this)
×
27
    }
28

29
    /**
30
     * Called before migrations are run.
31
     */
32
    async beforeMigration(): Promise<void> {
33
        await this.query(`PRAGMA foreign_keys = OFF`)
×
34
    }
35

36
    /**
37
     * Called after migrations are run.
38
     */
39
    async afterMigration(): Promise<void> {
40
        await this.query(`PRAGMA foreign_keys = ON`)
×
41
    }
42

43
    /**
44
     * Executes a given SQL query.
45
     */
46
    async query(
47
        query: string,
48
        parameters?: any[],
49
        useStructuredResult = false,
×
50
    ): Promise<any> {
51
        if (this.isReleased) {
×
52
            throw new QueryRunnerAlreadyReleasedError()
×
53
        }
54

55
        const connection = this.driver.connection
×
56

NEW
57
        const databaseConnection = await this.connect()
×
58

59
        return new Promise(async (ok, fail) => {
×
60
            const isInsertQuery = query.substr(0, 11) === "INSERT INTO"
×
61
            connection.logger.logQuery(query, parameters, this)
×
62

63
            const handler = (err: any, raw: any) => {
×
64
                // log slow queries if maxQueryExecution time is set
65
                const maxQueryExecutionTime =
66
                    this.driver.options.maxQueryExecutionTime
×
67
                const queryEndTime = Date.now()
×
68
                const queryExecutionTime = queryEndTime - queryStartTime
×
69

70
                if (
×
71
                    maxQueryExecutionTime &&
×
72
                    queryExecutionTime > maxQueryExecutionTime
73
                ) {
74
                    connection.logger.logQuerySlow(
×
75
                        queryExecutionTime,
76
                        query,
77
                        parameters,
78
                        this,
79
                    )
80
                }
81

82
                if (err) {
×
83
                    connection.logger.logQueryError(
×
84
                        err,
85
                        query,
86
                        parameters,
87
                        this,
88
                    )
89
                    fail(new QueryFailedError(query, parameters, err))
×
90
                }
91

92
                const result = new QueryResult()
×
93
                result.raw = raw
×
94

95
                if (!isInsertQuery && Array.isArray(raw)) {
×
96
                    result.records = raw
×
97
                }
98

99
                if (useStructuredResult) {
×
100
                    ok(result)
×
101
                } else {
102
                    ok(result.raw)
×
103
                }
104
            }
105
            const queryStartTime = Date.now()
×
106

107
            if (isInsertQuery) {
×
108
                databaseConnection.execSQL(query, parameters, handler)
×
109
            } else {
110
                databaseConnection.all(query, parameters, handler)
×
111
            }
112
        })
113
    }
114

115
    // -------------------------------------------------------------------------
116
    // Protected Methods
117
    // -------------------------------------------------------------------------
118

119
    /**
120
     * Parametrizes given object of values. Used to create column=value queries.
121
     */
122
    protected parametrize(
123
        objectLiteral: ObjectLiteral,
124
        startIndex: number = 0,
×
125
    ): string[] {
126
        return Object.keys(objectLiteral).map((key, index) => `"${key}"` + "=?")
×
127
    }
128
}
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