Defining a Blueprint
Last updated
Was this helpful?
Last updated
Was this helpful?
Defining blueprint can be done either via callback to or as a separate Blueprint class.
Class based blueprint must extend a blueprint class. Each adapter has it's own blueprint class which provides different available options specific to the adapter. By default the Object adapter can be used with Entity Factory. For more information on the options available please refer to the docs for each adapter blueprint.
Registering a blueprint via a callback is an alternative to using a blueprint class. It does however have it's drawbacks. Using this method will always return a basic Blueprint class that is not specific to the adapter being used. As a result any additional functionality provided by the adapters blueprint will not be available with this method.
Type is used as an identifier within entity factory and can be either a string or a class reference that is passed. An important thing to note is that the value passed to type()
will effect the value returned. Types passed as string will be created as plain javascript objects. If however a class reference is provided then Entity factory will attempt to return an instantiated instance of that class.
This method must be called
Each adapter blueprint can specify different available options. The options are passed to the adapter at runtime. For adapter blueprint specific options please refer to their documentation:
Used to create the primary factory method for an entity. The callback method receives a single argument which is a faker instance.
Property values can also an async callback to the factory which will allow data to be resolved from related factories.
This method must be called callback
context:
faker: faker instance
factory: factory instance
Used to override portions of the primary factory method and/or to add additional properties.
Property values can also an async callback to the factory which will allow data to be resolved from related factories.
callback
context:
faker: faker instance
factory: factory instance
afterMaking(callback(currentEntity, context))
afterMakingState(string, callback(currentEntity, context))
Fired after a call to make()
on the builder instance. The entity received will have been resolved to it's base type but not persisted. The callback will receive the current entity as well as a context object.
callback
currentEntity: resolved entity, plain object or class
context:
faker: faker instance
factory: factory instance
adapter: adapter instance (default ObjectAdapter)
afterCreating(callback(currentEntity, context))
afterCreatingState(string, callback(currentEntity, context))
Fired after a call to create()
on the builder instance. The entity received will have been resolved and persisted. The callback will receive the current entity as well as a context object.
callback
currentEntity: resolved entity, plain object or class
context:
faker: faker instance
factory: factory instance
adapter: adapter instance (default ObjectAdapter)