Raw SQL vs Query Builder
On this page
Why a Query Layer
Embedding SQL inside route handlers couples business logic to persistence details. A query layer centralizes data access and makes refactoring safer.
Pattern
export async function findUserById(id: string) {
return pool.query('SELECT id, email FROM users WHERE id = $1', [id]);
}
Benefits
- Reusability
- Testability
- Separation of concerns
Well-defined query modules reduce duplication and improve consistency.