Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Kachit/gorm-seeder
Gorm seeder package
https://github.com/Kachit/gorm-seeder
database golang gorm seeder
Last synced: about 2 months ago
JSON representation
Gorm seeder package
- Host: GitHub
- URL: https://github.com/Kachit/gorm-seeder
- Owner: Kachit
- License: mit
- Created: 2022-05-02T14:47:21.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-11-20T14:30:08.000Z (about 2 years ago)
- Last Synced: 2024-07-31T20:49:42.552Z (5 months ago)
- Topics: database, golang, gorm, seeder
- Language: Go
- Homepage:
- Size: 31.3 KB
- Stars: 13
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - gorm-seeder - Simple database seeder for Gorm ORM. (Database / Database Schema Migration)
- awesome-go-extra - gorm-seeder - 05-02T14:47:21Z|2022-07-11T03:51:37Z| (Generators / Database Schema Migration)
README
# Simple Gorm seeder
[![Build Status](https://app.travis-ci.com/Kachit/gorm-seeder.svg?branch=master)](https://app.travis-ci.com/Kachit/gorm-seeder)
[![Codecov](https://codecov.io/gh/Kachit/gorm-seeder/branch/master/graph/badge.svg)](https://codecov.io/gh/Kachit/gorm-seeder)
[![Go Report Card](https://goreportcard.com/badge/github.com/kachit/gorm-seeder)](https://goreportcard.com/report/github.com/kachit/gorm-seeder)
[![Version](https://img.shields.io/github/go-mod/go-version/Kachit/gorm-seeder)](https://go.dev/doc/go1.14)
[![Release](https://img.shields.io/github/v/release/Kachit/gorm-seeder.svg)](https://github.com/Kachit/gorm-seeder/releases)
[![License](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/kachit/gorm-seeder/blob/master/LICENSE)
[![GoDoc](https://pkg.go.dev/badge/github.com/kachit/gorm-seeder)](https://pkg.go.dev/github.com/kachit/gorm-seeder)
[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go#third-party-apis)## Description
Simple Gorm seeder package## Download
```shell
go get -u github.com/kachit/gorm-seeder
```## Usage
```go
package mainimport (
"fmt"
"github.com/kachit/gorm-seeder"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"time"
)//Init model
type User struct {
Id uint64 `json:"id" gorm:"primaryKey"`
Name string `json:"name"`
Email string `json:"email"`
Password string `json:"password"`
CreatedAt time.Time `json:"created_at"`
}func (u *User) BeforeCreate(tx *gorm.DB) (err error) {
u.CreatedAt = time.Now().UTC()
return
}// TableName overrides
func (User) TableName() string {
return "users"
}//Create new seeder instance of gorm_seeder.SeederInterface
type UsersSeeder struct {
gorm_seeder.SeederAbstract
}func NewUsersSeeder(cfg gorm_seeder.SeederConfiguration) UsersSeeder {
return UsersSeeder{gorm_seeder.NewSeederAbstract(cfg)}
}//Implement Seed method
func (s *UsersSeeder) Seed(db *gorm.DB) error {
var users []User
for i := 0; i < s.Configuration.Rows; i++ {
indexStr := fmt.Sprint(i)
user := User{
Name: "Name LastName" + indexStr,
Email: "foo" + indexStr + "@bar.gov",
Password: "password-hash-string" + indexStr,
}
users = append(users, user)
}
return db.CreateInBatches(users, s.Configuration.Rows).Error
}//Implement Clear method
func (s *UsersSeeder) Clear(db *gorm.DB) error {
entity := User{}
return s.SeederAbstract.Delete(db, entity.TableName())
}func main(){
//Init DB connection
db, _ := gorm.Open(postgres.New(postgres.Config{
DSN: "DSN connection string",
}))//Build seeders stack
usersSeeder := NewUsersSeeder(gorm_seeder.SeederConfiguration{Rows: 10})
seedersStack := gorm_seeder.NewSeedersStack(db)
seedersStack.AddSeeder(&usersSeeder)//Apply seed
err := seedersStack.Seed()
fmt.Println(err)//Apply clear
err = seedersStack.Clear()
fmt.Println(err)
}
```