entrostat / typeorm-test-transactions

Automatically rollback database changes after each test is completed when using TypeORM
MIT License
63 stars 5 forks source link

Connection "default" was not found. #9

Closed highflying closed 4 years ago

highflying commented 4 years ago

I'm getting the error:

    ConnectionNotFoundError: Connection "default" was not found.

      at new ConnectionNotFoundError (../../node_modules/typeorm-transactional-cls-hooked/src/error/ConnectionNotFoundError.ts:8:9)
      at ConnectionManager.Object.<anonymous>.ConnectionManager.get (../../node_modules/typeorm-transactional-cls-hooked/src/connection/ConnectionManager.ts:40:19)
      at Object.getManager (../../node_modules/typeorm-transactional-cls-hooked/src/index.ts:262:35)
      at ../../node_modules/typeorm-transactional-cls-hooked/src/Transactional.ts:61:13
      at Object.<anonymous> (../../node_modules/typeorm-transactional-cls-hooked/src/hook.ts:23:23)
      at step (../../node_modules/typeorm-transactional-cls-hooked/dist/hook.js:32:23)
      at Object.next (../../node_modules/typeorm-transactional-cls-hooked/dist/hook.js:13:53)
      at ../../node_modules/typeorm-transactional-cls-hooked/dist/hook.js:7:71
      at Object.<anonymous>.__awaiter (../../node_modules/typeorm-transactional-cls-hooked/dist/hook.js:3:12)
      at Object.<anonymous>.exports.runAndTriggerHooks (../../node_modules/typeorm-transactional-cls-hooked/src/hook.ts:21:75)
      at ../../node_modules/typeorm-transactional-cls-hooked/src/hook.ts:44:12
      at ../../node_modules/cls-hooked/context.js:118:13
      at Namespace.run (../../node_modules/cls-hooked/context.js:97:5)
      at Namespace.runAndReturn (../../node_modules/cls-hooked/context.js:117:8)
      at Object.<anonymous> (../../node_modules/typeorm-transactional-cls-hooked/src/hook.ts:42:24)

This is when running a test under runInTransaction after having created a default connection with createConnection

dan-klasson commented 4 years ago

post your code as well

kerren commented 4 years ago

Hey @highflying I've had this before, it's when you don't name your connection. So, for example, when you're creating a TypeORM connection, the JSON object would look like this:

{
    type: 'postgres',
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    name: 'default',
    database: process.env.DB_DATABASE
}

Check out the example for more than one connection in the TypeORM documentation here.