【发布时间】:2020-03-13 02:57:34
【问题描述】:
经过几个小时的挖掘,我需要你的帮助!
上下文
我目前正在使用堆栈创建(早期阶段)一个应用程序:Nx(monorepo) + NestJS + TypeOrm
这是我的 ormconfig 文件:
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "***",
"password": "****",
"database": "****",
"synchronize": false,
"logging":false,
"entities": ["apps/api/src/app/**/**.entity.ts"],
"migrations":["apps/api/src/migration/**.ts"],
"cli":{
"migrationsDir":["apps/api/src/migration"],
"entitiesDir":["apps/api/src/app/**/**.entity.ts"]
}
}
这是我的迁移文件:
import {MigrationInterface, QueryRunner, Table} from "typeorm";
export class users1573343025001 implements MigrationInterface {
public async up (queryRunner: QueryRunner): Promise<any> {
await queryRunner.createTable(new Table({
name: 'users',
columns: [
{ name: 'id', type: 'bigint', isPrimary: true,
isGenerated: true, generationStrategy: 'increment', unsigned: true },
{ name: 'username', type: 'varchar', isNullable: false },
{ name: 'password', type: 'varchar', isNullable: true },
]
}))
}
public async down (queryRunner: QueryRunner): Promise<any> {
await queryRunner.dropTable('users')
}
}
问题
当我运行命令 ng serve api 来运行我的后端时,我遇到了这个问题:
SyntaxError: Unexpected token {...
错误来自我的迁移文件:apps\api\src\migration\1573343025001-users.ts:1
什么让我烦恼
如果我使用 typeorm 命令运行迁移,typeorm 可以毫无问题地运行它。 迁移 users1573343025001 已成功执行! 所以我不明白为什么迁移文件在迁移期间但在运行期间对我的应用程序看起来是正确的。
我已经尝试过的
- 围绕这个主题的很多答案是:将迁移目录更改为 dist/migration。但我只是想为应用提供服务,而不是构建它。
- 使用 typeorm 命令重新创建文件
- 验证我的 package.json 是否有行:
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js",在 typescript 中执行 - npm install(谁知道?)
- 删除迁移并运行命令ng serve api,应用启动时没有任何错误迹象
我可能错过了这些对我来说是新的技术的基本内容。 希望这一切都足够清楚,让您了解情况。
谢谢,
塞伯
【问题讨论】:
标签: typescript migration nestjs typeorm unexpected-token