fix: improve async calls on disconnect (#11523)

This commit is contained in:
Lucian Mocanu 2025-06-19 00:43:38 +02:00 committed by GitHub
parent b1e93f736d
commit ead4f98ee4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 91 additions and 77 deletions

View File

@ -88,6 +88,7 @@
"test": "npm run compile && npm run test:fast --",
"test:ci": "mocha --bail",
"test:fast": "mocha",
"typecheck": "tsc --noEmit",
"watch": "tsc --watch"
},
"dependencies": {

View File

@ -11,12 +11,12 @@
"reflect-metadata": "^0.2.2",
"sql.js": "^1.13.0",
"ts-node": "^10.9.2",
"typeorm": "0.3.22"
"typeorm": "*"
},
"devDependencies": {
"@types/node": "^20.11.24",
"@types/node": "^20.19.0",
"@types/sql.js": "^1.4.9",
"typescript": "^5.3.3"
"typescript": "^5.8.3"
}
},
"node_modules/@cspotcode/source-map-support": {
@ -109,11 +109,12 @@
"dev": true
},
"node_modules/@types/node": {
"version": "20.17.32",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.17.32.tgz",
"integrity": "sha512-zeMXFn8zQ+UkjK4ws0RiOC9EWByyW1CcVmLe+2rQocXRsGEDxUCwPEIVgpsGcLHS/P8JkT0oa3839BRABS0oPw==",
"version": "20.19.0",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.19.0.tgz",
"integrity": "sha512-hfrc+1tud1xcdVTABC2JiomZJEklMcXYNTVtZLAeqTVWD+qL5jkHKT+1lOtqDdGxt+mB53DTtiz673vfjU8D1Q==",
"license": "MIT",
"dependencies": {
"undici-types": "~6.19.2"
"undici-types": "~6.21.0"
}
},
"node_modules/@types/sql.js": {
@ -381,6 +382,20 @@
}
}
},
"node_modules/dedent": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/dedent/-/dedent-1.6.0.tgz",
"integrity": "sha512-F1Z+5UCFpmQUzJa11agbyPVMbpgT/qA3/SKyJ1jyBgm7dUcUEa8v9JwDkerSQXfakBwFljIxhOJqGkjUwZ9FSA==",
"license": "MIT",
"peerDependencies": {
"babel-plugin-macros": "^3.1.0"
},
"peerDependenciesMeta": {
"babel-plugin-macros": {
"optional": true
}
}
},
"node_modules/diff": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz",
@ -806,9 +821,10 @@
"integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="
},
"node_modules/typeorm": {
"version": "0.3.22",
"resolved": "https://registry.npmjs.org/typeorm/-/typeorm-0.3.22.tgz",
"integrity": "sha512-P/Tsz3UpJ9+K0oryC0twK5PO27zejLYYwMsE8SISfZc1lVHX+ajigiOyWsKbuXpEFMjD9z7UjLzY3+ElVOMMDA==",
"version": "0.3.24",
"resolved": "https://registry.npmjs.org/typeorm/-/typeorm-0.3.24.tgz",
"integrity": "sha512-4IrHG7A0tY8l5gEGXfW56VOMfUVWEkWlH/h5wmcyZ+V8oCiLj7iTPp0lEjMEZVrxEkGSdP9ErgTKHKXQApl/oA==",
"license": "MIT",
"dependencies": {
"@sqltools/formatter": "^1.2.5",
"ansis": "^3.17.0",
@ -816,6 +832,7 @@
"buffer": "^6.0.3",
"dayjs": "^1.11.13",
"debug": "^4.4.0",
"dedent": "^1.6.0",
"dotenv": "^16.4.7",
"glob": "^10.4.5",
"sha.js": "^2.4.11",
@ -922,9 +939,10 @@
}
},
"node_modules/undici-types": {
"version": "6.19.8",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz",
"integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw=="
"version": "6.21.0",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz",
"integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==",
"license": "MIT"
},
"node_modules/uuid": {
"version": "11.1.0",

View File

@ -12,11 +12,11 @@
"reflect-metadata": "^0.2.2",
"sql.js": "^1.13.0",
"ts-node": "^10.9.2",
"typeorm": "0.3.22"
"typeorm": "*"
},
"devDependencies": {
"@types/node": "^20.11.24",
"@types/node": "^20.19.0",
"@types/sql.js": "^1.4.9",
"typescript": "^5.3.3"
"typescript": "^5.8.3"
}
}

