0

Here is the config:

@Module({
  imports: [
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        type: 'postgres',
        host: configService.get('DB_HOST'),
        port: configService.get('DB_PORT'),
        username: configService.get('DB_USER'),
        password: configService.get('DB_PASSWORD'),
        database: configService.get('DB_NAME'),
        entities: [
          __dirname + '/../**/*.entity{.ts,.js}',
        ],
        // synchronize: true,
      })
    }),
  ],
})
export class DatabaseModule {}

The connections with the database itself is working, but when I'm trying to set up the migrations it throws the errors. What I've tried is to add the migration options in the above config and to create additional ormconfig.js with the configurations. Here is what I have in package.json file:

"typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js"

The problem is when I try to create migration it is not being created in migrations folder as I want and is not using the config above, how to solve it?

2
  • What's the errors throws ? Commented Mar 7, 2022 at 16:42
  • do you know that when using typeorm's CLI you didn't have the access to that DatabaseModule, right? Commented Mar 7, 2022 at 16:49

3 Answers 3

1

First, you need to set the migrations path in the module config

TypeOrmModule.forRootAsync({
    // other properties
    entities: [
      __dirname + '/../**/*.entity{.ts,.js}', // from the question
    ],
    migrations:[/*I assume you already know migration file paths*/]
})

then, To generate a migration file

npx typeorm migration:create -n FileName -d src/migrations

At this point, you need to call runMigrations(). add the below code in your main.ts

const conn = await getConnection('default'); // connection name is "default"
await conn.runMigrations();
Sign up to request clarification or add additional context in comments.

Comments

1

If you're using typeorm or 3.0 or newer versions you should do it straight on the cli:

typeorm migration:create src/migration_path

For older versions, you can add the following command to the ormconfig file:

  "cli": {
    "migrationDir": "./src/migration_path"
  }

1 Comment

This should be the correct answer. However, just one more thing, if anyone need custom name for migration file then: typeorm migration:create src/migrations/CoffeeRefactor
0

TypeORM CLI reads the configuration in a file ormconfig.json or ormconfig.js at the root of the project.

You need to extract this config in suck a file and then import this file in your DatabaseModule to have both things working.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.