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 likeget
, but loads any additional data as neededput/3
is likeload
, 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 aMap
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 thecurrent_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. Thiscache
can be passed to other Dx functions (seecache
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 usingDx.Loaders.Dataloader.init/1
.loader
allows choosing a loader module. Defaults toDx.Loaders.Dataloader
.loader_options
are passed toloader.init/1
function. SeeDx.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
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.
Does not load any additional data.
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.
Options
Same as for get/3
.
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.
Returns the first record matching the given condition.
Options
Same as for query_all/3
.
Removes all elements matching the given condition from the given list.