View File

@ -346,8 +346,9 @@ export class CockroachDriver implements Driver {
* Closes connection with database.
*/
async disconnect(): Promise<void> {
if (!this.master)
return Promise.reject(new ConnectionIsNotSetError("cockroachdb"))
if (!this.master) {
throw new ConnectionIsNotSetError("cockroachdb")
}
await this.closePool(this.master)
await Promise.all(this.slaves.map((slave) => this.closePool(slave)))

View File

@ -260,11 +260,13 @@ export class MongoDriver implements Driver {
* Closes connection with the database.
*/
async disconnect(): Promise<void> {
if (!this.queryRunner) throw new ConnectionIsNotSetError("mongodb")
// const handler = (err: any) => (err ? fail(err) : ok())
this.queryRunner.databaseConnection.close()
const queryRunner = this.queryRunner
if (!queryRunner) {
throw new ConnectionIsNotSetError("mongodb")
}
this.queryRunner = undefined
// return ok()
await queryRunner.databaseConnection.close()
}
/**

View File

@ -445,8 +445,9 @@ export class MysqlDriver implements Driver {
* Closes connection with the database.
*/
async disconnect(): Promise<void> {
if (!this.poolCluster && !this.pool)
return Promise.reject(new ConnectionIsNotSetError("mysql"))
if (!this.poolCluster && !this.pool) {
throw new ConnectionIsNotSetError("mysql")
}
if (this.poolCluster) {
return new Promise<void>((ok, fail) => {

View File

@ -343,8 +343,9 @@ export class OracleDriver implements Driver {
* Closes connection with the database.
*/
async disconnect(): Promise<void> {
if (!this.master)
return Promise.reject(new ConnectionIsNotSetError("oracle"))
if (!this.master) {
throw new ConnectionIsNotSetError("oracle")
}
await this.closePool(this.master)
await Promise.all(this.slaves.map((slave) => this.closePool(slave)))

View File

@ -585,8 +585,9 @@ export class PostgresDriver implements Driver {
* Closes connection with database.
*/
async disconnect(): Promise<void> {
if (!this.master)
return Promise.reject(new ConnectionIsNotSetError("postgres"))
if (!this.master) {
throw new ConnectionIsNotSetError("postgres")
}
await this.closePool(this.master)
await Promise.all(this.slaves.map((slave) => this.closePool(slave)))

View File

@ -1,5 +1,6 @@
import {
ColumnType,
ConnectionIsNotSetError,
DataSource,
EntityMetadata,
ObjectLiteral,
@ -324,9 +325,13 @@ export class SapDriver implements Driver {
* Closes connection with the database.
*/
async disconnect(): Promise<void> {
const promise = this.master.clear()
const pool = this.master
if (!pool) {
throw new ConnectionIsNotSetError("sap")
}
this.master = undefined
return promise
await pool.clear()
}
/**

View File

@ -330,9 +330,9 @@ export class SqlServerDriver implements Driver {
* Closes connection with the database.
*/
async disconnect(): Promise<void> {
if (!this.master)
return Promise.reject(new ConnectionIsNotSetError("mssql"))
if (!this.master) {
throw new ConnectionIsNotSetError("mssql")
}
await this.closePool(this.master)
await Promise.all(this.slaves.map((slave) => this.closePool(slave)))
this.master = undefined

View File

@ -8,7 +8,7 @@ import {
reloadTestingDatabases,
} from "../../../../utils/test-utils"
describe("mysql driver > enableQueryTimeout connection option", () => {
describe("driver > mysql > connection options > enableQueryTimeout", () => {
let dataSources: DataSource[]
const commonConnectionOptions: TestingOptions = {
entities: [__dirname + "/entity/*{.js,.ts}"],
@ -34,16 +34,13 @@ describe("mysql driver > enableQueryTimeout connection option", () => {
beforeEach(() => reloadTestingDatabases(dataSources))
after(() => closeTestingConnections(dataSources))
it("should throw a query execution timeout error for the query when it exceeds the maxQueryExecutionTime", async () => {
await Promise.all(
it("should throw a query execution timeout error for the query when it exceeds the maxQueryExecutionTime", () =>
Promise.all(
dataSources.map(async (dataSource) => {
let errorThrown = false
try {
await dataSource.manager.query(
`SELECT SLEEP(${longQueryTimeSec})`,
)
} catch (err) {
errorThrown = true
await expect(
dataSource.manager
.sql`SELECT SLEEP(${longQueryTimeSec})`,
).to.eventually.be.rejected.then((err) => {
expect(err).to.have.nested.property(
"driverError.code",
"PROTOCOL_SEQUENCE_TIMEOUT",
@ -52,27 +49,19 @@ describe("mysql driver > enableQueryTimeout connection option", () => {
"driverError.timeout",
timeoutMs,
)
}
expect(errorThrown).to.be.true
})
}),
)
})
))
it("should not throw a query execution timeout error for the query when it runs within the maxQueryExecutionTime", async () => {
await Promise.all(
it("should not throw a query execution timeout error for the query when it runs within the maxQueryExecutionTime", async () =>
Promise.all(
dataSources.map(async (dataSource) => {
let errorThrown = false
try {
await dataSource.manager.query(
`SELECT SLEEP(${shortQueryTimeSec})`,
)
} catch (err) {
errorThrown = true
}
expect(errorThrown).to.be.false
await expect(
dataSource.manager
.sql`SELECT SLEEP(${shortQueryTimeSec})`,
).to.eventually.be.fulfilled
}),
)
})
))
})
describe("when enableQueryTimeout is not provided", () => {
@ -87,20 +76,14 @@ describe("mysql driver > enableQueryTimeout connection option", () => {
after(() => closeTestingConnections(datasources))
it("should not throw a query execution timeout error", () => {
it("should not throw a query execution timeout error", () =>
Promise.all(
datasources.map(async (dataSource) => {
let errorThrown = false
try {
await dataSource.manager.query(
`SELECT SLEEP(${longQueryTimeSec})`,
)
} catch (err) {
errorThrown = true
}
expect(errorThrown).to.be.false
await expect(
dataSource.manager
.sql`SELECT SLEEP(${longQueryTimeSec})`,
).to.eventually.be.fulfilled
}),
)
})
))
})
})

View File

@ -7,7 +7,7 @@ import {
import { DataSource } from "../../../../src/data-source/DataSource"
import { expect } from "chai"
describe("postgres specific options", () => {
describe("driver > postgres > connection options", () => {
let connections: DataSource[]
before(
async () =>
@ -21,7 +21,7 @@ describe("postgres specific options", () => {
beforeEach(() => reloadTestingDatabases(connections))
after(() => closeTestingConnections(connections))
it("should set application_name", () =>
it("should set session variable application_name", () =>
Promise.all(
connections.map(async (connection) => {
const result = await connection.query(

View File

@ -8,7 +8,7 @@ import {
import { DataSource } from "../../../../src/data-source/DataSource"
import { Post } from "./entity/Post"
describe("driver > convert raw results to entity", () => {
describe("query builder > convert raw results to entity", () => {
let connections: DataSource[]
before(
async () =>

View File

@ -1,10 +1,11 @@
import { expect } from "chai"
import "reflect-metadata"
import { DataSource } from "../../../src/data-source/DataSource"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { DataSource } from "../../../src/data-source/DataSource"
import { ActivityEntity } from "./entity/ActivityEntity"
describe("github issues > #320 Bug in getManyAndCount", () => {
@ -51,7 +52,7 @@ describe("github issues > #320 Bug in getManyAndCount", () => {
.setParameter("tiles", tiles)
.setParameter("tileCount", tiles.length)
await query.getMany
await expect(query.getMany()).to.eventually.be.fulfilled
}),
))
})