API
Factory Definition
->factory
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:
primary-key
Replace
A keyword that specifies a key in the template to identify the entity when referenced
template
Merge
persistable
Replace
A boolean that controls whether the entity should be persisted in calls to create
and create-list
. Defaults to true
traits
Merge
transients
Replace
before-build
Replace
after-build
Replace
inherit
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.
Building and Creation
build
build-list
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:
create
create-list
Directives
constant
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)
sequence
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.
one
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.
many
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.
derive
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.
associate-as
Persistence
persist!
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-default-persistence!
Set the default persistence method. Defaults to built-in persistence.
store
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.
reset-store!
Empties the built in store.
Last updated