mikro-orm/nestjs

Add Nest 8 support

Closed this issue ยท 20 comments

Nest 8 was released recently: https://github.com/nestjs/nest/releases/tag/v8.0.0

At first glance, it does not look like there are any breaking changes that would affect this package, so probably just the dependencies need to be updated in package.json

@mekwall is correct that there seems to be a problem with dependency injection due to Nest 8 using the instance vs name.

I did face a small issue in my Unit tests after upgrading to Nest 8. I used to get Repository classes using

moduleRef.get(UsageRepository);

Now I have to get the EntityManager and use it to get the Repository class, something like this.

em = moduleRef.get(EntityManager);
usageRepository = em.getRepository(Usage);

@inglkruiz Have you tried with getRepositoryToken?

import { EntityRepository } from "@mikro-orm/postgresql"
import { getRepositoryToken } from "@mikro-orm/nestjs"

const usageRepository = app.get<EntityRepository<Usage>>(getRepositoryToken(Usage))

I am pretty certain that the changes introduced in this PR broke the dependency injection of this package.

I am using in Nest 8 and works so far

i can't install with nestjs version 8 npm i -s @mikro-orm/nestjs --verbose

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: server@0.0.1
npm ERR! Found: @nestjs/common@8.0.2
npm ERR! node_modules/@nestjs/common
npm ERR!   @nestjs/common@"8.0.2" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer @nestjs/common@"^7.0.0" from @mikro-orm/nestjs@4.2.0
npm ERR! node_modules/@mikro-orm/nestjs
npm ERR!   @mikro-orm/nestjs@"*" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR! 

Install with --force, we need to update at least the peerDependecies (would be great to rush that, then works in the reported bugs)

Injecting Repositories seem to work fine, but injecting EntityManager (from @mikro-orm/postgresql to be specific) does not.

I get the following error:

Nest can't resolve dependencies of the RentalService (?, RentalRepository). Please make sure that the argument SqlEntityManager at index [0] is available in the RentalModule context.

Potential solutions:
- If SqlEntityManager is a provider, is it part of the current RentalModule?
- If SqlEntityManager is exported from a separate @Module, is that module imported within RentalModule?
  @Module({
    imports: [ /* the Module containing SqlEntityManager */ ]
  })

I am experiencing the same issue @Maikuh is experiencing after upgrading to NestJS 8.

I am experiencing the same issue as @Maikuh but I use the @mikro-orm/mysql package instead of the postgresql one.

Any progress on this? I'm having an issue with custom repositories that extend from EntityRepository. Basically, any custom methods that I define are coming back undefined at runtime...

B4nan commented

v4.3 stable is out

So is this fixed in 4.3? because I'm having the same problem with @Maikuh

I am also still experiencing this issue with 4.3

B4nan commented

This issue is already addressed, if you experience something similar, please open new issue with full reproduction.

I can confirm, no issues with this since v4.3 in August. Try deleting your node_modules dir and reinstalling packages.

thank you, was just about to update my comment, it is probably because of pnpm install, as mentioned in #41

B4nan commented

Will try to test it with pnpm too, but I am a bit overloaded these days.

In the meantime, as a workaround, we could use EntityManager from @mikro-orm/knex. As you said, it is the same thing, just reexported and the DI works as expected ๐Ÿ™‚ Thank you for your time!

To those experiencing this issue since upgrading to NestJS 8, using yarn, but not using pnpm: I was able solve the problem by matching the mysql2 dependency version in my project's package.json with the one used by @mikro-orm/mysql-base.

Not doing so will cause package-local installs of @mikro-orm/knex instead of a single global one, effectively breaking NestJS's DI. It wasn't a problem with NestJS 7 because it identified DI based on a string value (the class constructor's name) rather than a real reference to a class.

B4nan commented

Just came across this issue regarding pnpm - worth checking if the installation of @mikro-orm/knex package helps.

I was able solve the problem by matching the mysql2 dependency version in my project's package.json with the one used by @mikro-orm/mysql-base.

You should not install that package yourself (unless you work with it directly, not via the ORM). It is no longer a peer depedency - just install @mikro-orm/mysql that includes it in its direct/transitive dependencies.