TypeORM生成空迁移

时间:2019-07-07 17:28:54

标签: node.js json typescript typeorm

每当我运行typeorm migration:generate -n NAME时,我得到的只是一个错误,指出我没有对数据库进行任何更改。每当我运行typeorm migration:create -n NAME时,我都会得到一个空的迁移文件。我所有的实体都位于ormconfig.json文件中指定的文件夹中,并且为.ts格式。运行migration:generate命令时,出现与我实体中的语法有关的错误(特别是在文件顶部有导入的位置)。

这是我的ormconfig.json

{
  "name": "default",
  "type": "postgres",
  "host": "localhost",
  "port": 5432,
  "username": "postgres",
  "password": "admin",
  "database": "classmarker",
  "synchronize": true,
  "logging": false,
  "entities": [
    "src/entity/*.ts"
  ],
  "subscribers": [
    "src/subscriber/*.ts"
  ],
  "migrations": [
    "src/migration/*.ts"
  ],
  "cli": {
    "entitiesDir": "src/entity",
    "migrationsDir": "src/migration",
    "subscribersDir": "src/subscriber"
  }
}

我的package.json包含以下软件包:

  "dependencies": {
    "@tsed/common": "^5.21.0",
    "@tsed/core": "^5.21.0",
    "@tsed/di": "^5.21.0",
    "@types/mssql": "^4.0.15",
    "@types/node": "^12.0.12",
    "body-parser": "^1.19.0",
    "compression": "^1.7.4",
    "concurrently": "^4.1.1",
    "cookie-parser": "^1.4.4",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "express-handlebars": "^3.1.0",
    "method-override": "^3.0.0",
    "reflect-metadata": "^0.1.12",
    "pg": "^7.11.0",
    "typeorm": "^0.2.15"
  },
  "devDependencies": {
    "@types/express": "^4.17.0",
    "@types/node": "^9.6.5",
    "dotenv": "^8.0.0",
    "nodemon": "^1.19.1",
    "ts-node": "^3.3.0",
    "typescript": "^3.3.3333"
  }

我的tsconfig.json看起来像这样:

{
    "version": "2.4.2",
    "compilerOptions": {
        "lib": ["es5", "es6"],
        "target": "es6",
        "module": "commonjs",
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "sourceMap": true
    },
    "exclude": [
        "node_modules"
    ]
}

运行typeorm migration:generate -n Name时收到的错误:

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Function.PlatformTools.load (%AppData%\nvm\v8.11.2\node_modules\typeorm\platform\PlatformTools.js:107:28

1 个答案:

答案 0 :(得分:0)

当您尝试生成或运行.ts格式的迁移文件时,

onCreate往往会显示(我认为它是由于您尝试Unexpected token import您的import文件)。由于TypeORM可使用.js而不是.ts正常工作(不要问为什么),因此请尝试运行.ts生成迁移,并运行ts-node ./node_modules/typeorm/cli.js migration:generate -n NAME将其推送到数据库。

从本质上讲,在您的ts-node ./node_modules/typeorm/cli.js migration:run中添加这样的内容会更容易:

package.json

然后,您可以简单地使用 "add-migration": "ts-node ./node_modules/typeorm/cli.js migration:generate -n", "update-database": "ts-node ./node_modules/typeorm/cli.js migration:run" npm run add-migration -n NAME运行它们。

  

typeorm migration:create typeorm migration:generate 将创建ts文件。 migration:run migration:revert 命令仅适用于.js文件。因此,在运行命令之前,必须先编译打字稿文件。另外,您可以将ts-node与typeorm结合使用以运行.ts迁移文件。