Skip to content

Commit 1d2f59b

Browse files
committed
fix: implemented requested changes
1 parent f2fbd75 commit 1d2f59b

File tree

6 files changed

+319
-378
lines changed

6 files changed

+319
-378
lines changed

src/driver/aurora-mysql/AuroraMysqlQueryRunner.ts

+21-27
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
import { QueryResult } from "../../query-runner/QueryResult"
2-
import { QueryRunner } from "../../query-runner/QueryRunner"
31
import { ObjectLiteral } from "../../common/ObjectLiteral"
2+
import { TypeORMError } from "../../error"
3+
import { QueryRunnerAlreadyReleasedError } from "../../error/QueryRunnerAlreadyReleasedError"
44
import { TransactionNotStartedError } from "../../error/TransactionNotStartedError"
5-
import { TableColumn } from "../../schema-builder/table/TableColumn"
5+
import { ReadStream } from "../../platform/PlatformTools"
6+
import { BaseQueryRunner } from "../../query-runner/BaseQueryRunner"
7+
import { QueryResult } from "../../query-runner/QueryResult"
8+
import { QueryRunner } from "../../query-runner/QueryRunner"
9+
import { TableIndexOptions } from "../../schema-builder/options/TableIndexOptions"
610
import { Table } from "../../schema-builder/table/Table"
11+
import { TableCheck } from "../../schema-builder/table/TableCheck"
12+
import { TableColumn } from "../../schema-builder/table/TableColumn"
13+
import { TableExclusion } from "../../schema-builder/table/TableExclusion"
714
import { TableForeignKey } from "../../schema-builder/table/TableForeignKey"
815
import { TableIndex } from "../../schema-builder/table/TableIndex"
9-
import { QueryRunnerAlreadyReleasedError } from "../../error/QueryRunnerAlreadyReleasedError"
10-
import { View } from "../../schema-builder/view/View"
11-
import { Query } from "../Query"
12-
import { AuroraMysqlDriver } from "./AuroraMysqlDriver"
13-
import { ReadStream } from "../../platform/PlatformTools"
14-
import { OrmUtils } from "../../util/OrmUtils"
15-
import { TableIndexOptions } from "../../schema-builder/options/TableIndexOptions"
1616
import { TableUnique } from "../../schema-builder/table/TableUnique"
17-
import { BaseQueryRunner } from "../../query-runner/BaseQueryRunner"
17+
import { View } from "../../schema-builder/view/View"
1818
import { Broadcaster } from "../../subscriber/Broadcaster"
19+
import { InstanceChecker } from "../../util/InstanceChecker"
20+
import { OrmUtils } from "../../util/OrmUtils"
21+
import { Query } from "../Query"
1922
import { ColumnType } from "../types/ColumnTypes"
20-
import { TableCheck } from "../../schema-builder/table/TableCheck"
2123
import { IsolationLevel } from "../types/IsolationLevel"
22-
import { TableExclusion } from "../../schema-builder/table/TableExclusion"
23-
import { TypeORMError } from "../../error"
2424
import { MetadataTableType } from "../types/MetadataTableType"
25-
import { InstanceChecker } from "../../util/InstanceChecker"
25+
import { AuroraMysqlDriver } from "./AuroraMysqlDriver"
2626

