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

Daniel Santos picture Daniel Santos · Sep 3, 2018 · Viewed 23.5k times · Source

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?

Answer

Risto Novik picture Risto Novik · Sep 3, 2018

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]
}