Search code examples
mysqlnode.jstypeormmysql-connector

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


Solution

  • 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.