gormrepository

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2023 License: GPL-3.0 Imports: 2 Imported by: 0

README

Gorm Repository Boilerplate

 

Gorm Repository Boilerplate

Github top language Github language count Repository size License Github issues Github forks Github stars

About   |   Features   |   Technologies   |   Requirements   |   Usage   |   License   |   Author


🎯 About

Gorm Repository Boilerplate is a boilerplate for gorm repository pattern. It is a providing basic functions to CRUD and query entities as well as transactions and common error handling. It is a good starting point for gorm repository pattern.

✨ Features

✔ CRUD

🚀 Technologies

The following tools were used in this project:

✅ Requirements

Before starting 🏁, you need to have Go installed.

🏁 Usage

package base

import "github.com/bangadam/gorm-repository-boilerplate"

type BaseRepository interface {
	gormrepository.TransactionRepository
	FindByName(target interface{}, name string, preloads ...string) error
}

type repository struct {
	gormrepository.TransactionRepository
}

func NewRepository(db *gorm.DB, logger logging.Logger) BaseRepository {
	return &repository{
		TransactionRepository: gormrepository.NewGormRepository(db, logger, "Creator"),
	}
}

func (r *repository) FindByName(target interface{}, name string, preloads ...string) error {
	return r.TransactionRepository.FindOneByField(target, "name", name, preloads...)
}

📝 License

This project is under license from MIT. For more details, see the LICENSE file.

Made with ❤ by bangadam

 

Back to top

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRecordNotFound = gorm.ErrRecordNotFound
)

Functions

func NewGormRepository

func NewGormRepository(db *gorm.DB, defaultJoins ...string) *gormRepository

Types

type Repository

type Repository interface {
	FindByRaw(target interface{}, query string) error

	FindAll(target interface{}, preloads ...string) error
	FindBatch(target interface{}, limit, offset int, search, orderBy, groupBy string, preloads ...string) (count int64, err error)

	FindWhere(target interface{}, condition string, preloads ...string) error
	FindWhereBatch(target interface{}, condition string, limit, offset int, search, orderBy, groupBy string, preloads ...string) (count int64, err error)

	FindByField(target interface{}, field string, value interface{}, preloads ...string) error
	FindByFields(target interface{}, fields map[string]interface{}, preloads ...string) error

	FindByFieldBatch(target interface{}, field string, value interface{}, limit, offset int, search, orderBy, groupBy string, preloads ...string) (count int64, err error)
	FindByFieldsBatch(target interface{}, fields map[string]interface{}, limit, offset int, search, orderBy, groupBy string, preloads ...string) (count int64, err error)

	FindOneByField(target interface{}, field string, value interface{}, preloads ...string) error
	FindOneLastByField(target interface{}, field string, value interface{}, preloads ...string) error
	FindOneByFields(target interface{}, fields map[string]interface{}, preloads ...string) error
	FindOneByCondition(target interface{}, condition string, preloads ...string) error
	FindOneLastByCondition(target interface{}, condition string, preloads ...string) error

	FindOneByID(target interface{}, id interface{}, preloads ...string) error

	Create(target interface{}) error
	Save(target interface{}) error
	Delete(target interface{}) error

	DB() *gorm.DB
	DBWithPreloads(preloads []string) *gorm.DB
	HandleError(res *gorm.DB) error
	HandleOneError(res *gorm.DB) error
}

type TransactionRepository

type TransactionRepository interface {
	Repository
	CreateTx(target interface{}, tx *gorm.DB) error
	SaveTx(target interface{}, tx *gorm.DB) error
	UpdateOrCreateTx(target interface{}, attributes map[string]interface{}, values map[string]interface{}, tx *gorm.DB) error
	DeleteTx(target interface{}, tx *gorm.DB) error
	DeleteTxByCondition(target interface{}, condition string, tx *gorm.DB) error
}

Jump to

Keyboard shortcuts

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