advanced
Query builder
Drop to TypeORM query builder when repository helpers cannot express joins, locking, pagination, or partial selects cleanly.
TypeORM query builder handles complex selects, joins, subqueries, locks, and partial projections when repository helpers are insufficient. Treat it as SQL design with aliases and parameter binding.
await dataSource
.createQueryBuilder('order')
.innerJoin('order.customer', 'customer')
.where('order.status = :status', { status: 'open' })
.setLock('pessimistic_write')
.getMany();
On interviews: joins, aliases, pagination, locking, selected columns, raw results, generated SQL inspection.
Common pitfalls: offset pagination with joins wrong or slow; raw mapping bypassing entity validation assumptions.
The trade-off is flexibility versus ORM abstraction leaks.
Checklist:
- Bind parameters.
- Inspect SQL and plans.
- Test pagination with real join cardinality.
- Document lock and isolation needs.