add seeding demo
This commit is contained in:
52
cmd/seed.go
Normal file
52
cmd/seed.go
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
_ "atom/database/seeders"
|
||||
_ "atom/providers"
|
||||
|
||||
"atom/container"
|
||||
"atom/contracts"
|
||||
"log"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"go.uber.org/dig"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// seedCmd represents the seed command
|
||||
var seedCmd = &cobra.Command{
|
||||
Use: "seed",
|
||||
Short: "seed databases",
|
||||
Long: `seed your database with data using seeders.`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return container.Container.Invoke(func(seedersContainer SeedersContainer) error {
|
||||
if len(seedersContainer.Seeders) == 0 {
|
||||
log.Print("no seeder exists")
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, seeder := range seedersContainer.Seeders {
|
||||
log.Printf("seed table (%s) for %d times", seeder.Table(), seeder.Times())
|
||||
seeder.Run(seedersContainer.DB)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
},
|
||||
PostRun: func(cmd *cobra.Command, args []string) {
|
||||
log.Println("BINGO! seeding done")
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(seedCmd)
|
||||
}
|
||||
|
||||
type SeedersContainer struct {
|
||||
dig.In
|
||||
|
||||
DB *gorm.DB
|
||||
Seeders []contracts.Seeder `group:"seeders"`
|
||||
}
|
||||
10
contracts/seeder.go
Normal file
10
contracts/seeder.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package contracts
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
// Migration route interface
|
||||
type Seeder interface {
|
||||
Times() uint
|
||||
Table() string
|
||||
Run(*gorm.DB)
|
||||
}
|
||||
59
database/seeders/user.go
Normal file
59
database/seeders/user.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package seeders
|
||||
|
||||
import (
|
||||
"atom/container"
|
||||
"atom/contracts"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"go.uber.org/dig"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func init() {
|
||||
if err := container.Container.Provide(NewUserSeeder, dig.Group("seeders")); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
type UserSeeder struct {
|
||||
}
|
||||
|
||||
func NewUserSeeder() contracts.Seeder {
|
||||
return &UserSeeder{}
|
||||
}
|
||||
|
||||
type User struct {
|
||||
gorm.Model
|
||||
|
||||
Username string
|
||||
Password string
|
||||
Birthday time.Time
|
||||
Status bool
|
||||
}
|
||||
|
||||
func (s *UserSeeder) Times() uint {
|
||||
return 10
|
||||
}
|
||||
|
||||
func (s *UserSeeder) Run(db *gorm.DB) {
|
||||
var i uint
|
||||
for i = 0; i < s.Times(); i++ {
|
||||
data := s.Generate(i)
|
||||
db.Table(s.Table()).Create(&data)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *UserSeeder) Table() string {
|
||||
return "users"
|
||||
}
|
||||
|
||||
func (s *UserSeeder) Generate(idx uint) User {
|
||||
return User{
|
||||
Username: fmt.Sprintf("username: %d", idx),
|
||||
Password: fmt.Sprintf("password: %d", idx),
|
||||
Birthday: time.Now().Add(time.Hour * 24 * time.Duration(idx)),
|
||||
Status: idx%2 == 0,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user