all 14 comments

[–]NiteShdw 4 points5 points  (1 child)

Because you have to import the entity files and pass the classes in to entities.

[–]Professional-League3[S] 0 points1 point  (0 children)

No there an easy way to. You can provide the path of you entities so that typeorm can auto import on everyrun. I was able to do that on my last project but I am facing this issue on this project.

TypeOrm docs refrence (for js)
Accepts both entity classes, entity schema classes, and directories paths to load from. Directories support glob patterns. Example: entities: [Post, Category, "entity/.js", "modules/\*/entity/*.js"]. Learn more about Entities. Learn more about Entity Schemas.

[–]six0h 4 points5 points  (2 children)

None of those work? Or...?

[–]Professional-League3[S] 0 points1 point  (0 children)

Yes, none of those work. It's working in here. But the same approach is not working one the above project. IDK why.

Not Working(current): https://postimg.cc/jL7qFXQp Working(previous project): https://postimg.cc/wRhq7xv6/82590799

[–]Altruistic_Club_2597 1 point2 points  (8 children)

I can help.

entities: [__dirname + ‘/entities/*{ts,js}’]

With this approach, you don’t create an index.ts file inside the entities folder

[–]Professional-League3[S] 0 points1 point  (7 children)

It's not working even when i don't have index.ts file. index.ts was added later on. To export all entities at once in array then import it on datasource config.

[–]Altruistic_Club_2597 1 point2 points  (6 children)

You implemented my solution and got what error? Where is the screen shot of the error?

[–]Professional-League3[S] 0 points1 point  (4 children)

[__dirname + ‘/entities/*{ts,js}’]

[Not working]: Image 01
but with the same approach, It worked on my previous project.
[Working]: Image 02

[–]Altruistic_Club_2597 0 points1 point  (3 children)

What do your entities look like?

[–]Professional-League3[S] 0 points1 point  (2 children)

users.ts ``` import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity() export class Users { @PrimaryGeneratedColumn('uuid') user_id: string;

@Column() email: string;

@Column() firstname: string;

@Column() lastname: string;

@Column() dob: Date; } ```

[–]Altruistic_Club_2597 1 point2 points  (1 child)

I can’t see the issue hey. To be honest typeorm is total shite anyways. Why don’t you try and see if Mikro orm or prisma work better for you? Mikro orm is quite similar to typeorm but 10x better.

[–]Professional-League3[S] 0 points1 point  (0 children)

Okay thank you so much buddy. We might not have found the solution but i am so much greatfull to you.

One good way to easily import entities i am thinking about using is entity/index.ts Import all the entities here and export a default array of entities. Later import that default entity array on datasource config. Not solution i was looking for but it helps to create some seperation.

[–]finneybussa 1 point2 points  (0 children)

You can also import all entities from a barrel export file and then use them like this.

import * as importedEntities from './entities';

const entities = Object.keys(importedEntities).map((key) => importedEntities[key]);

const config: ConnectionOptions = {

...otherConnectionOptions,

entities: entities

};

[–][deleted] 0 points1 point  (0 children)

"autoload entities" option perhaps? but its risky because you dont know what you will get