Skip to content

Backend Pattern Query

Ferris Tseng edited this page Jan 2, 2020 · 4 revisions

[Pattern] Query

Description

Query objects store complex SQL queries, data aggregation, and filtering methods. 1

Location

  • app/queries

Best Practices

  • The Query object should take a relation as it's argument, that should enable the caller to control the scope of the query.
  • Query objects should be composable. 2
  • Should return an ActiveRecord::Relation. 2

Note: In practice, it's difficult to achieve some of these desired qualities because Caseflow's data spans multiple databases.

Tradeoffs

Use the Service pattern if you are encapsulating complex logic that requires a read.

Resources

Examples in Caseflow

Additional Reading

Related Patterns

Pattern Description
Service Alternative for database logic that requires writes
Clone this wiki locally