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

seeder

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package seeder runs database seeders with dependency ordering and transactional execution. A Seeder is any type that implements Seeder; the Runner topologically sorts the registered seeders and runs them in turn.

Index

Constants

This section is empty.

Variables

View Source
var Default = NewRegistry()

Default is the process-wide registry.

Functions

func Call added in v0.12.0

func Call(ctx context.Context, conn *database.Connection, seeders ...Seeder) error

Call runs the given seeders in the order they were passed. Intended for use inside a DatabaseSeeder's Run() to orchestrate child seeders the Laravel way:

func (DatabaseSeeder) Run(ctx context.Context, conn *database.Connection) error {
    return seeder.Call(ctx, conn,
        &CategorySeeder{},
        &UserSeeder{},
    )
}

Errors are wrapped with the failing seeder's Name() for easier triage.

func Register

func Register(s Seeder)

Register is a package-level helper for Default.

Types

type Dependent

type Dependent interface {
	Dependencies() []string
}

Dependent is an optional interface for declaring dependencies. Seeders without Dependencies are ordered by registration order.

type FuncSeeder

type FuncSeeder struct {
	N    string
	Deps []string
	Fn   func(ctx context.Context, conn *database.Connection) error
}

FuncSeeder adapts a function to the Seeder interface.

func (*FuncSeeder) Dependencies

func (f *FuncSeeder) Dependencies() []string

func (*FuncSeeder) Name

func (f *FuncSeeder) Name() string

func (*FuncSeeder) Run

type Options

type Options struct {
	// Transactional wraps each seeder run in a transaction.
	Transactional bool
	// Logger receives status messages.
	Logger *logger.Logger
	// Only restricts the run to the named seeders (and their deps).
	Only []string
}

Options configures Runner behavior.

type Registry

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

Registry stores seeders.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates an empty registry.

func (*Registry) Get

func (r *Registry) Get(name string) (Seeder, bool)

Get returns a seeder by name.

func (*Registry) Names

func (r *Registry) Names() []string

Names returns registration order.

func (*Registry) Register

func (r *Registry) Register(s Seeder)

Register adds a seeder. Panics on duplicate.

func (*Registry) Sorted

func (r *Registry) Sorted() ([]Seeder, error)

Sorted returns seeders in dependency order. Cycles return an error.

type Runner

type Runner struct {
	Conn     *database.Connection
	Registry *Registry
	Opts     Options
}

Runner executes seeders.

func NewRunner

func NewRunner(conn *database.Connection, registry *Registry, opts Options) *Runner

NewRunner builds a Runner. registry may be nil to use Default.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context) error

Run executes all (or selected) seeders.

type Seeder

type Seeder interface {
	Name() string
	Run(ctx context.Context, conn *database.Connection) error
}

Seeder defines the contract for a seeder. Name uniquely identifies the seeder; Dependencies declares names this seeder requires to run first.

func Define

func Define(name string, deps []string, fn func(ctx context.Context, conn *database.Connection) error) Seeder

Define builds an inline FuncSeeder.

Jump to

Keyboard shortcuts

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