Skip to content

Commit

Permalink
feat: allows user to specify which mysql package should be used (#8771)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ginden committed Mar 22, 2022
1 parent edf27d9 commit 35106df
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/driver/mysql/MysqlConnectionOptions.ts
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
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

0 comments on commit 35106df

Please sign in to comment.