Defining blueprint can be done either via callback to EntityFactory.register() or as a separate Blueprint class.
classUserBlueprintextendsObjectBlueprint{constructor(){super(); // set type used to identify the blueprintthis.type('user'); // set the options for the blueprintthis.options({ generateId:true,}); // define the default factory method for this blueprintthis.define(async({faker,factory})=>{return{/* user properties */};}); // create a state transform for this blueprint to override or supplement // properties created in define()this.state('active',async({faker,factory})=>{return{/* user properties */};}); // Called after making an entitythis.afterMakinge(async(user,{factory,adapter,faker})=>{/* manipulate user */}); // called after making an entity with a given statethis.afterMakingState('active',async(user,{factory,adapter,faker})=>{/* manipulate user */}, ); // called after creating and entitythis.afterCreating(async(user,{factory,adapter,faker})=>{/* manipulate user */}); // called after creating an entity with a given statethis.afterCreatingState('active',async(user,{factory,adapter,faker})=>{/* manipulte user */}, );}}
Class based definition
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.
Blueprint Methods
type(string | class) *Required
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
options(opts)
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:
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
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
// class based Blueprint definition
export class UserBlueprint extends ObjectBlueprint {
constructor() {
super();
this.type('user');
this.define(async ({ faker, factory }) => {
return {
// user attributes
};
});
}
}
// class based blueprints are passed to entity factory via the constructor
const factory = new EntityFactory({
blueprints: [UserBlueprint],
});