question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use entities defined in external package

See original GitHub issue

Issue type:

[x] question

Database system/driver:

[x] postgres

TypeORM version:

[x] latest

Hi, I have an app where I’m creating connection in the following way:

import { entities } from "@mypackage/entities";

(async function() {
  const connectionOptionsReader = new TypeORM.ConnectionOptionsReader();
  const connectionOptions = (await connectionOptionsReader.all())[0];
  connectionOptions.entities.push(...entities);
  await TypeORM.createConnection(connectionOptions);

  /* ... */
})();

As you can see, I’m loading connectionOptions from config file (that part works properly). And later I’m adding entities from the @mypackage/models to the connectionOptions.entities list. I’ve checked the connectionOptions and it looks good. However, when I try to load something from the database I get the Context creation failed: No repository for \"User\" was found. Looks like this entity is not registered in current \"default\" connection? error.

Now, going to the package. It’s pretty straightforward.

import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";

@Entity({ name: "users" })
class User {
  @PrimaryGeneratedColumn({ type: "bigint" })
  readonly id: number;

  @Column({
    name: "first_name",
    type: "character varying",
    length: 50
  })
  firstName: string;

  @Column({
    name: "last_name",
    type: "character varying",
    length: 50
  })
  lastName: string;
}

const entities = [User];

export { entities };

I’m building this package with TS. Everything is built properly. I’ve read in some issue regarding Nest that this kind of operation is possible. But I’m probably missing something. Could you help? I haven’t found anything in docs

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
tdnghia98commented, May 20, 2020

Hi @lukejagodzinski , 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 lerna. The issue was that when I tried to use the repository in the other packages by importing the class from @app/common, I got the “No repository for <Entity name> was found.”. Would you mind sharing the architecture of your application please? Thanks a lot,

2reactions
abrar30commented, Jun 29, 2020

@tdnghia98 , hi man i was having same kind a problem , Here is my solution ! I tested as below : first way

 entities: [
      'src/modules/**/*Entity.ts',
       Test,   // directly import entity from common-node
    ],

Second way (suggested)

 entities: [
      'src/modules/**/*Entity.ts',
      '../common-node/src/modules/**/*Entity.ts',
    ],

Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

How use external entities in nestjs project with typeorm?
This module import/export the entities defined on the package. Thus, I could inject the entities on the main application. nestjs · typeorm.
Read more >
External Entities | Mendix Documentation
1 Introduction External entities can be added to the Domain Model through the Data Hub pane. They are displayed as purple entity containers ......
Read more >
DTD - Entities - Tutorialspoint
DTD - Entities, Entities are used to define shortcuts to special characters within the XML documents. Entities can be primarily of four types...
Read more >
External Entities - Creating Data Structures - What is Decisions?
An External Entity will take in data that is not stored in Decisions and create an object that is used in the same...
Read more >
External Entity Definition - Law Insider
Define External Entity. means any individual, organization or government ... External Entity uses router rules, access control lists and segmentation on the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found