Use afterBuild and afterCreate hooks
mainFishery provides hooks to execute code during the object lifecycle:
afterBuild: Executed after an object is built. Useful for setting up relationships or modifying the object.afterCreate: Executed after theonCreatelogic is finished. MultipleafterCreatehooks can be defined.onCreate: Defines or replaces the behavior of.create(). Only one can be defined.
// afterBuild example
const userFactory = Factory.define<User>(({ sequence, afterBuild }) => {
afterBuild(user => {
// perform side effects or setup relationships
});
return { id: sequence, name: 'Bob' };
});
// afterCreate example
const userFactory = Factory.define<User, {}, SavedUser>(({ onCreate, afterCreate }) => {
onCreate(user => apiService.create(user));
afterCreate(savedUser => doMoreStuff(savedUser));
return { id: 1, name: 'Bob' };
});