Generating migration file for a project using NestJS and TypeORM

28,974

The command npm run typeorm migration:create will generate empty migration file.

The command for migrations auto generation is: npm run typeorm migration:generate

As written in the error you received you need to specify the configuration file for the cli. Than means should extract the configuration passed to forRoot to a ts/json file. You'll need 2 files for that, 1 for the server's connection and another for migrations configuration.

For example:

// ormconfig.ts
export const config: TypeOrmModuleOptions = {
      type: 'mysql',
      host: database().host,
      port: parseInt(database().port),
      username: database().username,
      password: database().password,
      database: database().schema,
      entities: [Question, QuestionOption], // maybe you should also consider chage it to something like:  [__dirname + '/**/*.entity.ts', __dirname + '/src/**/*.entity.js']
      migrations: ['src/migration/*{.ts,.js}'],
      cli: {
        migrationsDir: 'src/migration'
      },
      synchronize: true,
    }

// ormconfig-migrations.ts

import {config} from './ormconfig';

export = config;

import {config} from './ormconfig';

TypeOrmModule.forRoot(config);
// package.json

"scripts": {
     ...
     "typeorm:cli": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli -f ./ormconfig-migrations.ts",
     "migration-generate": "npm run typeorm:cli -- migration:generate -n"
}
Share:
28,974
Maturano
Author by

Maturano

I'm a .Net Software Developer, with experience working with .Net Core to build APIs, Xamarin Forms and React. I'm open-minded to learn new things and I love to share knowledge.

Updated on July 09, 2022

Comments

  • Maturano
    Maturano almost 2 years

    I'm trying to generate migration files for my entities, but whenever I run the command to create the entity, it creates an "empty" file, just the up and down methods are created.

    I have added this script in my package.json file: "typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js".

    In my app.module.ts, the connection is configured like this:

     TypeOrmModule.forRoot({
          type: 'mysql',
          host: database().host,
          port: parseInt(database().port),
          username: database().username,
          password: database().password,
          database: database().schema,
          entities: [Question, QuestionOption],
          migrations: ['src/migration/*{.ts,.js}'],
          cli: {
            migrationsDir: 'src/migration'
          },
          synchronize: true,
        })
    

    Where database() it's a nestjs config file and get the values from an .env file.

    The script I'm using to create the migration is: npm run typeorm migration:create -- -n QuestionTables -d src/migrations where need to specify the -d, otherwise the migration file is not created (even if it's specified in the cli of the forRoot method.

    Do I need to write manually the SQL to create the tables?

    What if I need to add a new column to an existing table, should I create a new migration file and write manually the SQL code to add that?

    Another command that I tried to run was this one: npm run typeorm migration:generate -- -n QuestionTables -d src/migrations and here it gives me an error: " Error: No connection options were found in any orm configuration files."

  • Maturano
    Maturano over 3 years
    Alright, so now if I run the command npm run migration-generate Test, it says that No changes in database schema were found - cannot generate a migration. Do I need to create a migration and code manually the SQL inside of it to create the table?
  • noam steiner
    noam steiner over 3 years
    Have you already created any tables? If so than clear the schema, drop the tables or drop the schema and recreate it, and then run migration-generate. This error says your database already have tables matching to the entities in your project.
  • Maturano
    Maturano over 3 years
    Not yet, my database is empty.
  • noam steiner
    noam steiner over 3 years
    You have any migrations in the migrations table?
  • Maturano
    Maturano over 3 years
    I deleted everything from the database, there's only a database without any tables there. If I run the npm run migration-generate, I got a message saying that no changes in database schema were found. If I run migration-create, it creates a file in the migration folder, but with up and down method empty. Should it create a migration file with some SQL code or do I need to write that SQL code?
  • Maturano
    Maturano over 3 years
    Also, I modified the ormconfig.ts with this: entities: [__dirname + '/src/event/entities/*.entity.ts', __dirname + '/src/event/entities/*.entity.js'] src/event/entities is where my Typescript classes are right now
  • Maturano
    Maturano over 3 years
    Nevermind my friend, I found the error, my Entity class has a constructor that was causing some sort of cannot destructure property error. I fixed that and could run the generate command!
  • thisismydesign
    thisismydesign about 3 years
    Won't synchronize: true defeat the whole purpose? Whenever you initiation this connection it will always sync entities with the DB schema, therefore there are no migrations to be generated.
  • Renato Junior
    Renato Junior over 2 years
    It is not necessary to add the npm run