简体   繁体   English

嵌套:无法创建名为“默认”的新连接,因为具有该名称的连接已经存在并且它现在具有活动连接 session

[英]Nest: Cannot create a new connection named "default", because connection with such name already exist and i t now has an active connection session

I am trying to create a seeder file in nestjs, the problem is when I run the project using start:dev, somehow nestjs also start seed.ts file with main.ts file.我正在尝试在nestjs中创建一个播种器文件,问题是当我使用start:dev运行项目时,nestjs也以main.ts文件启动seed.ts文件。 How can I make nestjs not start seed file when I run the project either on prod/dev but seed must be called only when I run the seed script.当我在 prod/dev 上运行项目时,如何使 nestjs 不启动种子文件,但只有在运行种子脚本时才必须调用种子。

Here is the seed.ts code这是seed.ts代码

 import { NestFactory } from '@nestjs/core' import { Logger } from '@nestjs/common' import { SeederModule } from './database/seeder.module' import { Seeder } from './database/seeder' async function bootstrap() { NestFactory.createApplicationContext(SeederModule).then((appContext) => { const logger = appContext.get(Logger) const seeder = appContext.get(Seeder) seeder.seedRoles().then(() => { logger.debug('Seeding Roles complete.') }).catch((error) => { logger.error('Seeding Roles failed.') throw error }) seeder.seedAdmin().then(() => { logger.debug('Seeding Admin complete.') }).catch((error) => { logger.error('Seeding Admin failed!') throw error }) .finally(() => appContext.close()) }) .catch((error) => { throw error }) } bootstrap()
Here is the main.ts file for nestJS 这是 nestJS 的 main.ts 文件

 import { Logger, ValidationPipe } from '@nestjs/common' import { NestFactory } from '@nestjs/core' import { AppModule } from './modules/app/app.module' async function bootstrap() { const app = await NestFactory.create(AppModule) app.enableCors() const port = process.env.PORT || 3000 app.useGlobalPipes(new ValidationPipe()) await app.listen(port).then(() => { Logger.log(`App listening on port ${port}`) }).catch((err) => { Logger.log(`Error while connecting to port ${port}`, err) }) } bootstrap()

And here is the package.json file这是 package.json 文件

 { "name": "jugg-website", "version": "0.0.1", "description": "", "author": "", "private": true, "license": "UNLICENSED", "scripts": { "prebuild": "rimraf dist", "build": "nest build", "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "start": "nest start", "start:dev": "link-module-alias && 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", "seed": "ts-node -r tsconfig-paths/register src/seed.ts" }, "dependencies": { "@nestjs/common": "^7.6.13", "@nestjs/core": "^7.6.13", "@nestjs/jwt": "^7.2.0", "@nestjs/passport": "^7.1.5", "@nestjs/platform-express": "^7.6.13", "@nestjs/typeorm": "^7.1.5", "class-transformer": "^0.4.0", "class-validator": "^0.13.1", "config": "^3.3.6", "link-module-alias": "^1.2.0", "passport": "^0.4.1", "pg": "^8.5.1", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^6.6.6", "typeorm": "^0.2.32" }, "devDependencies": { "@nestjs/cli": "^7.5.6", "@nestjs/schematics": "^7.2.7", "@nestjs/testing": "^7.6.13", "@types/express": "^4.17.11", "@types/jest": "^26.0.20", "@types/node": "^14.14.31", "@types/supertest": "^2.0.10", "@typescript-eslint/eslint-plugin": "^4.15.2", "@typescript-eslint/parser": "^4.15.2", "eslint": "^7.20.0", "eslint-config-prettier": "^8.1.0", "eslint-plugin-prettier": "^3.3.1", "jest": "^26.6.3", "prettier": "^2.2.1", "supertest": "^6.1.3", "ts-jest": "^26.5.2", "ts-loader": "^8.0.17", "ts-node": "^9.1.1", "tsconfig-paths": "^3.9.0", "typescript": "^4.1.5" }, "jest": { "moduleFileExtensions": [ "js", "json", "ts" ], "rootDir": "src", "testRegex": ".*\\.spec\\.ts$", "transform": { "^.+\\.(t|j)s$": "ts-jest" }, "collectCoverageFrom": [ "**/*.(t|j)s" ], "coverageDirectory": "../coverage", "testEnvironment": "node" } }

you can try setting KeepConnectionAlive option to true in typeOrm configuration in the app.module file https://github.com/nestjs/typeorm/issues/61您可以尝试在 app.module 文件https://github.com/nestjs/typeorm/issues/61的 typeOrm 配置中将 KeepConnectionAlive 选项设置为 true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM