Documentation
¶
Overview ¶
Package seeder provides the public Go API for programmatic database seeding.
Users create Go seed files that register seeds via init() functions. Each seed has a direction (Up or Down) and a Run function.
Example ¶
func init() {
seeder.Register(&seeder.Seed{
Version: 1720310400,
Name: "add_admin",
Direction: seeder.Up,
Driver: "postgres",
Run: func(ctx context.Context, db seeder.DB) error {
return db.InsertJSON(ctx, "users", []map[string]interface{}{
{"name": "Admin", "role": "admin"},
})
},
})
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DB ¶
type DB interface {
// ExecSQL executes a raw SQL query statement (PostgreSQL and MySQL only).
ExecSQL(ctx context.Context, query string) error
// InsertJSON inserts a slice of records/documents into the specified table/collection.
InsertJSON(ctx context.Context, table string, records []map[string]interface{}) error
// DeleteJSON removes records/documents matching the filter map from the specified table/collection.
DeleteJSON(ctx context.Context, table string, filter map[string]interface{}) error
// Truncate deletes all records/documents from the specified tables/collections.
Truncate(ctx context.Context, tables ...string) error
}
DB defines the database operations interface available inside a seed function.
type Direction ¶
type Direction string
Direction represents the execution direction of a seed operation (Up or Down).
type Options ¶
type Options struct {
// Driver specifies the database driver. If empty, the driver is auto-detected from DSN.
Driver string
// DSN is the connection URI string.
DSN string
}
Options contains configuration options for initializing a new Seeder.
type Seed ¶
type Seed struct {
// Version is the unique version identifier of the seed (usually a Unix timestamp).
Version int64
// Name is the descriptive name of the seed.
Name string
// Direction specifies if the seed is an Up or Down migration.
Direction Direction
// Driver specifies the target database engine: "postgres", "mysql", or "mongodb".
// If empty, the seed runs on any connected database.
Driver string // postgres, mysql, mongodb
// Run is the function hook executed during the seed operation.
Run SeedFunc
}
Seed represents a single registered seed containing version metadata and execution logic.
func GetRegistered ¶
func GetRegistered() []*Seed
GetRegistered returns all registered seeds sorted by version.
type Seeder ¶
type Seeder struct {
// contains filtered or unexported fields
}
Seeder provides direct programmatic control over database seed versions and execution.
func New ¶
New creates and initializes a Seeder, establishes a database connection, and ensures the version tracking schema is created.
func (*Seeder) DB ¶
DB returns a DB adapter wrapping the active database connection for manual seeding operations.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
seeder
command
|
|
|
examples
|
|
|
basic
command
|
|
|
internal
|
|
|
cli
Package cli implements the flag-based CLI for go-seeder.
|
Package cli implements the flag-based CLI for go-seeder. |
|
driver
Package driver defines the database driver interface and provides factory functions to create drivers by name or auto-detect from DSN.
|
Package driver defines the database driver interface and provides factory functions to create drivers by name or auto-detect from DSN. |
|
loader
Package loader provides seed file parsing for go-seeder.
|
Package loader provides seed file parsing for go-seeder. |
|
seeder
Package seeder provides the core engine for versioned seed operations.
|
Package seeder provides the core engine for versioned seed operations. |