I'm trying to use type orm entities defined outside of the project. That is, I have two projects: models and core-api. Models is an npm package that exports the entities definition. So, from core-api(nestjs project) i'm trying to define the connection using those entities. Something like this:
@Module({
imports: [ TypeOrmModule.forRoot({
type: 'postgres',
host: 'postgres',
port: 5432,
username: 'myuser',
password: 'mypass',
database: 'mydb',
entities: [
__dirname + '../node_modules/something/models/src/*.entity.ts'
],
synchronize: true,
}), AModule ],
controllers: [],
providers: [],
})
export class AppModule {}
Then, in A module, I'm importing the A entity from the npm package:
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AsController } from './As.controller';
import { AsService } from './As.service';
import { A } from 'something/models';
@Module({
imports: [TypeOrmModule.forFeature([ A ])],
controllers: [AController],
providers: [AService],
})
export class AModule {}
This ends up with the next error:
RepositoryNotFoundError: No repository for "A" was found. Looks like this entity is not registered in current "https://stackoverflow.com/a/54191513/3943197https://stackoverflow.com/a/54191513/3943197default" connection?
I'm quite lost in how to follow since i don't understand how it works under the hood. I saw the code of typeorm and @nestjs/typeorm but I couldn't figure out how to solve it.
Thanks!
UPDATE:
I could make it work adding the entity class instead of a path like @serianox said. Also, the something/models
npm package exports a standalone nest module. This module import/export the entities defined on the package. Thus, I could inject the entities on the main application.