Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typeorm: force mysql2 if mysql is installed

I know this is a special use case, but I have both mysql and mysql2 packages installed and I need to test both of them. However as my understanding typeorm will first check if mysql is in the node_modules and use it.

How can I force mysql2 to be used instead? Or programmatically switch between them? Thanks

like image 915
Matteo Avatar asked Dec 19 '25 10:12

Matteo


1 Answers

I had the same problem recently, After a period of research (including reading the source code) I found a way.

1 Programmatically:

// for 0.2.x
{
  driver: PlatformTools.load('mysql2'),
  //...other options
}
// for 0.3.x
{
  connectorPackage: 'mysql2'
  //other options
}

2 Using yml configuration

Create ormconfig.yml in your project root, put an empty object to default.driver option. See below.

default:
  type: mysql #=TYPEORM_CONNECTION
  driver: {} #THIS'LL FORCE TO mysql2
  host: localhost #=TYPEORM_HOST
  port: 3306 #=TYPEORM_PORT
  username: xxxx #=TYPEORM_USERNAME
  password: xxxxx #=TYPEORM_PASSWORD
  database: xxx #=TYPEORM_DATABASE
  entities: #=TYPEORM_ENTITIES
    - dist/**/*.entity.js
  migrations: #=TYPEORM_MIGRATIONS
    - migration/*.js
  cli:
    migrationsDir: migration #=TYPEORM_MIGRATIONS_DIR

When use yml, make sure there is no other ormconfig.* in your path.

like image 120
Ryan Ouyang Avatar answered Dec 21 '25 03:12

Ryan Ouyang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!