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

seeder

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: 14 Imported by: 0

Documentation

Overview

Package seeder runs seeders (like Laravel's DatabaseSeeder).

Index

Constants

View Source
const RootName = "DatabaseSeeder"

RootName is the seeder `db:seed` runs when no --class is given.

Variables

View Source
var Default = NewRegistry()

Default is used by Register and by the CLI.

Functions

func Register

func Register(s Seeder)

Register adds a seeder to Default under its type name (UserSeeder).

Types

type Context

type Context struct {
	*goqu.TxDatabase
	// contains filtered or unexported fields
}

Context is passed to every seeder. It embeds the goqu transaction, so c.Insert, c.From, c.Update and c.Delete are available directly, and it implements factory.Env.

func (*Context) Call

func (c *Context) Call(ctx context.Context, seeders ...Seeder) error

Call runs other seeders, in order, inside the same transaction.

func (*Context) Faker

func (c *Context) Faker() *gofakeit.Faker

Faker returns the faker shared by the whole run (seeded by --seed).

type Registry

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

Registry maps seeder names to implementations.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty registry.

func (*Registry) Add

func (r *Registry) Add(s Seeder)

Add registers a seeder under its type name.

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 lists registered seeder names, sorted.

type Runner

type Runner struct {
	DB       *sql.DB
	Driver   string
	Registry *Registry
	// Seed makes fake data reproducible. 0 = random each run.
	Seed uint64
	// Out receives progress lines (nil = silent).
	Out io.Writer
}

Runner executes a seeder inside one transaction.

func (*Runner) Run

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

Run runs the named seeder (RootName when empty). Everything happens in one transaction: if any seeder fails, nothing is written.

type Seeder

type Seeder interface {
	Run(ctx context.Context, c *Context) error
}

Seeder fills the database with data.

Jump to

Keyboard shortcuts

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