LiJell's 성장기

createDescendantsQueryBuilder — TypeORM 본문

Back-End/TypeORM

createDescendantsQueryBuilder — TypeORM

All_is_LiJell 2022. 8. 11. 16:06
반응형

When you work with tree entities in TypeORM, you may need to use createDescendantsQueryBuilder. This post will let you know a thing that you need to care for using createDescendantsQueryBuilder.

What is TypeORM

ORM stands for Object Relational Mapping, and TypeORM is known as the most popular library that offers a diverse features to link TypeScript applications up to database and manage its. Among the TypeORM features, this article will point out the Closure table patterns in this post.

Closure Table

According to gitbook, it says “Closure table stores relations between parent and child in a separate table in a special way. It’s efficient in both reads and writes.” You may need to use createDescendantsQueryBuilder when you query hierarchical data that is designed by Closure table pattern.

Whereas createQueryBuilder literally creates a new query builder that can be used to build a query, createDescendantsQueryBuilder creates a query builder with a condition. The difference is createDescendantsQueryBuilder can be used when you need to get descendants of the entities in a tree.

createDescendantsQueryBuilder

Typeorm Github

If you see the picture above, createDescendantsQueryBuilder return values that are filtered by condition. The point is createDescendantsQueryBuilder not only creates a query builder but also apply condition by using where.

According to typeorm, it says “If you had previously WHERE expression defined, calling this function will override previously set WHERE conditions.”

Therefore, you must always use andWhere if you want to query with more conditions while createDescendantsQueryBuilder since where is already being used.

TypeORM Tree Entities

This is because the given example of createDescendantsQueryBuilder on TypeORM web page is also using andWhere when adding more conditions.

What you need to remember!

Finally, what you need to remember is createDescendantsQueryBuilder and where cannot stick together. PERIOD


Reference

반응형
Comments