Build Typeorm Entity Workaround into @nestjs/typeorm module
See original GitHub issueFeature Request
Is your feature request related to a problem? Please describe.
Many new Nest users are experiencing this (or related) error:
No repository for "Permission" was found. Looks like this entity is not registered in current "default" connection?
This is because NestJS currently doesn’t support file globs for defining entity locations. To get around this, many people are manually referencing all of their models in the root of their application. This is dangerous for large apps, since you’re technically leaking implementation details into the root of the application.
When implementing TypeormModule, you have to specify entities in two places, at the root of the application in forRoot
, and when you import TypeormModule via forResources
. This seems a little redundant, and can result in leaking implementation details to other parts of the app. See this link for an example. Just imagine doing this workaround with 100+ models in various nested modules that you didn’t necessarily write: https://stackoverflow.com/questions/52155315/typeorm-cannot-find-entities-if-entity-directory-was-not-set-in-configuration-fi/52155464
Describe the solution you’d like
I’d like to suggest one of several solutions, based on workarounds from the community, and my own perspective:
- Create a util to automatically extract entities from Webpack See https://github.com/nestjs/nest/issues/755#issuecomment-394073763
Ideally this would look something like:
...
import { TypeormModule, getEntitiesFromWebpack } from @nestjs/typeorm
@Module({
...
TypeormModule.forRoot({
...
entities: getEntitiesFromWebpack(/\.entity\.ts$/)
})
})
- Create a util to automatically extract entities from Typeorm. See https://github.com/nestjs/nest/issues/755#issuecomment-496793495
...
import { TypeormModule, getEntities } from @nestjs/typeorm
@Module({
...
TypeormModule.forRoot({
...
entities: getEntities()
})
})
-
Automatically load entities specified in
.forFeature
so that no entities need to be specified at the root. -
Create a custom decorator to be used in addition to or instead of the default TypeORM @Entity decorator.
Teachability, Documentation, Adoption, Migration Strategy
See the above examples
What is the motivation / use case for changing the behavior?
Eliminate implementation detail leaks, improve adoptability.
This is meant as a conversation starter, not an exhaustive list of solutions. Some of the solutions aren’t mutually exclusive either. Please chime in if you have better ideas. Let me know if there’s any other way I can help.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:11 (4 by maintainers)
Added in the latest release. See docs https://docs.nestjs.com/techniques/database#auto-load-entities
@jmcdo29 the problem is that this won’t work with
webpack
. I do agree with @robbyemmert, having a way to automatically register all entities without explicitly importing them in the root makes total sense.