go-migrationgo-migration
Package Reference
Documentation

Factory

Complete package reference for the go-migration Factory[T] generic type and Faker methods — all public methods with signatures, parameters, return types, and usage examples.

Factory API

The factory system generates realistic test data using a generic Factory[T] type and a built-in Faker for random value generation.

Import path: github.com/gopackx/go-migration/seeder/factory

For conceptual documentation, see Factories.


factory.NewFactory()

Creates a new generic factory with a builder function.

go
func NewFactory[T any](definition func(faker Faker) T) *Factory[T]
ParameterTypeDescription
definitionfunc(faker Faker) TFunction that receives a Faker and returns an instance of T

Returns: *Factory[T]

The builder function is called each time you generate an instance, producing unique random values on every call.

go
import "github.com/gopackx/go-migration/seeder/factory"

type User struct {
    ID    int
    Name  string
    Email string
}

var UserFactory = factory.NewFactory(func(f factory.Faker) User {
    return User{
        ID:    f.IntBetween(1, 10000),
        Name:  f.Name(),
        Email: f.Email(),
    }
})

See Creating Factories.


f.Make()

Generates a single instance of type T by calling the builder function once.

go
func (f *Factory[T]) Make() T

Returns: T — a single struct instance with random values.

go
user := UserFactory.Make()
fmt.Println(user.Name)  // e.g., "Alice Johnson"
fmt.Println(user.Email) // e.g., "alice@example.com"

Each call produces a new instance with different random values.

See Make and MakeMany.


f.MakeMany()

Generates a slice of n instances by calling the builder function n times.

go
func (f *Factory[T]) MakeMany(n int) []T
ParameterTypeDescription
nintNumber of instances to generate

Returns: []T — a slice of struct instances with independently generated random values.

go
users := UserFactory.MakeMany(50)
for _, u := range users {
    fmt.Printf("%s <%s>\n", u.Name, u.Email)
}

MakeMany generates all instances in memory before returning. For very large batches, consider generating and inserting in smaller chunks.

See Make and MakeMany.


f.State()

Registers a named state variant on the factory.

go
func (f *Factory[T]) State(name string, fn func(faker Faker, base T) T) *Factory[T]
ParameterTypeDescription
namestringUnique name for this state
fnfunc(faker Faker, base T) TModifier function that receives the base instance and returns a modified one
go
UserFactory.State("admin", func(f factory.Faker, base User) User {
    base.Role = "admin"
    return base
})

See Named States.


f.WithState()

Returns a new factory instance that uses the named state's builder function.

go
func (f *Factory[T]) WithState(name string) *Factory[T]
ParameterTypeDescription
namestringName of a previously registered state

Returns: *Factory[T] — a new factory instance (the original is unchanged).

go
// Single admin
admin := UserFactory.WithState("admin").Make()

// Batch of admins
admins := UserFactory.WithState("admin").MakeMany(10)

See Named States.


Faker Methods

The factory.Faker instance is passed to factory builder functions. Each method generates a new random value on every call.

String Methods

MethodSignatureReturn TypeDescription
Namef.Name()stringFull person name (e.g., "Alice Johnson")
FirstNamef.FirstName()stringFirst name (e.g., "Alice")
LastNamef.LastName()stringLast name (e.g., "Johnson")
Emailf.Email()stringEmail address (e.g., "alice@example.com")
Phonef.Phone()stringPhone number
Addressf.Address()stringFull street address
Cityf.City()stringRandom city name
Countryf.Country()stringRandom country name
UUIDf.UUID()stringRandom UUID v4 string
Sentencef.Sentence()stringRandom sentence of text
Paragraphf.Paragraph()stringRandom paragraph of text
Wordf.Word()stringRandom single word

Numeric Methods

MethodSignatureReturn TypeDescription
IntBetweenf.IntBetween(min, max int)intRandom integer in range [min, max]
Float64Betweenf.Float64Between(min, max float64)float64Random float in range [min, max)

Other Methods

MethodSignatureReturn TypeDescription
Boolf.Bool()boolRandom boolean value
Datef.Date()time.TimeRandom date
DateBetweenf.DateBetween(start, end time.Time)time.TimeRandom date between start and end
Pickf.Pick(items []string)stringRandom element from a string slice

Usage Example

go
var OrderFactory = factory.NewFactory(func(f factory.Faker) Order {
    return Order{
        ID:        f.UUID(),
        Customer:  f.Name(),
        Email:     f.Email(),
        Total:     f.Float64Between(10.00, 500.00),
        Items:     f.IntBetween(1, 20),
        Shipped:   f.Bool(),
        CreatedAt: f.Date(),
    }
})

See Faker Methods for the full reference.


Quick Reference

ComponentMethodSignatureDescription
FactoryNewFactoryNewFactory[T](builder) *Factory[T]Create a factory
FactoryMakeMake() TGenerate one instance
FactoryMakeManyMakeMany(n) []TGenerate n instances
FactoryStateState(name, builder)Register a named state
FactoryWithStateWithState(name) *Factory[T]Use a named state
FakerNameName() stringRandom full name
FakerFirstNameFirstName() stringRandom first name
FakerLastNameLastName() stringRandom last name
FakerEmailEmail() stringRandom email
FakerPhonePhone() stringRandom phone
FakerAddressAddress() stringRandom address
FakerCityCity() stringRandom city
FakerCountryCountry() stringRandom country
FakerUUIDUUID() stringRandom UUID
FakerSentenceSentence() stringRandom sentence
FakerParagraphParagraph() stringRandom paragraph
FakerWordWord() stringRandom word
FakerIntBetweenIntBetween(min, max) intRandom int in range
FakerFloat64BetweenFloat64Between(min, max) float64Random float in range
FakerBoolBool() boolRandom boolean
FakerDateDate() time.TimeRandom date
FakerDateBetweenDateBetween(start, end) time.TimeRandom date in range
FakerPickPick(items) stringRandom element from slice