Repository vs QueryBuilder vs raw SQL?
Try code in the playgroundUnderstand the concept
A repository exposes common operations around an entity, QueryBuilder assembles more flexible database queries, and raw SQL gives direct control over SQL text. These are levels of access rather than a ranking from bad to good. Use the simplest approach that expresses the required query clearly and efficiently.
Important ideas
-
Repository: common entity reads and writes.
-
QueryBuilder: more control over joins, filtering and selected fields.
-
Raw SQL: useful for database-specific queries; still use bound parameters.
-
Bind values in QueryBuilder and raw SQL; dynamic table or column identifiers need separate allowlists.
Can you explain it without the notes?
Repository vs QueryBuilder vs raw SQL?
Use your own words. Explain an example and one limitation before checking the explanation.
Check your explanation
A repository exposes common operations around an entity, QueryBuilder assembles more flexible database queries, and raw SQL gives direct control over SQL text. These are levels of access rather than a ranking from bad to good. Use the simplest approach that expresses the required query clearly and efficiently.
Turn this concept into a clear answer.
- Show how you would inspect the resulting SQL before blaming the ORM for a slow endpoint.
Adapted from 03_NESTJS.md · “Repository vs QueryBuilder vs raw SQL?”. Examples are study fragments; framework behavior can depend on your version.