2727
/**
2828
* Runs queries on a single mysql database connection.
@@ -186,25 +186,19 @@ export class AuroraMysqlQueryRunner
186186
/**
187187
* Returns raw data stream.
188188
*/
189-
stream(
189+
async stream(
190190
query: string,
191191
parameters?: any[],
192192
onEnd?: Function,
193193
onError?: Function,
194194
): Promise<ReadStream> {
195195
if (this.isReleased) throw new QueryRunnerAlreadyReleasedError()
196196

197-
return new Promise(async (ok, fail) => {
198-
try {
199-
const databaseConnection = await this.connect()
200-
const stream = databaseConnection.query(query, parameters)
201-
if (onEnd) stream.on("end", onEnd)
202-
if (onError) stream.on("error", onError)
203-
ok(stream)
204-
} catch (err) {
205-
fail(err)
206-
}
207-
})
197+
const databaseConnection = await this.connect()
198+
const stream = databaseConnection.query(query, parameters)
199+
if (onEnd) stream.on("end", onEnd)
200+
if (onError) stream.on("error", onError)
201+
return stream
208202
}
209203

210204
/**

src/driver/mysql/MysqlQueryRunner.ts

+79-103
Original file line numberDiff line numberDiff line change
@@ -187,132 +187,108 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
187187
): Promise<any> {
188188
if (this.isReleased) throw new QueryRunnerAlreadyReleasedError()
189189

190-
return new Promise(async (ok, fail) => {
191-
const databaseConnection = await this.connect()
190+
const databaseConnection = await this.connect()
192191

193-
this.driver.connection.logger.logQuery(query, parameters, this)
194-
await this.broadcaster.broadcast("BeforeQuery", query, parameters)
192+
this.driver.connection.logger.logQuery(query, parameters, this)
193+
await this.broadcaster.broadcast("BeforeQuery", query, parameters)
195194

196-
const broadcasterResult = new BroadcasterResult()
197-
const queryStartTime = Date.now()
195+
const broadcasterResult = new BroadcasterResult()
196+
const queryStartTime = Date.now()
198197

199-
try {
200-
const enableQueryTimeout =
201-
this.driver.options.enableQueryTimeout
202-
const maxQueryExecutionTime =
203-
this.driver.options.maxQueryExecutionTime
204-
const queryPayload =
205-
enableQueryTimeout && maxQueryExecutionTime
206-
? { sql: query, timeout: maxQueryExecutionTime }
207-
: query
208-
databaseConnection.query(
209-
queryPayload,
198+
try {
199+
const enableQueryTimeout = this.driver.options.enableQueryTimeout
200+
// log slow queries if maxQueryExecution time is set
201+
const maxQueryExecutionTime =
202+
this.driver.options.maxQueryExecutionTime
203+
const queryPayload =
204+
enableQueryTimeout && maxQueryExecutionTime
205+
? { sql: query, timeout: maxQueryExecutionTime }
206+
: query
207+
const raw = databaseConnection.query(queryPayload, parameters)
208+
209+
const queryEndTime = Date.now()
210+
const queryExecutionTime = queryEndTime - queryStartTime
211+
212+
if (
213+
maxQueryExecutionTime &&
214+
queryExecutionTime > maxQueryExecutionTime
215+
)
216+
this.driver.connection.logger.logQuerySlow(
217+
queryExecutionTime,
218+
query,
210219
parameters,
211-
async (err: any, raw: any) => {
212-
// log slow queries if maxQueryExecution time is set
213-
const maxQueryExecutionTime =
214-
this.driver.options.maxQueryExecutionTime
215-
const queryEndTime = Date.now()
216-
const queryExecutionTime = queryEndTime - queryStartTime
217-
218-
if (
219-
maxQueryExecutionTime &&
220-
queryExecutionTime > maxQueryExecutionTime
221-
)
222-
this.driver.connection.logger.logQuerySlow(
223-
queryExecutionTime,
224-
query,
225-
parameters,
226-
this,
227-
)
228-
229-
if (err) {
230-
this.driver.connection.logger.logQueryError(
231-
err,
232-
query,
233-
parameters,
234-
this,
235-
)
236-
this.broadcaster.broadcastAfterQueryEvent(
237-
broadcasterResult,
238-
query,
239-
parameters,
240-
false,
241-
undefined,
242-
undefined,
243-
err,
244-
)
245-
246-
return fail(
247-
new QueryFailedError(query, parameters, err),
248-
)
249-
}
220+
this,
221+
)
250222

251-
this.broadcaster.broadcastAfterQueryEvent(
252-
broadcasterResult,
253-
query,
254-
parameters,
255-
true,
256-
queryExecutionTime,
257-
raw,
258-
undefined,
259-
)
223+
this.broadcaster.broadcastAfterQueryEvent(
224+
broadcasterResult,
225+
query,
226+
parameters,
227+
true,
228+
queryExecutionTime,
229+
raw,
230+
undefined,
231+
)
260232

261-
const result = new QueryResult()
233+
const result = new QueryResult()
262234

263-
result.raw = raw
235+
result.raw = raw
264236

265-
try {
266-
result.records = Array.from(raw)
267-
} catch {
268-
// Do nothing.
269-
}
237+
try {
238+
result.records = Array.from(raw)
239+
} catch {
240+
// Do nothing.
241+
}
270242

271-
if (raw?.hasOwnProperty("affectedRows")) {
272-
result.affected = raw.affectedRows
273-
}
243+
if (raw?.hasOwnProperty("affectedRows")) {
244+
result.affected = raw.affectedRows
245+
}
274246

275-
if (useStructuredResult) {
276-
ok(result)
277-
} else {
278-
ok(result.raw)
279-
}
280-
},
281-
)
282-
} catch (err) {
283-
fail(err)
284-
} finally {
285-
await broadcasterResult.wait()
247+
if (useStructuredResult) {
248+
return result
249+
} else {
250+
return result.raw
286251
}
287-
})
252+
} catch (err) {
253+
this.driver.connection.logger.logQueryError(
254+
err,
255+
query,
256+
parameters,
257+
this,
258+
)
259+
this.broadcaster.broadcastAfterQueryEvent(
260+
broadcasterResult,
261+
query,
262+
parameters,
263+
false,
264+
undefined,
265+
undefined,
266+
err,
267+
)
268+
269+
throw new QueryFailedError(query, parameters, err)
270+
} finally {
271+
await broadcasterResult.wait()
272+
}
288273
}
289274

290275
/**
291276
* Returns raw data stream.
292277
*/
293-
stream(
278+
async stream(
294279
query: string,
295280
parameters?: any[],
296281
onEnd?: Function,
297282
onError?: Function,
298283
): Promise<ReadStream> {
299284
if (this.isReleased) throw new QueryRunnerAlreadyReleasedError()
300285

301-
return new Promise(async (ok, fail) => {
302-
try {
303-
const databaseConnection = await this.connect()
304-
this.driver.connection.logger.logQuery(query, parameters, this)
305-
const databaseQuery = databaseConnection.query(
306-
query,
307-
parameters,
308-
)
309-
if (onEnd) databaseQuery.on("end", onEnd)
310-
if (onError) databaseQuery.on("error", onError)
311-
ok(databaseQuery.stream())
312-
} catch (err) {
313-
fail(err)
314-
}
315-
})
286+
const databaseConnection = await this.connect()
287+
this.driver.connection.logger.logQuery(query, parameters, this)
288+
const databaseQuery = databaseConnection.query(query, parameters)
289+
if (onEnd) databaseQuery.on("end", onEnd)
290+
if (onError) databaseQuery.on("error", onError)
291+
return databaseQuery.stream()
316292
}
317293

318294
/**

0 commit comments

Comments
 (0)