factory

package
v0.10.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 22, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package factory provides a generic, type-safe model factory inspired by Laravel's factories. Factories generate fake model instances and can persist them via orm.Save. States layer transformations on top of the default definition.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DefinitionFn

type DefinitionFn[T any] func(f *Faker) T

DefinitionFn returns a freshly-built model instance.

type Factory

type Factory[T any] struct {
	// contains filtered or unexported fields
}

Factory builds and persists T instances.

func New

func New[T any](conn *database.Connection, def DefinitionFn[T]) *Factory[T]

New constructs a Factory using a definition function.

func (*Factory[T]) AfterCreate

func (f *Factory[T]) AfterCreate(fn func(context.Context, *T) error) *Factory[T]

AfterCreate runs after persistence.

func (*Factory[T]) AfterMake

func (f *Factory[T]) AfterMake(fn func(*T)) *Factory[T]

AfterMake runs after construction but before persistence.

func (*Factory[T]) Count

func (f *Factory[T]) Count(n int) *Factory[T]

Count sets how many instances to build.

func (*Factory[T]) Create

func (f *Factory[T]) Create(ctx context.Context) ([]T, error)

Create persists instances and returns them.

func (*Factory[T]) CreateOne

func (f *Factory[T]) CreateOne(ctx context.Context) (T, error)

CreateOne creates and returns one instance.

func (*Factory[T]) Make

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

Make builds instances without persisting.

func (*Factory[T]) MakeOne

func (f *Factory[T]) MakeOne() T

MakeOne builds a single instance.

func (*Factory[T]) Set

func (f *Factory[T]) Set(fn func(*T)) *Factory[T]

Set is a convenience: overrides a specific field via callback. Useful for pinning a relation: factory.User().Set(func(u *User) { u.TenantID = 1 }).

func (*Factory[T]) State

func (f *Factory[T]) State(fn StateFn[T]) *Factory[T]

State applies an inline state transformer to every produced instance.

type Faker

type Faker struct {
	// contains filtered or unexported fields
}

Faker wraps gofakeit with stable, allocation-conscious helpers. Each Factory owns one Faker; in tests, callers may seed the underlying Faker for reproducible builds.

func NewFaker

func NewFaker() *Faker

NewFaker constructs an un-seeded Faker.

func NewSeeded

func NewSeeded(seed uint64) *Faker

NewSeeded constructs a deterministic Faker.

func (*Faker) Address

func (f *Faker) Address() string

func (*Faker) Bool

func (f *Faker) Bool() bool

func (*Faker) City

func (f *Faker) City() string

func (*Faker) Country

func (f *Faker) Country() string

func (*Faker) Email

func (f *Faker) Email() string

func (*Faker) FirstName

func (f *Faker) FirstName() string

func (*Faker) Float64Range

func (f *Faker) Float64Range(min, max float64) float64

func (*Faker) IntRange

func (f *Faker) IntRange(min, max int) int

func (*Faker) LastName

func (f *Faker) LastName() string

func (*Faker) Name

func (f *Faker) Name() string

func (*Faker) Paragraph

func (f *Faker) Paragraph(paragraphs, sentences, words int, separator string) string

func (*Faker) Password

func (f *Faker) Password() string

func (*Faker) Phone

func (f *Faker) Phone() string

func (*Faker) PickString

func (f *Faker) PickString(values []string) string

func (*Faker) Raw

func (f *Faker) Raw() *gofakeit.Faker

Raw exposes the underlying gofakeit.Faker for advanced usage.

func (*Faker) Sentence

func (f *Faker) Sentence(words int) string

func (*Faker) URL

func (f *Faker) URL() string

func (*Faker) UUID

func (f *Faker) UUID() string

func (*Faker) UserName

func (f *Faker) UserName() string

func (*Faker) Word

func (f *Faker) Word() string

type StateFn

type StateFn[T any] func(f *Faker, m *T)

StateFn mutates a model in place.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL