我正在浏览 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/