Coder Social home page Coder Social logo

Comments (19)

pleerock avatar pleerock commented on May 22, 2024 10
        let loadedPost = await connection.getRepository(Post).findOneById(post.id);

findOneById never joins relations by itself, you need to specify manually what relations you want to join. This is implemented this way because your object can have tons of relations, which can be nested deep and even recursive, thats why its not possible to always join all relations. User needs to be explicit what he wants. You have two choices: use query builder or provide find options to findOneById method. Docs for both you can find on site

from typeorm.

MichalLytek avatar MichalLytek commented on May 22, 2024 9

findOneById never joins relations by itself, you need to specify manually what relations you want to join. This is implemented this way because your object can have tons of relations, which can be nested deep and even recursive, thats why its not possible to always join all relations.

It's not an user friendly solution. People use ORM to simplify data access - just use @Decorator and don't worry about foreing keys, joins and other stuffs.

If someone have relation which might be bigger (not one-to-one) eg. 1000 relations, and he don't want do load it all, he can use lazy relations. But when I add new relation to my class I have to remember to change joins in all query builder or make another layer with custom repository which will do that.

I think we should stick to the idea of Hibernate - eager and lazy loding. QueryBuilder or joins in find should be used only to make custom joins on columns that aren't defined as relations.

from typeorm.

pleerock avatar pleerock commented on May 22, 2024 4

It will look like this:

leftJoinAndSelect: {
    category: "product.category",
    user: "category.user"
}

Also its generally better to use query builder for complex queries:

const results = await repository.createQueryBuilder("product")
    .leftJoinAndSelect("product.category", "category") 
    .leftJoinAndSelect("category.user", "category.user")
    .getResults();

from typeorm.

pleerock avatar pleerock commented on May 22, 2024 2

People use ORM to simplify data access - just use @decorator and don't worry about foreing keys, joins and other stuffs.

actually you are not worrying about keys or joins. You are just telling which relation you need to load.

But I got your point, I have some ideas how to add automatic joins, i'll try to implement them later. Until that use query builder or find options

from typeorm.

pleerock avatar pleerock commented on May 22, 2024 2

BTW, @pleerock could you update repository types to TS 2.0? findOne and findOneById should return Promise<Entity | undefined> πŸ˜‰

@19majkel94 done in 57e13f6

from typeorm.

pleerock avatar pleerock commented on May 22, 2024 2

Eager relations are added in 0.1.0-alpha.44. Docs can be found here.

from typeorm.

MichalLytek avatar MichalLytek commented on May 22, 2024 1

If it's OneToMany, you will get an empty array. If it's OneToOne or ManyToOne - undefined.
Of course when it works ok - now it's not working due to wrong sql syntax 😜

from typeorm.

jcrben avatar jcrben commented on May 22, 2024 1

@19majkel94 if it's so easy couldn't you you have made a pull request? doesn't seem right to hassle someone who is already so busy and volunteering his time...

from typeorm.

pleerock avatar pleerock commented on May 22, 2024 1

@19majkel94 is awesome guy and made lot of contributions in different other libraries, strange that he is still not in contributors list of typeorm

from typeorm.

MichalLytek avatar MichalLytek commented on May 22, 2024

You are just telling which relation you need to load.

Ok, but how to deep/nested join? I mean in this chain Product -> Category -> User I can do

leftJoinAndSelect: {
    category: "product.category"
}

to join category to product but with no user info. But what to do to join user to category? I tried "category.user": "category.user" and other combination but no luck - I got error in SQL (...) category.user AS category_user, category.user.id AS category.user_id 😒

from typeorm.

MichalLytek avatar MichalLytek commented on May 22, 2024

Thanks! I thought that it will join to product.user, not to product.category.user πŸ˜‰

QueryBuilder are more powerfull but my custom repository is extending an Repository to have access to persist or create without copy-paste boilerplate code.

from typeorm.

mibanez avatar mibanez commented on May 22, 2024

What about returning a default value when join doesn't return any relation? Like an empty array or null

from typeorm.

MichalLytek avatar MichalLytek commented on May 22, 2024

In non lazy relation, if you do leftJoinAndSelect you will get undefined on relation property and on innerJoinAndSelect you will get undefined object (like returning no rows in select query). Haven't tested the lazy relations yet due to this #46 bug.

BTW, @pleerock could you update repository types to TS 2.0? findOne and findOneById should return Promise<Entity | undefined> πŸ˜‰

from typeorm.

mibanez avatar mibanez commented on May 22, 2024

Mmm that makes sense, specially when the relation is not loaded. Now, if the relation is explicitly loaded and no rows are returned, wouldn't be better to return an empty array? I mean, the relation does exist and was required so i'm expecting it as a member of my entity.

from typeorm.

mibanez avatar mibanez commented on May 22, 2024

That's why then, thanks :)

from typeorm.

pleerock avatar pleerock commented on May 22, 2024

@19majkel94 PRs haha 😈 This is something that needs to be done (and easy to do in most obvious places ;)) Right now Im pretty busy with refactoring of persistent mechanizm to solve some small and hard fixable bugs...

from typeorm.

MichalLytek avatar MichalLytek commented on May 22, 2024

@pleerock It's 15s change in function's return type declarations - do it in next npm release, PM is waste of time and resources 😜

from typeorm.

MichalLytek avatar MichalLytek commented on May 22, 2024

@pleerock thanks ☺️
typeorm is really big library so it's not as easy to contribute like class-validator or routing-controllers and I have no experience with ORMs so I can only suggest in issues what I would like to have or change the behavior

@jcrben
I left this for pleerock because updating this two types was just a part of upgrading to TS 2.0 - I haven't know which other functions or methods might needed type change as find* was.

from typeorm.

pleerock avatar pleerock commented on May 22, 2024

thats easy to get started with something small and little by little make bigger and bigger contributions. There are really lot of small tasks that can be implemented easily ;)

from typeorm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.