3
votes

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.

3

3 Answers

5
votes

You can pass the references to the classes to "entities" instead of the path, for example:

import {Entity1, Entity2} from '@models'

@Module({
    imports: [ TypeOrmModule.forRoot({
    type: 'postgres',
    host: 'postgres',
    port: 5432,
    username: 'myuser',
    password: 'mypass',
    database: 'mydb',
    entities: [
        Entity1, Entity2
    ],
    synchronize: true,
    }), AModule ],
    controllers: [],
    providers: [],
})
export class AppModule {}
0
votes

It seems you are importing .ts files from node_modules. I believe once you have built your npm package that these entities are now .js files. You should try modifying your expression so it can parse .js files.

Personally I never use ts-node as it brings this kind of troubles and debugging is not really available with it. I feel you should always use the node runtime and parsing only .js files (also, it is closer to production runtime).

0
votes

I think found a workaround for this problem;

we can import modules from npm module like this

import * as Models from "your-models-package";

then iterate them and store in an array

const models = [];
for (const key in Models) {
  if (Models.hasOwnProperty(key)) {
    const model = Models[key];
    models.push(model);
  }
}

finaly use in module definition

const dbConfig = TypeOrmModule.forRoot({
  type: "postgres",
  database: config.postgres.db,
  host: config.postgres.host,
  port: Number(config.postgres.port),
  username: config.postgres.user,
  password: config.postgres.pass,
  entities: models,
  synchronize: true,
});

I hope it helps

cheers