question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Injecting custom repositories that use the default connection is broken on 6.1.0

See original GitHub issue

I’m submitting a…


[x] Regression 
[ ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

On @nestjs/typeorm 6.1.0, if you use a custom repository and the default connection, the repository cannot be injected anywhere. 6.0.0 did not have this issue.

The app crashes with:

[Nest] 28596   - 04/30/2019, 3:50 PM   [ExceptionHandler] Nest can't resolve dependencies of the AppController (?). Please make sure that the argument at index [0] is available in the AppModule context. +11ms
Error: Nest can't resolve dependencies of the AppController (?). Please make sure that the argument at index [0] is available in the AppModule context.
    at Injector.lookupComponentInExports (/home/brianmcd/work/typeorm-issue/node_modules/@nestjs/core/injector/injector.js:180:19)
    at processTicksAndRejections (internal/process/next_tick.js:81:5)
    at process.runNextTicks [as _tickCallback] (internal/process/next_tick.js:51:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:777:11)
    at Object.<anonymous> (/home/brianmcd/work/typeorm-issue/node_modules/ts-node/src/bin.ts:157:12)
    at Module._compile (internal/modules/cjs/loader.js:721:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
 1: 0x946a50 node::Abort() [/home/brianmcd/.nvm/versions/node/v11.7.0/bin/node]
 2: 0x9b34f9  [/home/brianmcd/.nvm/versions/node/v11.7.0/bin/node]
 3: 0xb97dca  [/home/brianmcd/.nvm/versions/node/v11.7.0/bin/node]
 4: 0xb98979 v8::internal::Builtin_HandleApiCall(int, v8::internal::Object**, v8::internal::Isolate*) [/home/brianmcd/.nvm/versions/node/v11.7.0/bin/node]
 5: 0x2f2a22ecfc5d 
Aborted (core dumped)

Expected behavior

Custom repositories should work.

Minimal reproduction of the problem with instructions

https://github.com/brianmcd/nest-typeorm-bug

I think what is happening is that custom repositories are getting setup with a malformed injection token.

The repository provider token is created here: https://github.com/nestjs/typeorm/blob/0a6c085353bca2adfc9e613971baca2e7dc7b47d/lib/typeorm.providers.ts#L29-L38

With a default connection, this calls getRepositoryToken(entity, DEFAULT_CONNECTION_NAME).

getRepositoryToken calls getConnectionToken(connection) on line 31.

https://github.com/nestjs/typeorm/blob/0a6c085353bca2adfc9e613971baca2e7dc7b47d/lib/common/typeorm.utils.ts#L24-L41

For the default connection, getConnectionToken returns Connection, not DEFAULT_CONNECTION_NAME.

https://github.com/nestjs/typeorm/blob/0a6c085353bca2adfc9e613971baca2e7dc7b47d/lib/common/typeorm.utils.ts#L61-L71

The bug manifests itself here: https://github.com/nestjs/typeorm/blob/0a6c085353bca2adfc9e613971baca2e7dc7b47d/lib/common/typeorm.utils.ts#L33

connectionToken is the class Connection, which isn’t DEFAULT_CONNECTION_NAME, so the prefix is set to a stringified version of the Connection constructor. You end up with a very weird injection token, which doesn’t match the Repository’s name, so the dependency can’t be found when you try to inject it.

Environment


@nestjs/typeorm version: 6.1.0

 
For Tooling issues:
- Node version: XX  
- Platform:  Linux 

Others:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
doubliezcommented, Apr 30, 2019

I encountered the same issue after updating from @nestjs/typeorm 6.0.0 to 6.1.0.

In my tests I’m injecting the repository like this:

const module = await Test.createTestingModule({
    controllers: [UserController],
    providers: [
        UserService,
        {
            provide: 'UserRepository',
            useClass: Repository
        }
    ]
}).compile();

In 6.0.0 this works fine and “UserRepository” matches the token used internally by @nestjs/typeorm.

In 6.1.0 this doesn’t work anymore, and I can confirm what was observed by @brianmcd: the token generated by @nestjs/typeorm is now prefixed with the stringified version of the Connection constructor.

2reactions
kamilmysliwieccommented, May 12, 2019

Thanks for reporting @brianmcd, fixed in 6.1.1 😃

PS. @doubliez you should use getRepositoryToken function instead of overriding the token by hand.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NestJS TypeOrmModule repository injection seems not ...
I'm trying to inject user repository to UserService, but it seems that typeorm connection is not making. I've googled about this error, but...
Read more >
kieService trying to connect to repo1.maven.org when ...
xml while starting the WildFly server, my Java EE application does only attempt to resolve the kjar artifacts only to internal repo. Please...
Read more >
Azure PowerShell release notes
Learn about all of the latest updates to the Azure PowerShell modules.
Read more >
release-note.txt\6.1.0\qt - qt/qtreleasenotes.git
You can turn off the deprecation warning using the "qt.qml.compiler" logging category. b7412dc866 QML: Warn about usage of injected signal parameters - The ......
Read more >
Upgrading your build from Gradle 5.x to 6.0
The previous step will help you identify potential problems by issuing deprecation warnings when a plugin does try to use a deprecated part...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found