简体   繁体   中英

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.

尝试在 ormconfig.json 或 .env 中更改您的实体目录,它对我有用:

"entities": ["dist/**/**.entity{.ts,.js}"]

You should specify the connection in ormconfig.json in the project root folder.

looking something like this

{
    "type": "postgres",
    "host": "localhost",
    "port": <port>,
    "username": "",
    "password": "",
    "database": "",
    "entities": ["dist/**/*.entity{.ts,.js}"],
    "synchronize": true,
    "logging": "all",
    "migrations": [
        "migrations/**/*.js"
    ],
    "subscribers": [
        "subscriber/**/*.js"
    ],
    "cli": {
        "migrationsDir": "<migrations directory>",
        "subscribersDir": "<subscriber directory>"
    }
}

Read more in the documentation chapter

On a side note, you might have to remove your dist/ folder for changes to get through.

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}'],
      }),
 }),

you can try the following: I do not use an .env file, I use a configuration file directly I hope it serves you.

create config.ts file

paste this:

export const MYSQL = {
    HOST: 'localhost',
    PORT: 3306,
    USERNAME: 'db_user',
    PASSWORD: 'db_pass',
    DATABASE: 'db_name'
};

In the file where is your connection configuration write. In my case it's app module:

import { MYSQL } from './config/config';

TypeOrmModule.forRoot({
      type: 'mysql',
      host: MYSQL.HOST,
      port: MYSQL.PORT,
      username: MYSQL.USERNAME,
      password: MYSQL.PASSWORD,
      database: MYSQL.DATABASE,
      entities: [__dirname + '/**/*.entity{.ts,.js}'],
      synchronize: true,
    }),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM