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.

Build Typeorm Entity Workaround into @nestjs/typeorm module

See original GitHub issue

Feature 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:

  1. 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$/)
  })
})
  1. 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()
  })
})
  1. Automatically load entities specified in .forFeature so that no entities need to be specified at the root.

  2. 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:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
kamilmysliwieccommented, Mar 10, 2020
6reactions
kamilmysliwieccommented, Dec 5, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Database | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines ...
Read more >
NestJS: The code first approach with TypeOrm | by exFabrica
Our job is to create specific classes called “Entities” and use an ORM to convert the classes into tables. NestJS manage out of...
Read more >
How to workraound this TypeORM error, "EntityRepository is ...
First of all: npm install @nestjs/typeorm@next. NOTE. In my project @nestjs/typeorm version is 9.0.0-next.2 and typeorm version is 0.3.6.
Read more >
Using TypeORM's QueryBuilder in NestJS - LogRocket Blog
Set up NestJS. Generate basic app setup; Configure TypeORM with SQLite; Create database entities · Run basic queries with TypeORM QueryBuilder in ......
Read more >
Top 5 @nestjs/typeorm Code Examples - Snyk
Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues...
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