Skip to main content

Factories

Formidable provides a simple way to generate fake data for your application using Faker. You can use this to create fake data for your Repositories or Tables, which is useful for testing and seeding your database.

Creating Factoriesโ€‹

You can create a factory using the make:factory Craftsman command. This will generate a new factory class in the database/factories directory.

node craftsman make:factory TaskFactory

This will create a new factory class named TaskFactory. You can then use this class to define fake data for the corresponding Repository or Table::

import {Factory} from '@formidablejs/framework'

export class TaskFactory extends Factory {
async definition(): Promise<any> {
return {
name: this.faker().lorem.sentence(),
description: this.faker().lorem.paragraph(),
completed: false,
}
}
}

Using Factoriesโ€‹

Factories can be used to create and insert records into your database.

Creating a Single Recordโ€‹

await TaskFactory.factory().create()

Creating Multiple Recordsโ€‹

Use the count method to specify how many records to generate:

await TaskFactory.factory().count(10).create()

Resetting the Tableโ€‹

You can reset the table before seeding data:

await TaskFactory.factory().reset().create()

This will delete all existing records before inserting new ones.

Using Statesโ€‹

States allow you to define different variations of the same factory. This is useful for representing different scenarios or states of a model.

Defining a Stateโ€‹

database/factories/TaskFactory.ts
import {Factory} from '@formidablejs/framework'

export class TaskFactory extends Factory {
async definition(): Promise<any> {
return {
name: this.faker().lorem.sentence(),
description: this.faker().lorem.paragraph(),
completed: false,
}
}

public completed() {
return this.state({
completed: true,
})
}
}

Using a Stateโ€‹

await TaskFactory.factory().completed().create()

This will create a task record with the completed field set to true.

Using Factories with Seedersโ€‹

You can use factories inside seeders to populate your database with test data:

database/seeders/DatabaseSeeder.ts
import {type Database} from '@formidablejs/framework'
import {TaskFactory} from '../factories/TaskFactory'

export const seed = async (DB: Database): Promise<void> => {
await TaskFactory.factory().reset().count(10).create()
}