postgresql - 迁移文件中的 NestJS TypeORM 语法错误

标签 postgresql typescript nestjs typeorm

我正在浏览 NestJS 官方文档。我在 Heroku 上设置了 PostgreSQL,与 TypeORM 连接,运行迁移,然后我的应用程序开始崩溃。我尝试了不同的方法并在 github/questions here 上搜索了博客/问题,但没有任何帮助。

这是一个错误:

[Nest] 46723   - 05/10/2020, 6:33:42 PM   [InstanceLoader] TypeOrmModule dependencies initialized +84ms
[Nest] 46723   - 05/10/2020, 6:33:43 PM   [TypeOrmModule] Unable to connect to the database. Retrying (1)... +493ms
/Users/Shared/diploma/be/migration/1589119433066-AddUser.ts:1
import {MigrationInterface, QueryRunner} from "typeorm";
       ^

SyntaxError: Unexpected token {
    at Module._compile (internal/modules/cjs/loader.js:721:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:690:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Function.PlatformTools.load (/***/PROJECT_ROOT/node_modules/typeorm/platform/PlatformTools.js:114:28)
    at /***/PROJECT_ROOT/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:39:69
    at Array.map (<anonymous>)

这是我的ormconfig.json:

  "type": "postgres",
  "url": "postgres://***",
  "ssl": true,
  "extra": {
    "ssl": {
      "rejectUnauthorized": false
    }
  },
  "entities": ["dist/**/*.entity{.ts,.js}"],
  "migrationsTableName": "custom_migration_table",
  "migrations": ["migration/*{.ts,.js}"],
  "cli": {
    "migrationsDir": "migration"
  }
}

迁移是使用 ts-node ./node_modules/.bin/typeorm migration:generate -n AddUser 生成的 我正在使用 nest start --watch 命令启动应用。

迁移文件{TIMESTAMP}-AddUser.ts:

import {MigrationInterface, QueryRunner} from "typeorm";

export class AddUser1589119433066 implements MigrationInterface {
    name = 'AddUser1589119433066'

    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`CREATE TABLE "users" (...)`, undefined);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`DROP TABLE "users"`, undefined);
    }

}

最佳答案

感谢@Isolated! 我已经更改了 ormconfig.json,所以实体和迁移文件现在看起来像这样,它对我来说工作正常:

"entities": ["dist/**/*.entity{.ts,.js}"],
"migrations": ["dist/migration/*{.ts,.js}"],

关于postgresql - 迁移文件中的 NestJS TypeORM 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61715052/

相关文章:

sql - 是否可以在 postgreSQL 中按子字符串排序?

node.js - 无法在 View 目录 NestJs 中查找 View "index"

validation - 如何使用 NestJS 和类验证器手动测试输入验证

postgresql - Postgres - 从记录变量动态引用列

sql - 在单个列上进行复杂选择

javascript - d3js/Angular 8 应用程序问题

angular - 带 ngx-translate 的 Ionic 3 - 延迟加载在浏览器中有效,但在构建后在 android 中无效

typescript - Nestjs GRPC 不带参数的服务方法

java - 通过 Hibernate 从 PostgreSQL 数据库随机选择

javascript - 有 Angular ;错误: Component NewPharmacyComponent is not part of any NgModule or the module has not been imported into your module