Comments (19)
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.
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.
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.
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.
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.
Eager relations are added in 0.1.0-alpha.44
. Docs can be found here.
from typeorm.
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.
@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.
@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.
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.
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.
What about returning a default value when join doesn't return any relation? Like an empty array or null
from typeorm.
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.
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.
That's why then, thanks :)
from typeorm.
@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.
@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.
@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.
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)
- Postgres geometry column is always being altered to type geometry in every migration despite already being type geometry
- QueryFailedError: read ECONNRESET HOT 7
- Using transactions in tests w/ NestJS & TypeORM
- QueryBuilder looks up the wrong joinAttr when entities have identical property names
- Simple SQL statement generation with Repository object
- SSL AMAZON RDS HOT 2
- typeorm Not Compatible with Latest Version of Mongo HOT 1
- Can not generate migrations with capacitor driver: "Cannot use import statement outside a module"d HOT 1
- No metadata for "book" was found.
- Cascade save on 1 to 1 relations HOT 1
- Switching to esnext - fields not part of "select" are returned with "undefined"
- (B)LOB streaming
- Upsert with generated columns
- Returntype for find() using FindOneOptions and relations
- Invalid use of take in MySQL
- Retrieving additional relationships after an entity has been retrieved and instantiated seems an obvious, useful and easy feature to implement.
- Add support for better-sqlite3 v10
- Property metadata override does not work when extending an entity class
- Why did timezone become `SYSTEM` HOT 1
- format code
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typeorm.