LoadResource v0.4.0 LoadResource.QueryBuilder View Source

Each time a connection comes in, we build up a query using a specified set of LoadResource.Scopes.

Each scope representing a condition that needs to be fulfilled (looking up by ID, verifying a column, checking another parameter, etc.); the QueryBuilder joins them all together into a single composite database query to be executed in the plug.

Link to this section Summary

Functions

Build an Ecto query for a model record by evaluating a set of scopes for a given connection

Link to this section Functions

Link to this function build(model, conn, scopes) View Source

Build an Ecto query for a model record by evaluating a set of scopes for a given connection.

See LoadResource.Scope for more information on how scopes are constructed.

Example:

scope = %Scope{column: :source_book_id, value: &identify_source_book_id/1}
QueryBuilder.build(Quote, conn, [scope])
# => select * from quotes where source_book_id = ${identify_source_book_id(conn)}

QueryBuilder.build(Quote, conn, [:book])
# scopes can be built from atoms -- see the Scope documentation
# => select * from quotes where book_id = ${conn.assigns[:book].id}

Multiple scopes can be provided (and they don’t have to all be columns, either):

scope = %Scope{column: :book_type, value: fn(conn) -> conn.params[:book_type] end}
QueryBuilder.build(Quote, conn, [:book, scope])
# => select * from quotes where book_id = ${conn.assigns[:book].id} and book_type = ${conn.params[:book_type]}