gormrepository

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

README

go-gorm-repository

This is a simple repository implementation for GORM providing basic functions to CRUD and query entities as well as transactions and common error handling.

Example

For a complete list of supported methods, please see types.go.

package main

import (
	"github.com/sirupsen/logrus"
	"github.com/aklinkert/go-gorm-repository"
	"gorm.io/gorm"
)

func main() {
	logger := logrus.New()
	db, _ := gorm.Open(_, _)
	
	// third parameter is a list of related entities that should always preload
	repo := gormrepository.NewGormRepository(db, logger, "CreatorUser", "Organization")
	
	instance := &exampleModel{}

	if err := repo.Create(instance); err != nil {
		logger.Fatalf("failed to create cache instance: %v", err)
	}
}

License

Apache 2.0 License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is a convenience reference for the actual GORM error
	ErrNotFound = gorm.ErrRecordNotFound
)

Functions

This section is empty.

Types

type Repository

type Repository interface {
	GetAll(target interface{}, preloads ...string) error
	GetBatch(target interface{}, limit, offset int, preloads ...string) error

	GetWhere(target interface{}, condition string, preloads ...string) error
	GetWhereBatch(target interface{}, condition string, limit, offset int, preloads ...string) error

	GetByField(target interface{}, field string, value interface{}, preloads ...string) error
	GetByFields(target interface{}, filters map[string]interface{}, preloads ...string) error
	GetByFieldBatch(target interface{}, field string, value interface{}, limit, offset int, preloads ...string) error
	GetByFieldsBatch(target interface{}, filters map[string]interface{}, limit, offset int, preloads ...string) error

	GetOneByField(target interface{}, field string, value interface{}, preloads ...string) error
	GetOneByFields(target interface{}, filters map[string]interface{}, preloads ...string) error

	// GetOneByID assumes you have a PK column "id" which is a UUID. If this is not the case just ignore the method
	// and add a custom struct with this Repository embedded.
	GetOneByID(target interface{}, id string, 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
}

Repository is a generic DB handler that cares about default error handling

type TransactionRepository

type TransactionRepository interface {
	Repository
	CreateTx(target interface{}, tx *gorm.DB) error
	SaveTx(target interface{}, tx *gorm.DB) error
	DeleteTx(target interface{}, tx *gorm.DB) error
}

TransactionRepository extends Repository with modifier functions that accept a transaction

func NewGormRepository

func NewGormRepository(db *gorm.DB, logger logging.Logger, defaultJoins ...string) TransactionRepository

NewGormRepository returns a new base repository that implements TransactionRepository

Jump to

Keyboard shortcuts

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