How use external entities in nestjs project with typeorm?

nestjs/typeorm example
what is typeorm
typeorm migrations
nestjs docs
typeorm mongodb
typeorm create database
typeorm connection
nestjs sqlite

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.

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

How use external entities in nestjs project with typeorm?, 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  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:

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

NestJS, TypeORM and PostgreSQL, To have a good development and project setup right from the the setup - NestJS & typeORM connected to a PostgreSQL DB in a docker container What is nice about TypeORM is, that it allows you to model your data entities in type (​API to Database) and your external model (API-consumer to API). In this tutorial, we will be doing a NestJS TypeORM Pagination example. The example will be a products API. Setting up a NestJS GraphQL Project. First, let’s set up our project. We will generate a new NestJS project using the Nest CLI and then install the GraphQL module along with its peer dependencies. $> nest new products-api Adding TypeORM

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

nestjs/typeorm, still not working! can't inject relative entity to feature module. https://github.com/​nestjs/typeorm/blob/master/lib/interfaces/typeorm-options.interface.ts should be passed at Btw, It is your decision and I don't know your project specification. the setup - NestJS & typeORM connected to a PostgreSQL DB in a docker container. To build the API in NodeJS we will use NestJS.It’s a pretty flexible framework and is built on ExpressJS

Use entities defined in external package · Issue #4238 · typeorm , Do you use nestjs or only node? I currently have an issue with importing external entities, I tried to create a package at @app/common with all my entities decorated with type-orm, then sym link this package to two other packages using Would you mind sharing the architecture of your application please? For this example, using postgresql, I’d only have to run createdb dogs. Then, to be able to manipulate this database, I will use an ORM. I chose typeorm, which is also written in Typescript and recommended by the nestJS developers. Add typeorm and pg to the dependencies of the project: yarn add @nestjs/typeorm typeorm pg. Create Dogs entity

Database, keepConnectionAlive, If true , connection will not be closed on application shutdown To begin using the User entity, we need to let TypeORM know about it by  In this article we look at how to do database migrations using TypeORM and NestJS. For people who don’t know these frameworks: NestJS is an opinionated server-side application development

Complete guide to using TypeORM and TypeScript for data , In this article we'll learn about using TypeORM to set up Entity objects to for Node.js to handle storing data in a database for an application. A detailed step by a step video tutorial, on how you can integrate typeorm with nestjs framework using .env file. This talks about entities, migrations, running a migration from CLI, the issue with entities when running typescript code using ts-node, or compiled code using node. We will use yarn as a package manager.

Comments
  • Yes, that's one of the things that I needed. Thanks!