Help us shape the future of Go! Take the 2026 Go Developer Survey

factory

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package factory builds fake records (like Laravel model factories).

var UserFactory = factory.New("users", func(f *gofakeit.Faker, i int) User {
	return User{Name: f.Name(), Email: f.Email()}
})

UserFactory.Count(50).Create(ctx, c)

Index

Constants

View Source
const DefaultChunkSize = 500

DefaultChunkSize is how many rows go into one INSERT statement.

Variables

This section is empty.

Functions

This section is empty.

Types

type Env

type Env interface {
	Insert(table interface{}) *goqu.InsertDataset
	Faker() *gofakeit.Faker
}

Env is what Create needs: an INSERT builder and a faker. *seeder.Context implements it.

type Factory

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

Factory produces values of type T. T is usually a struct with `db:"..."` tags; tag auto-increment columns with `goqu:"skipinsert"`.

A Factory is immutable: Count, State and With return a copy, so a shared base factory can be specialised safely.

func New

func New[T any](table string, def func(f *gofakeit.Faker, i int) T) *Factory[T]

New creates a factory. def receives a faker and a sequence number that grows with every record this factory (and its copies) builds. Use the sequence for values that must be unique:

Email: fmt.Sprintf("user%d@example.com", i)

func (*Factory[T]) ChunkSize

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

ChunkSize sets rows per INSERT statement.

func (*Factory[T]) Count

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

Count sets how many records to build (default 1).

func (*Factory[T]) Create

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

Create builds the records and inserts them (in chunks). It returns the records as built; database-generated columns such as IDs are not filled in, query them afterwards if you need them.

func (*Factory[T]) CreateOne

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

CreateOne builds and inserts a single record.

func (*Factory[T]) Make

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

Make builds records in memory without touching the database.

func (*Factory[T]) MakeOne

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

MakeOne builds a single record in memory.

func (*Factory[T]) State

func (f *Factory[T]) State(fns ...func(*T)) *Factory[T]

State adds modifiers that run after the definition, in order:

UserFactory.State(func(u *User) { u.Role = "admin" })

func (*Factory[T]) Table

func (f *Factory[T]) Table() string

Table returns the target table.

func (*Factory[T]) With

func (f *Factory[T]) With(fk *gofakeit.Faker) *Factory[T]

With uses a specific faker (for example a seeded one) in Make.

Jump to

Keyboard shortcuts

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