Documentation
¶
Overview ¶
Package seed provides Lucid-style factories and seeders.
Index ¶
- Constants
- func Call(ctx context.Context, db *migro.DB, classes ...Class) error
- func Register(c Class)
- func ResetRegistry()
- type Class
- type Factory
- func (f *Factory[T]) After(fn func(ctx context.Context, db *migro.DB, m *T) error) *Factory[T]
- func (f *Factory[T]) AfterMaking(fn func(m *T)) *Factory[T]
- func (f *Factory[T]) Apply(names ...string) *Factory[T]
- func (f *Factory[T]) Create(ctx context.Context, db *migro.DB) (*T, error)
- func (f *Factory[T]) CreateMany(ctx context.Context, db *migro.DB, n int) ([]T, error)
- func (f *Factory[T]) For(name string) *Factory[T]
- func (f *Factory[T]) Make() (T, error)
- func (f *Factory[T]) MakeMany(n int) ([]T, error)
- func (f *Factory[T]) Merge(fn func(*T)) *Factory[T]
- func (f *Factory[T]) Parent(name string, fn ParentFn[T]) *Factory[T]
- func (f *Factory[T]) Relation(name string, fn RelationFn[T]) *Factory[T]
- func (f *Factory[T]) Seed(seed int64) *Factory[T]
- func (f *Factory[T]) Sequence(fns ...func(i int, m *T)) *Factory[T]
- func (f *Factory[T]) State(name string, fn func(*T)) *Factory[T]
- func (f *Factory[T]) With(relation string, n int) *Factory[T]
- type Faker
- func (f *Faker) Bool() bool
- func (f *Faker) Date(from, to time.Time) time.Time
- func (f *Faker) Email() string
- func (f *Faker) FirstName() string
- func (f *Faker) Float(min, max float64) float64
- func (f *Faker) Int(min, max int) int
- func (f *Faker) LastName() string
- func (f *Faker) Name() string
- func (f *Faker) Paragraph() string
- func (f *Faker) Pick(options ...string) string
- func (f *Faker) Sentence() string
- func (f *Faker) UUID() string
- func (f *Faker) Username() string
- func (f *Faker) Word() string
- func (f *Faker) Words(n int) string
- type Option
- type ParentFn
- type RelationFn
- type Result
- type Runner
- type Seeder
Constants ¶
const RootSeeder = "DatabaseSeeder"
RootSeeder is the name of the seeder `db:seed` starts from when one is registered.
Variables ¶
This section is empty.
Functions ¶
func Call ¶
Call runs seeders in the order given, stopping at the first error (Laravel's $this->call([...])). Use it from a DatabaseSeeder so dependent tables are filled in foreign-key order; the calls share the caller's transaction and context, so a failure rolls the whole DatabaseSeeder back.
Types ¶
type Class ¶
Class is a Lucid-style seeder: a type with a Run method, registered from init():
func init() { seed.Register(&UserSeeder{}) }
Add `func (UserSeeder) Environments() []string` to limit where it runs.
type Factory ¶
type Factory[T any] struct { // contains filtered or unexported fields }
Factory builds and persists models. Configure it once with Define/State/Relation/After, then derive variants with Merge/Apply/With/Seed — each returns a copy, so a shared factory is never mutated.
func (*Factory[T]) After ¶
After registers a hook run after each Create (Lucid's .after('create')). Call during setup.
func (*Factory[T]) AfterMaking ¶
AfterMaking registers a hook run after each model is built, by Make and Create alike (Laravel's afterMaking).
func (*Factory[T]) Create ¶
Create builds and inserts one model, then runs With relations and After hooks.
func (*Factory[T]) CreateMany ¶
CreateMany inserts n models in a single transaction: either all of them exist afterwards or none.
func (*Factory[T]) For ¶
For creates the named parent before each insert and links the child to it (Laravel's for()). It has no effect on Make, which never touches the database.
func (*Factory[T]) Parent ¶
Parent registers a named belongs-to builder. Call during setup; activate it with For.
func (*Factory[T]) Relation ¶
func (f *Factory[T]) Relation(name string, fn RelationFn[T]) *Factory[T]
Relation registers a named relation builder. Call during setup.
func (*Factory[T]) Sequence ¶
Sequence alternates attribute changes across the generated models: model i gets fns[i % len(fns)] (Laravel's sequence()). The index counts from 0 within one Make/Create call.
type Faker ¶
type Faker struct {
// contains filtered or unexported fields
}
Faker generates plausible fake values. It is deterministic for a given seed.
func (*Faker) Email ¶
Email uses reserved example domains so generated data never reaches real mailboxes.
type Option ¶
type Option func(*Runner)
func Once ¶
func Once() Option
Once records executed seeders in a table so re-running skips them. Without it seeders run every time, like Lucid — write them idempotently (e.g. with FirstOrCreate).
func WithEnvironment ¶
WithEnvironment sets the environment (default: MIGRO_ENV, then APP_ENV, then "development").
func WithoutEvents ¶
func WithoutEvents() Option
WithoutEvents runs seeders with model events muted (Laravel's WithoutModelEvents trait), so observers and event handlers do not fire for seeded rows.
type ParentFn ¶
ParentFn creates a parent row for a child about to be inserted and points the child at it (Laravel's ->for(User::factory())).
type RelationFn ¶
RelationFn creates n related rows for a freshly created parent (Lucid's factory.with()).
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
Runner executes seeders in name order, each in its own transaction.
func (*Runner) AddRegistered ¶
AddRegistered adds every seeder registered through Register.
type Seeder ¶
type Seeder struct {
Name string
Environments []string
Run func(ctx context.Context, db *migro.DB) error
}
Seeder is one unit of seed data. Like Lucid's, it may be limited to environments: with no Environments it runs everywhere except "production"; to seed production, list "production" explicitly.
func FromFS ¶
FromFS loads every *.sql file in dir as a seeder named after the file. Statements are split like migration files.
func Registered ¶
func Registered() []Seeder
Registered returns the registered seeders sorted by name.