Skip to content

cheerego/gorm-generics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gorm-generics

go generics use in gorm demo

example

package main

import (
	"context"
	gorm_generic "github.com/cheerego/gorm-generics"
	"gorm.io/gorm"
	"log"
)

type Student struct {
	gorm.Model
	Name string
	Age  int
}

type StudentRepository struct {
	gorm_generic.BaseRepository[Student]
	db *gorm.DB
}

func NewStudentRepository(db *gorm.DB) *StudentRepository {
	return &StudentRepository{
		BaseRepository: gorm_generic.NewBaseRepository[Student](db),
		db:             db,
	}
}
func (s *StudentRepository) CustomQuery(ctx context.Context) (*Student, error) {
	var m Student
	err := gorm_generic.FromContext(ctx, s.db).First(&m, 1).Error
	if err != nil {
		return nil, err
	}
	return &m, nil
}

func main() {
	db, err := gorm.Open(nil)
	if err != nil {
		log.Fatalln(err)
	}

	r := NewStudentRepository(db)

	students := []*Student{
		{
			Name: "test1",
			Age:  18,
		},
		{
			Name: "test2",
			Age:  18,
		},
	}

	r.BatchInsert(context.TODO(), students)
	r.FindById(context.TODO(), 1)
	r.FindByIdsWithDeleted(context.TODO(), 1)
	r.CustomQuery(context.TODO())

}

Releases

No releases published

Packages

No packages published

Languages