Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestJs TypeORM unable to connect to mysql

I am trying to connect to MySQL. I have defined the db connection vars in a .env file in my root dir, and I am initializing the connection in the app.module.ts file. the only issue I am facing now is when creating or running migrations using the CLI, I followed the typeorm docs here to configure the connection, however when I run typeorm migrate:create -n myNewTable, it should create the migration file in the specified directory, what it does instead is it creates it in the app root directory, similarily, I solved the issue by using the -d flag after the typeorm migrate:create to specify the directory, however when I try running my migration files, I get this

No connection options were found in any of configurations file.

here is my app.module.ts file.

TypeOrmModule.forRoot({
      type: 'mysql',
      host: process.env.TYPEORM_HOST,
      port: parseInt(process.env.TYPEORM_PORT, 10),
      username: process.env.TYPEORM_USERNAME,
      password: process.env.TYPEORM_PASSWORD,
      database: process.env.TYPEORM_DATABASE,
      synchronize: false,
      migrations: [process.env.TYPEORM_MIGRATIONS],
      cli: {
        migrationsDir: process.env.TYPEORM_MIGRATIONS_DIR,
      },
      logging: (process.env.TYPEORM_LOGGING === 'true') ? true : false,
      entities: [__dirname + '/../**/*.entity{.ts,.js}'],
    }),
 

and here is my .env file

# use .ts for development, .js for production
TYPEORM_CONNECTION = mysql
TYPEORM_HOST = 127.0.0.1
TYPEORM_PORT = 3306
TYPEORM_USERNAME = <username>
TYPEORM_PASSWORD = <password>
TYPEORM_DATABASE = <dbname>
TYPEORM_SYNCHRONIZE = true
TYPEORM_MIGRATIONSRUN = true
TYPEORM_LOGGING = true
TYPEORM_ENTITIES = src/**/**.entity.ts
#TYPEORM_ENTITIES = src/**/**.entity.js
TYPEORM_SUBSCRIBERS = src/subscriber/*.ts
#TYPEORM_SUBSCRIBERS = src/subscriber/*.js
TYPEORM_MIGRATIONS = src/database/migration/*.ts
TYPEORM_MIGRATIONS_DIR = src/database/migration
TYPEORM_SUBSCRIBERS_DIR = src/subscriber

any help/hint is highly appreciated, thanks in advance.

like image 768
MoSwilam Avatar asked Dec 13 '22 12:12

MoSwilam


2 Answers

Try to change your entities dir in ormconfig.json or .env, it worked for me as:

"entities": ["dist/**/**.entity{.ts,.js}"]
like image 142
Alfredo Bautista Avatar answered Dec 18 '22 00:12

Alfredo Bautista


There is an issue, how you are loading the **TypeOrmModule**. The way, you had loaded, it is synchronous so it simply means custom env will not be available instantly on application boot.

So what you can do, is to load TypeOrmModule` asynchronusly like below -

 ConfigModule.forRoot({
  isGlobal: true, // [REQUIRED if want to use env gloablly among all modules]
 }),
 TypeOrmModule.forRootAsync({
      useFactory: () => ({
        type: 'mysql',
        host: process.env.TYPEORM_HOST,
        port: parseInt(process.env.TYPEORM_PORT, 10),
        username: process.env.TYPEORM_USERNAME,
        password: process.env.TYPEORM_PASSWORD,
        database: process.env.TYPEORM_DATABASE,
        synchronize: false,
        migrations: [process.env.TYPEORM_MIGRATIONS],
        cli: {
          migrationsDir: process.env.TYPEORM_MIGRATIONS_DIR,
        },
        logging: process.env.TYPEORM_LOGGING === 'true' ? true : false,
        entities: [__dirname + '/../**/*.entity{.ts,.js}'],
      }),
 }),
like image 41
Abhay Avatar answered Dec 18 '22 00:12

Abhay