使用 NestJS 和 TypeORM 为项目生成迁移文件 [英] Generating migration file for a project using NestJS and TypeORM

查看:258
本文介绍了使用 NestJS 和 TypeORM 为项目生成迁移文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的实体生成迁移文件,但是每当我运行命令来创建实体时,它都会创建一个空"的文件.文件,只创建了 up 和 down 方法.

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.

我在 package.json 文件中添加了这个脚本:"typeorm":"node --require ts-node/register ./node_modules/typeorm/cli.js".

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

在我的 app.module.ts 中,连接是这样配置的:

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

其中 database() 它是一个 nestjs 配置文件,并从 .env 文件中获取值.

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

我用来创建迁移的脚本是:npm run typeorm migration:create -- -n QuestionTables -d src/migrations 其中需要指定 -dstrong>,否则不会创建迁移文件(即使在forRoot方法的cli中指定.

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.

我是否需要手动编写 SQL 来创建表?

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

如果我需要向现有表添加新列,我应该创建一个新的迁移文件并手动编写 SQL 代码来添加它吗?

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?

我尝试运行的另一个命令是:npm run typeorm migration:generate -- -n QuestionTables -d src/migrations 在这里它给了我一个错误:"错误:在任何 orm 配置文件中都找不到连接选项."

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."

推荐答案

npm run typeorm migration:create 命令会生成空的迁移文件.

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

迁移自动生成的命令是:npm run typeorm migration:generate

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

如您收到的错误中所述,您需要为 cli 指定配置文件.这意味着应该将传递给 forRoot 的配置提取到 ts/json 文件中.为此,您需要 2 个文件,1 个用于服务器连接,另一个用于迁移配置.

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.

例如:

// 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"
}

这篇关于使用 NestJS 和 TypeORM 为项目生成迁移文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