为什么我的TypeORM无法以产品模式连接数据库?

时间:2019-09-16 05:53:39

标签: nestjs typeorm

我正在使用Nest.js + TypeORM进行开发并尝试在计算机中进行部署。

我可以在develop mode中连接到mysql,但是在product mode中却失败了。

下面是我的TypeORM配置。

@Module({
  imports: [
    AuthModule,
    ClassificationModule,
    ArticleModule,
    UserModule,
    CommentModule,
    FragmentModule,
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      // logging: ["query"],
      port: 3306,
      username: 'root',
      password: '123456',
      database: 'myblog',
      entities: ['src/**/**.entity{.ts,.js}'],
      synchronize: true
    })
  ]
})
export class AppModule { }

develop mode中,它可以成功连接到mysql。

enter image description here

但是在product mode中,它显示无法连接mysql。

enter image description here

1 个答案:

答案 0 :(得分:3)

ts-node在内存中管理您的打字稿编译,并在内部处理从srcdist的引用更改。但是,当您开始运行纯node变体时,这是一个问题,因为您将位于dist目录中,而不是src中,因此TypeORM将无法找到具有已定义实体数组的实体。相反,您应该使用entities: [join(__dirname, '/**/*.entity.{js,ts}')],来允许在ts和js之间动态更改,因此您不必担心运行的是什么。

我还建议使用tsc-watch进行开发,因为默认情况下它将在成功编译时运行node,而您不必担心ts-node的内存消耗。