TypeORM cannot find entities if entity directory was not set in configuration files

Viewed 51105

I'm using TypeORM with the fallowing configuration file: ormconfig.json

{
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "root",
"password": "my-secret-pw",
"database": "mytestdb",
}

My Entities files are stored on the ./src/bar/entity directory. I always get the following error:

RepositoryNotFoundError: No repository for "myTable" was found. Looks like this entity is not registered in current "default" connection?

The Entity is found when I manually add the directory to the configuration file:

{
...
"entities": ["src/bar/entity/**/*.ts"]
}

My Entity is defined like:

@Entity('myTable')
export default class MyTable {
    @PrimaryGeneratedColumn()
    public id: number;
    ...

How can I allow the TypeORM to find those entities without setting manually in the configuration file for each directory?

9 Answers

The most common case you described is to have separate entities directory which consists only of Entity declarations.

{
...
"entities": ["src/bar/entities/**/*.ts"]
}

Another approach would be importing each entity separately:

import {User} from "./payment/entity/User";
import {Post} from "./blog/entity/Post";

{
...
"entities": [User, Post]
}

For me it helped to include also src directory to ormconfig.json:

  "entities": [
    "dist/**/*.entity{.ts,.js}",
    "src/**/*.entity{.ts,.js}"
  ],

If you don't want to put all entities in the same place (ie if you have module folders and want to put entities in their associated module folders), and if you are using a file naming convention like foo.entity.ts, foo.service.ts, etc. then you can do the following and it will find all entities wherever they are in your source tree:

{
  ...
  "entities": ["src/**/*{.entity.ts}"],
}

Using Nest.js in order to work for both development and production environment, I had to do like this:

entities: [
   this.isProduction() ? 
       path.join(__dirname, '../**/**.entity{.ts,.js}') : '**/*.entity{.ts,.js}',
],

// ....

private isProduction(): boolean {
    const mode = this.configService.get('NODE_ENV');
    return mode !== 'development';
}

My problem was that my users entity file was in lowercase, when I capitalised it - all worked.

This works for me.

entities: [
  path.join(
    __dirname,
    process.env.NODE_ENV === 'development' ?
    '/**/*.entity{.ts,.js}' :
    '/**/*.entity.js', // afaik building stuffs are js-only
  ),
]

It might be an older question, but now it is possible to set autoloadEntities: true.

Just do:

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      ...
      autoLoadEntities: true,
    }),
  ],
})
export class AppModule {}

This snippet was extracted from the NestJS documentation.

Notice that the entities have to be registered through the forFeature method.

Make sure that the name of the entity file is not plural. For example if you're creating a post entity file, it should not be "posts.entity.ts" instead it should be "post.entity.ts"

Related