API
Last updated
Last updated
Accepts a keyword and a factory definition and returns a factory record. The keyword will be used as an id to register the factory in a global directory internal to fabrikk. If a factory with the key already exists in the directory, it will be overwritten. Namespaced keywords are recommended.
Most of the API accepts either a factory record or the id, with the exception of the persist!
multimethod which requires the id.
Factories are records that can be called as functions:
A factory definition has the following structure:
Property | Required? | Inherit Behaviour | Description |
---|---|---|---|
Accepts an existing factory (or id), a keyword and a factory description, and returns a new new factory that inherits from the existing factory. All keys listed in ->factory may be specified, see the inherit behaviour column in the table for more.
Accepts a factory and a quantity and builds that many entities. Build option can either be a single option map that is used to build each entity, or a list of option maps, each of which will be used to build one entity. If this list contains fewer than quantity
options, the last one is repeated. For example:
Returns a directive that evaluates to x
during build. Useful if you don't want Fabrikk to try and interpret the value (e.g. if you want to specify a function as a value)
Returns a directive that evaluates to an increasing series of integers, producing a new value each time the enclosing factory is invoked. Accepts an optional transform-fn
that is called with the generated integer.
By default each call to sequence
creates an 'series' of integers isolated to this specific key on this specific factory. If the optional identifier
keyword is passed, then the sequence will be shared with each other sequence
directive with that identifier.
Sequences persist for the lifetime of the program.
Returns a directive that will invoke the given factory at build time and add the resulting entity into the current build graph.
The entity is referenced via the key associated with this directive in the template.
Returns a directive that will invoke the given factory at build time, build quantity
copies of that entity, and add the resulting list of entities into the current build graph.
The list of entities are referenced via the key associated with this directive in the template. Entities can be referenced within this list via their index.
Returns a directive that derives a value from the build graph at build time. If a keyword is passed, then the value will be derived from the current entity. If a path - i.e. a list of keywords - is passed, then the value will be derived from the entity at that path, relative to the current entity.
Accepts an optional transform-fn
that accepts the derived value and returns a transformed value.
When deriving from an entity in the build graph using a path, if no transform-fn
is specified the derived value will be the primary-key
if the entities factory specifies one (see ->factory), or the entity itself otherwise.
A multimethod that facilitates custom persistence methods. Takes a factory id, and an entity, and expects a persisted version of that entity to be returned. Changes made to the entity during persistence will be propagated through the build graph as necessary, depending on what other entities have references to the persisted one.
Set the default persistence method. Defaults to built-in persistence.
An atom that stores entities created using the built-in persistence.
The built-in persistence method collects built entities in lists keyed by their factory id. It does not modify them.
Empties the built in store.
A template must be specified in the inherited factory. This template is combined with the existing template following the behaviour described in
Accepts a factory and some optional build and output options, and outputs an entity. See and for more
See and for more.
As for build
but also persists each entity in the graph in build order. See and for more.
As for build-list
but also persists each entity in the graph in build order. See and for more.
Either the factory instance itself or its id can be used. Accepts the same build options as build
etc. See for more.
Either the factory instance itself or its id can be used. Accepts the same build options as build
etc. As is the case for build-list
, either a single build option can be passed, or a list. See for more.
Takes an entity
and a transform-fn
and overrides the associate-as value on that entity. This is sort of on an honorary directive, along with simply passing the entity in a :with
template. See for more
primary-key
Replace
A keyword that specifies a key in the template to identify the entity when referenced
template
Merge
A template specifying the default representation of the entity, see Concepts
persistable
Replace
A boolean that controls whether the entity should be persisted in calls to create
and create-list
. Defaults to true
traits
Merge
A map of keywords to templates, traits are identified by these keywords, see Concepts for more
transients
Replace
A template whose keys will be removed from the entity after it is built, see Concepts for more
before-build
Replace
A function that takes the compiled template (constructed from template, transients and any build options) and returns an updated template, see Build Hooks for more
after-build
Replace
A function that takes the current build context, and the value of the built entity (including transients), and returns an updated entity, see Build Hooks for more