gormseed

package module
v0.0.0-...-892eca1 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2024 License: MIT Imports: 7 Imported by: 0

README

Gormseed

Gormseed is a command-line utility designed to work seamlessly with Gormseeder, providing an efficient and structured approach to creating and managing database seeders for projects using the Gorm ORM in Golang. While Gorm handles the intricacies of database interactions and migrations, Gormseed focuses on the vital task of populating your database with initial or sample data.

With Gormseed, you can easily generate seeder files that are organized and maintainable. Gormseed is particularly useful for developers who need a reliable and straightforward way to manage data population in their applications, complementing Gorm's powerful ORM capabilities with an equally robust data seeding solution.

How to install

go install github.com/raspiantoro/gormseeder/gormseed/gorms@latest

How to use

Before adding your seed files, you need to initialize Gormseeder within your application.

gorms init -c

Then, you can add a new seed with the following command:

gorms add seed "insert product"

It will produce a new seed file in your seeds directory with the following content:

// Code generated by Gormseed (gorms).

package seeds

import (
	"gorm.io/gorm"
)

// don't rename this function
func (s *Seeds) SeedInsertProduct(db *gorm.DB) error {
	// place your seed code here
	return nil
}

// don't rename this function
func (s *Seeds) RollbackInsertProduct(db *gorm.DB) error {
	// place your rollback code here
	return nil
}

You can update both your seed function and rollback function. Below is an example:

// don't rename this function
func (s *Seeds) SeedInsertProduct(db *gorm.DB) error {
	type Product struct {
		gorm.Model
		ID   uuid.UUID `gorm:"type:uuid;default:gen_random_uuid()"`
		Name string
	}

	products := []*Product{
		{
			ID:   uuid.MustParse("e5024ae0-c9e0-40f9-b2b7-9813e125cb16"),
			Name: "Amazon Fire TV Stick",
		},
		{
			ID:   uuid.MustParse("118ae13f-afd0-4433-89e3-bce9770c4cc9"),
			Name: "Samsung Galaxy Tab S6",
		},
	}

	for _, product := range products {
		result := db.Clauses(clause.OnConflict{DoNothing: true}).Create(&product)
		if result.Error != nil {
			return result.Error
		}
	}

	return nil
}

// don't rename this function
func (s *Seeds) RollbackInsertProduct(db *gorm.DB) error {
	type Product struct {
		gorm.Model
		ID   uuid.UUID `gorm:"type:uuid;default:gen_random_uuid()"`
		Name string
	}

	productIDs := []uuid.UUID{
		uuid.MustParse("e5024ae0-c9e0-40f9-b2b7-9813e125cb16"),
		uuid.MustParse("118ae13f-afd0-4433-89e3-bce9770c4cc9"),
	}

	result := db.Unscoped().Delete(&Product{}, productIDs)

	return result.Error
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(seeds Seeds) []*gormseeder.Seed

Types

type Seeds

type Seeds interface {
	Path() string
}

Directories

Path Synopsis
cmd
internal
app

Jump to

Keyboard shortcuts

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