View Source Dx (dx v0.3.4)

This is the main entry for using the Dx API.

  • get/3 evaluates the given predicate(s) using only the (pre)loaded data available, and returns the result(s)
  • load/3 is like get, but loads any additional data as needed
  • put/3 is like load, but puts the results into the :inferred field (or virtual schema field) of the subject(s) as a map, and returns the subject(s)

These functions return a tuple, either {:ok, result}, {:error, error}, or {:not_loaded, data_reqs} (only get).

The corresponding get!/3, load!/3 and put!/3 functions return result directly, or otherwise raise an exception.

Arguments:

  • subjects can either be an individual subject (with the given predicates defined on it), or a list of subjects. Passing an individual subject will return the predicates for the subject, passing a list will return a list of them.
  • predicates can either be a single predicate, or a list of predicates. Passing a single predicate will return the resulting value, passing a list will return a Map of the predicates and their resulting values.
  • options (optional) See below.

Options:

  • args (list or map) can be used to pass in data from the caller's context that can be used in rules (see Arguments below). A classic example is the current_user, e.g.
    put!(project, :can_edit?, args: [user: current_user])
  • extra_rules (module or list of modules) can be used to add context-specific rules that are not defined directly on the subject. This can be used to structure rules into their own modules and use them only where needed.
  • debug (boolean) makes Dx print additional information to the console as rules are evaluated. Should only be used while debugging.
  • return_cache (boolean) makes non-bang functions return {:ok, result, cache} instead of {:ok, result} on success. This cache can be passed to other Dx functions (see cache option)
  • cache (Dataloader struct) can be used to pass in an existing cache, so data already loaded doesn't need to be loaded again. Can be initialized using Dx.Loaders.Dataloader.init/1.
  • loader allows choosing a loader module. Defaults to Dx.Loaders.Dataloader.
  • loader_options are passed to loader.init/1 function. See Dx.Loaders.Dataloader for options supported by the default loader.

Summary

Functions

Removes all elements not matching the given condition from the given list.

Evaluates one or multiple predicates for one or multiple records and returns the results.

Like get/3 but returns the result value, or raises an error.

Like get/3, but loads additional data if needed.

Like get!/3, but loads additional data if needed.

Loads the given predicate(s) for the given record(s) and merges the results into the inferred map field of the record(s), returning them.

Returns all records matching the given condition.

Returns the first record matching the given condition.

Removes all elements matching the given condition from the given list.

Functions

Link to this function

filter(records, condition, opts \\ [])

View Source

Removes all elements not matching the given condition from the given list.

Link to this function

get(records, predicates, opts \\ [])

View Source

Evaluates one or multiple predicates for one or multiple records and returns the results.

Does not load any additional data.

Link to this function

get!(records, predicates, opts \\ [])

View Source

Like get/3 but returns the result value, or raises an error.

Link to this function

load(records, predicates, opts \\ [])

View Source

Like get/3, but loads additional data if needed.

Link to this function

load!(records, predicates, opts \\ [])

View Source

Like get!/3, but loads additional data if needed.

Link to this function

put(records, predicates, opts \\ [])

View Source

Loads the given predicate(s) for the given record(s) and merges the results into the inferred map field of the record(s), returning them.

Options

Same as for get/3.

Link to this function

put!(records, predicates, opts \\ [])

View Source
Link to this function

query_all(queryable, condition, opts \\ [])

View Source

Returns all records matching the given condition.

Caveat

In general, as much work as possible is done in the database. If possible, the condition is completely translated to an Ecto.Query so the database only returns matching records. All condition parts that can not be translated to an Ecto.Query, will be evaluated by loading all remaining records, and associations as needed, and evaluating the rules on them.

Link to this function

query_one(queryable, condition, opts \\ [])

View Source

Returns the first record matching the given condition.

Options

Same as for query_all/3.

Link to this function

reject(records, condition, opts \\ [])

View Source

Removes all elements matching the given condition from the given list.