NODEJS Contents

Raw SQL vs Query Builder

A query layer isolates raw SQL from the rest of your application, improving maintainability and testability.

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.