Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allows user to specify which mysql package should be used #8771

Merged
merged 1 commit into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/driver/mysql/MysqlConnectionOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ export interface MysqlConnectionOptions
*/
readonly flags?: string[]

/**
* TypeORM will automatically use package found in your node_modules, prioritizing mysql over mysql2,
* but you can specify it manually
*/
readonly connectorPackage?: "mysql" | "mysql2"

/**
* Replication setup.
*/
Expand Down
17 changes: 13 additions & 4 deletions src/driver/mysql/MysqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1057,9 +1057,15 @@ export class MysqlDriver implements Driver {
* Loads all driver dependencies.
*/
protected loadDependencies(): void {
const connectorPackage = this.options.connectorPackage ?? "mysql"
const fallbackConnectorPackage =
connectorPackage === "mysql"
? ("mysql2" as const)
: ("mysql" as const)
try {
// try to load first supported package
const mysql = this.options.driver || PlatformTools.load("mysql")
const mysql =
this.options.driver || PlatformTools.load(connectorPackage)
this.mysql = mysql
/*
* Some frameworks (such as Jest) may mess up Node's require cache and provide garbage for the 'mysql' module
Expand All @@ -1070,14 +1076,17 @@ export class MysqlDriver implements Driver {
*/
if (Object.keys(this.mysql).length === 0) {
throw new TypeORMError(
"'mysql' was found but it is empty. Falling back to 'mysql2'.",
`'${connectorPackage}' was found but it is empty. Falling back to '${fallbackConnectorPackage}'.`,
)
}
} catch (e) {
try {
this.mysql = PlatformTools.load("mysql2") // try to load second supported package
this.mysql = PlatformTools.load(fallbackConnectorPackage) // try to load second supported package
} catch (e) {
throw new DriverPackageNotInstalledError("Mysql", "mysql")
throw new DriverPackageNotInstalledError(
"Mysql",
connectorPackage,
)
}
}
}
Expand Down