Advanced Queries with TypeORM QueryBuilder

To interact with our database, we use convenient global methods provided by the TypeORM repository, such as findOneBy, save, update, and delete. However, as our virtual library evolves, it requires more flexibility in crafting complex queries or handling dynamic conditions. This section will explore the efficient use of QueryBuilder to search for books, allowing us to find them based on various criteria.

What is QueryBuilder?

The QueryBuilder tool is a powerful feature in TypeORM, allowing for more complex database operations. With it, we can craft intricate queries tailored to specific needs. Let’s use it to refactor our findBooks function.

Setting up the base query

The foundation of any query using the QueryBuilder is setting up the target entity and alias:

Get hands-on with 1200+ tech skills courses.