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

Viewed 6638

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. 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.

Here is the seed.ts code

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

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

{
  "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"
  }
}

2 Answers

I was getting this error when running e2e tests. I found that the default nestjs template sets up the e2e tests with a beforeEach like this:

beforeEach(async () => {
  const moduleFixture: TestingModule = await Test.createTestingModule({
    imports: [AppModule],
  }).compile();

  app = moduleFixture.createNestApplication();
  await app.init();   

});

But since I had more than one test in the spec file (I'm a newbie to node.js dev so not sure whether this is the right thing to do or not, but regardless...), what was happening was the app was being initialised once per test which meant it was attempting to create multiple connections to the database.

The solution was quite simple. Change the beforeEach to beforeAll

beforeAll(async () => {
  const moduleFixture: TestingModule = await Test.createTestingModule({
    imports: [AppModule],
  }).compile();

  app = moduleFixture.createNestApplication();
  await app.init();   

});

And don't forget to close the app when the tests are done

afterAll(async () => {
  await app.close();
});
Related