Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrations been added to root folder not migration folder

Tags:

typeorm

nestjs

I am trying to set up migrations for Nestjs TypeORM, in my TypeOrmModule.forRoot() i have added the desired folder for the migrations, but it keeps adding the migrations to the root folder.

TypeOrmModule.forRoot({
  type: 'mssql',
  host: 'test',
  port: 1,
  username: 'test',
  password: 'test',
  database: 'test',
  entities: [__dirname + '/**/entities/*{.ts,.js}'],
  synchronize: false,
  options: {
    useUTC: true,
  },
  migrations: [__dirname + '/**/migration/*.ts'],
  cli: {
    migrationsDir: __dirname + '/**/migration',
  },
})
like image 675
user3182518 Avatar asked Oct 26 '25 01:10

user3182518


2 Answers

I had the same issue, I just added the -d option in the CLI command to specify the directory , like this :

ts-node ./node_modules/typeorm/cli.js migration:generate -n migration -d src/infrastructure/migrations
like image 60
PedroMiotti Avatar answered Oct 29 '25 15:10

PedroMiotti


I think this might be because of when you create a migration you're probably using the typeorm package right? (like so typeorm migration:create -n PostRefactoring). Which will use an entirely different config to that which you've specified in your nest application. The easiest way I suppose would be to create an env file and use TYPEORM_MIGRATIONS_DIR to define your migration directory.

See here for available env options http://typeorm.io/#/using-ormconfig/using-environment-variables. You can then link up your envs with your application so they're defined in one place.

I don't want to be that person that goes around advertising their own packages, you could easily achieve your own setup if you wish. I built a config module which you can use for typeorm configs like so

https://github.com/nestjs-community/nestjs-config#typeorm

import {Module} from '@nestjs/common';
import {ConfigModule, ConfigService} from 'nestjs-config';
import {TypeOrmModule} from '@nestjs/typeorm';
import * as path from 'path';

@Module({
    imports: [
        ConfigModule.load(path.resolve(__dirname, 'config/**/*.{ts,js}')),
        TypeOrmModule.forRootAsync({
            useFactory: (config: ConfigService) => config.get('database'),
            inject: [ConfigService],
        }),
    ],
})
export class AppModule {}

This would allow you to define your configs in a file like so

//src/config/database.ts
export default {
    type: 'mssql',
    host: process.env.TYPEORM_HOST,
    port: process.env.TYPEORM_PORT,
    username: process.env.TYPEORM_USERNAME,
    password: process.env.TYPEORM_PASSWORD,
    database: process.env.TYPEORM_DATABASE,
    entities: [process.env.TYPEORM_ENTITIES],
    synchronize: process.env.TYPEORM_SYNCHRONIZE == 'true',
    migrationsDir: process.env.TYPEORM_MIGRATIONS_DIR
};

Then your .env

TYPEORM_HOST=test
TYPEORM_USERNAME=test
TYPEORM_PASSWORD=test
TYPEORM_PORT=1
TYPEORM_MIGRATIONS_DIR=src/migrations

Now you'll be able to use the typeorm command and you'll still have your database configs defined in one place. Hope this helps!

like image 23
bashleigh Avatar answered Oct 29 '25 15:10

bashleigh