【问题标题】:NestJS project starting when i running migration generateNestJS 项目在我运行迁移生成时启动
【发布时间】:2022-07-13 00:22:16
【问题描述】:

当我运行npm run typeorm migration:generate -- -n CreateUserTable 迁移成功生成,但同时我的项目正在启动。当我运行migration:create 时一切正常并且项目没有启动。 我试过了

  1. ts-node 更改为node
  2. 运行npx typeorm 而不是npm run typeorm
  3. dist 文件夹更改为src
  4. 创建mig:gen 脚本

这是错误日志

> ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli "migration:generate" "-n" "CreateUserTable"

[Nest] 6648  - 2022-01-27 19:23:24     LOG [NestFactory] Starting Nest application...
[Nest] 6648  - 2022-01-27 19:23:24   ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...
AlreadyHasActiveConnectionError: Cannot create a new connection named "default", because connection with such name already exist and it
 now has an active connection session.
    at AlreadyHasActiveConnectionError.TypeORMError [as constructor] (C:\Users\whitered932\Projects\wr-todo\src\error\TypeORMError.ts:7
:9)
    at new AlreadyHasActiveConnectionError (C:\Users\whitered932\Projects\wr-todo\src\error\AlreadyHasActiveConnectionError.ts:8:9)    
    at ConnectionManager.create (C:\Users\whitered932\Projects\wr-todo\src\connection\ConnectionManager.ts:57:23)
    at C:\Users\whitered932\Projects\wr-todo\src\globals.ts:77:35
    at step (C:\Users\whitered932\Projects\wr-todo\node_modules\tslib\tslib.js:143:27)
    at Object.next (C:\Users\whitered932\Projects\wr-todo\node_modules\tslib\tslib.js:124:57)
    at fulfilled (C:\Users\whitered932\Projects\wr-todo\node_modules\tslib\tslib.js:114:62)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
[Nest] 6648  - 2022-01-27 19:23:24     LOG [InstanceLoader] AppModule dependencies initialized +7ms
[Nest] 6648  - 2022-01-27 19:23:24     LOG [InstanceLoader] TypeOrmModule dependencies initialized +2ms
Migration C:\Users\whitered932\Projects\wr-todo/src/migrations/1643293402567-CreateUserTable.ts has been generated successfully.
[Nest] 6648  - 2022-01-27 19:23:27     LOG [InstanceLoader] TypeOrmCoreModule dependencies initialized +3152ms
[Nest] 6648  - 2022-01-27 19:23:27     LOG [InstanceLoader] TypeOrmModule dependencies initialized +2ms
[Nest] 6648  - 2022-01-27 19:23:27     LOG [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 6648  - 2022-01-27 19:23:27     LOG [InstanceLoader] UserModule dependencies initialized +3ms
[Nest] 6648  - 2022-01-27 19:23:27     LOG [InstanceLoader] TaskModule dependencies initialized +1ms
[Nest] 6648  - 2022-01-27 19:23:28     LOG [RoutesResolver] TaskController {/task}: +344ms
[Nest] 6648  - 2022-01-27 19:23:28     LOG [RouterExplorer] Mapped {/task, GET} route +5ms
[Nest] 6648  - 2022-01-27 19:23:28     LOG [RouterExplorer] Mapped {/task/:id, GET} route +3ms
[Nest] 6648  - 2022-01-27 19:23:28     LOG [RouterExplorer] Mapped {/task, POST} route +2ms
[Nest] 6648  - 2022-01-27 19:23:28     LOG [RouterExplorer] Mapped {/task/:id, PATCH} route +3ms
[Nest] 6648  - 2022-01-27 19:23:28     LOG [RouterExplorer] Mapped {/task/:id, DELETE} route +3ms
[Nest] 6648  - 2022-01-27 19:23:28     LOG [RoutesResolver] UserController {/user}: +3ms
[Nest] 6648  - 2022-01-27 19:23:28     LOG [NestApplication] Nest application successfully started +7ms
Завершить выполнение пакетного файла [Y(да)/N(нет)]? Y

这是我的文件 package.json 脚本

 "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json",
    "typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js"
  },

ormconfig.json

{
    "type": "postgres",
    "host": "localhost",
    "port": 5432,
    "username": "postgres",
    "password": "postgres",
    "database": "postgres",
    "entities": ["dist/**/*.js"],
    "migrations": ["dist/migrations/*.js"],
    "cli": {
        "migrationsDir": "src/migrations"
    }
}

【问题讨论】:

    标签: nestjs typeorm


    【解决方案1】:

    尝试在您的 ormconfig.json 文件中添加 keepConnectionAlive

    【讨论】:

      猜你喜欢
      • 2020-12-30
      • 1970-01-01
      • 2019-08-23
      • 1970-01-01
      • 2021-08-05
      • 2020-01-07
      • 2018-11-06
      • 1970-01-01
      • 2014-06-14
      相关资源
      最近更新 更多