node.js - TypeORM 生成空迁移

标签 node.js json typescript typeorm

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

这是我的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"
    ]
}

运行typeormigration: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

最佳答案

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

意外的 token 导入往往会出现(我认为它的出现是由于您尝试在 .ts 文件顶部导入某些内容)。由于 TypeORM 适用于 .js 而不是 .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 NAMEnpm run update-database 简单地运行它们。

typeorm migration:create and typeorm migration:generate will create ts files. The migration:run and migration:revert commands only work on .js files. Thus the typescript files need to be compiled before running the commands. Alternatively you can use ts-node in conjunction with typeorm to run .ts migration files.

关于node.js - TypeORM 生成空迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56924509/

相关文章:

javascript - 使用 Jest 创建测试或模拟 DiscordJS Fetch/Catch

javascript - 如果对象包含 JavaScript 中的字母,如何返回对象

javascript - jQuery JSON循环遍历嵌套对象

c# - 具有数字成员的 Json 对象未绑定(bind)到 MVC Controller 中的 Object.IntegerProperty

typescript - 动态导入的返回类型是什么?

node.js - CORS预检 channel 未成功,React to Express

node.js - 有没有办法为基于浏览器的视频 uploader 设置最长持续时间?

angular - ngFor block 内模板中的异步管道触发 http GET 调用循环

typescript - 使用TypeScript,如何强烈键入mysql查询结果

node.js - node.js 中的地理编码工具