Factory
The Factory
helps you generate fake data and seed your database for testing or development purposes. It uses @faker-js/faker
and provides a fluent API for creating real or stubbed model data.
Generating a Factory
Section titled “Generating a Factory”You can quickly generate a factory for any model using the generator:
bun g
Then select the Factory
option and provide the model name when prompted:
? What do you want to forge? (Use arrow keys) Module Controller Test❯ Factory Seed Task Validation Observer
✔ What do you want to forge? Factory✔ Factory name (Use a model name): user
This will create a pre-configured seed file at app/database/factories/user.factory.ts
.
Defining a Factory
Section titled “Defining a Factory”You define a factory by passing a model and a function that returns fake attributes.
import { Factory } from "app/helpers/Factory";import { User } from "..";
const UserFactory = new Factory().define(User, (faker) => { return { name: faker.person.fullName(), email: faker.internet.email(), role: "USER", }});
export { UserFactory };
Using a Factory
Section titled “Using a Factory”You can merge static or dynamic attributes, create single or multiple records, or return stubbed data without saving to the database.
import { UserFactory } from "app/database/factories/user.factory";
const user = await UserFactory .merge({ role: "ADMIN" }) .create();
Creating Multiple Records
Section titled “Creating Multiple Records”await UserFactory.createMany(10);
Returning Stubbed Data (no DB interaction)
Section titled “Returning Stubbed Data (no DB interaction)”const fakeUser = await UserFactory.makeStubbed();const fakeUsers = await UserFactory.makeStubbedMany(5);
Example with Relationships
Section titled “Example with Relationships”import { PostFactory } from "app/database/factories/post.factory";import { UserFactory } from "app/database/factories/user;factory";
export async function run() { const author = await UserFactory.merge({ role: "AUTHOR" }).create();
await PostFactory.merge({ authorId: author.id }).createMany(3);}
Attribute Selection (pick
and get
)
Section titled “Attribute Selection (pick and get)”The Factory provides pick
and get
methods to select specific attributes from objects or arrays, either from created records or stubbed data.
// Select single or multiple attributes from a created recordconst userName = await user.pick("name");const userInfo = await user.pick(["name", "email"]);
// Get attribute values directlyconst emails = await user.get("email");const values = await user.get(["name", "role"]);
// Works on multiple recordsconst users = await UserFactory.createMany(5);const names = await users.pick("name"); // Array of objects with only the nameconst emailValues = await users.get("email"); // Array of emails
pick(keys)
returns an object or array of objects containing only the selected keys.get(keys)
returns the values of the selected keys directly.
Method | Description |
---|---|
.define(model, fakerFn) | Define a factory for a model. |
.merge(attributes) | Merge static values into the generated data. |
.create() | Create and return one record. |
.createMany(count) | Create and return multiple records. |
.makeStubbed() | Return one fake object (not persisted). |
.makeStubbedMany(count) | Return multiple fake objects. |
.pick(keys) | Select specific attributes from a record or array of records. |
.get(keys) | Get the values of specific attributes from a record or array of records. |
Each call to
create()
ormakeStubbed()
resets the merge state, ensuring isolation between uses.