gormgenericrepo

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2023 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddGormBeforeCreateUUIDIDSetCallback added in v0.0.3

func AddGormBeforeCreateUUIDIDSetCallback(db *gorm.DB)

AddGormBeforeCreateUUIDIDSetCallback sets before "gorm:before_create" callback that sets uuid ids for entities

func ToSnakeCase

func ToSnakeCase(str string) string

ToSnakeCase converts camelCase field name to snake_case

Types

type BaseTestEntity added in v0.0.6

type BaseTestEntity[IDType comparable] struct {
	Entity[IDType]
	TestEntityFields
}

BaseTestEntity base test entity struct (generic)

func (BaseTestEntity[IDType]) GetDataFields added in v0.0.6

func (t BaseTestEntity[IDType]) GetDataFields() TestEntityFields

GetDataFields gets struct with entity data fields

type Entity added in v0.0.6

type Entity[IDType comparable] struct {
	//	ID - entity identifier
	ID IDType `gorm:"primary_key;"`
	//	CreatedAt - time when the entity was created
	CreatedAt time.Time `gorm:"index"`
	//	UpdatedAt - time when the entity was updated
	UpdatedAt *time.Time `gorm:"index"`
	//	DeletedAt - time when the entity was deleted
	DeletedAt gorm.DeletedAt `gorm:"index"`
}

Entity structure

func (Entity[IDType]) GetCreatedAt added in v0.0.6

func (e Entity[IDType]) GetCreatedAt() time.Time

GetCreatedAt time

func (Entity[IDType]) GetDeletedAt added in v0.0.6

func (e Entity[IDType]) GetDeletedAt() *time.Time

GetDeletedAt time

func (Entity[IDType]) GetID added in v0.0.6

func (e Entity[IDType]) GetID() IDType

GetID of the entity

func (Entity[IDType]) GetUpdatedAt added in v0.0.6

func (e Entity[IDType]) GetUpdatedAt() *time.Time

GetUpdatedAt time

type EntityUUID added in v0.0.6

type EntityUUID struct {
	Entity[uuid.UUID]
	//	ID - entity identifier
	ID uuid.UUID `gorm:"primary_key; type:varchar(36)"`
}

EntityUUID base entity structure where ID type is uuid.UUID

func (EntityUUID) GetID added in v0.0.6

func (e EntityUUID) GetID() uuid.UUID

GetID of the entity

type GenericRepositoryTester added in v0.0.6

type GenericRepositoryTester[IDType comparable, EntityType ITestEntity[IDType]] struct {
	IDTypeName     string
	EntityTypeName string
	Implementation string
	// contains filtered or unexported fields
}

GenericRepositoryTester generic struct for repository tester

func NewGenericRepositoryTester added in v0.0.6

func NewGenericRepositoryTester[IDType comparable, EntityType ITestEntity[IDType]](repo interfaces.IGenericRepository[IDType, EntityType]) (*GenericRepositoryTester[IDType, EntityType], error)

NewGenericRepositoryTester constructor for generic repository tester

func (*GenericRepositoryTester[IDType, EntityType]) Dispose added in v0.0.6

func (t *GenericRepositoryTester[IDType, EntityType]) Dispose() error

Dispose all tester stuff

func (*GenericRepositoryTester[IDType, EntityType]) SetTesting added in v0.0.6

func (t *GenericRepositoryTester[IDType, EntityType]) SetTesting(testing *testing.T)

SetTesting sets testing for interactive with tests stuff

func (*GenericRepositoryTester[IDType, EntityType]) TestCount added in v0.0.6

func (t *GenericRepositoryTester[IDType, EntityType]) TestCount()

TestCount testing for Count method

func (*GenericRepositoryTester[IDType, EntityType]) TestDelete added in v0.0.6

func (t *GenericRepositoryTester[IDType, EntityType]) TestDelete()

TestDelete Deletion test

func (*GenericRepositoryTester[IDType, EntityType]) TestDeleteAll added in v0.0.6

func (t *GenericRepositoryTester[IDType, EntityType]) TestDeleteAll()

TestDeleteAll delete all test

func (*GenericRepositoryTester[IDType, EntityType]) TestGetByID added in v0.0.6

func (t *GenericRepositoryTester[IDType, EntityType]) TestGetByID()

TestGetByID get by id repo testing

func (*GenericRepositoryTester[IDType, EntityType]) TestGetByIDExtended added in v0.0.6

func (t *GenericRepositoryTester[IDType, EntityType]) TestGetByIDExtended()

TestGetByIDExtended get by id with conditions repo testing

func (*GenericRepositoryTester[IDType, EntityType]) TestInsertAndDelete added in v0.0.6

func (t *GenericRepositoryTester[IDType, EntityType]) TestInsertAndDelete()

TestInsertAndDelete test inserting entity to repository and check that fields of entity wrote too

func (*GenericRepositoryTester[IDType, EntityType]) TestIsExist added in v0.0.6

func (t *GenericRepositoryTester[IDType, EntityType]) TestIsExist()

TestIsExist testing for IsExist method

type GormGenericRepository

type GormGenericRepository[EntityIDType comparable, EntityType interfaces.IEntity[EntityIDType]] struct {
	// contains filtered or unexported fields
}

GormGenericRepository is implementation of interfaces.IGenericRepository

func NewGormGenericRepository

func NewGormGenericRepository[EntityIDType comparable, EntityType interfaces.IEntity[EntityIDType]](db *gorm.DB, log *logrus.Logger) *GormGenericRepository[EntityIDType, EntityType]

NewGormGenericRepository GORM generic repository constructor

Params

  • *gorm.DB - gorm database
  • *logrus.Logger - logrus logger

Return

  • *GormGenericRepository[EntityType] - repository for instantiated entity

func (*GormGenericRepository[EntityIDType, EntityType]) Count

func (g *GormGenericRepository[EntityIDType, EntityType]) Count(ctx context.Context, queryBuilder interfaces.IQueryBuilder) (int, error)

Count gets total count of entities with current query

Params

  • ctx - context
  • queryBuilder - query builder with conditions

Return

  • int64 - number of entities
  • error - if an error occurs, otherwise nil

func (*GormGenericRepository[EntityIDType, EntityType]) DB added in v0.0.5

func (g *GormGenericRepository[EntityIDType, EntityType]) DB() *gorm.DB

func (*GormGenericRepository[EntityIDType, EntityType]) Delete

func (g *GormGenericRepository[EntityIDType, EntityType]) Delete(ctx context.Context, id EntityIDType) error

Delete entity from the repository

Params

  • ctx - context
  • id - entity id

Return

  • error - if an error occurs, otherwise nil

func (*GormGenericRepository[EntityIDType, EntityType]) DeleteAll

func (g *GormGenericRepository[EntityIDType, EntityType]) DeleteAll(ctx context.Context, queryBuilder interfaces.IQueryBuilder) error

DeleteAll entities matching the condition

Params

  • ctx - context
  • queryBuilder - query builder with conditions

Return

  • error - if an error occurs, otherwise nil

func (*GormGenericRepository[EntityIDType, EntityType]) Dispose

func (g *GormGenericRepository[EntityIDType, EntityType]) Dispose() error

Dispose releases all resources

Return

  • error - if an error occurred, otherwise nil

func (*GormGenericRepository[EntityIDType, EntityType]) GetByID

func (g *GormGenericRepository[EntityIDType, EntityType]) GetByID(ctx context.Context, id EntityIDType) (EntityType, error)

GetByID gets entity by ID from repository

Params

  • ctx - context
  • id - entity id

Return

  • EntityType - point to entity
  • error - if an error occurs, otherwise nil

func (*GormGenericRepository[EntityIDType, EntityType]) GetByIDExtended

func (g *GormGenericRepository[EntityIDType, EntityType]) GetByIDExtended(ctx context.Context, id EntityIDType, queryBuilder interfaces.IQueryBuilder) (EntityType, error)

GetByIDExtended Get entity by ID and query from repository

Params

  • ctx - context
  • id - entity id
  • queryBuilder - extended query conditions

Return

  • *EntityType - point to entity
  • error - if an error occurs, otherwise nil

func (*GormGenericRepository[EntityIDType, EntityType]) GetList

func (g *GormGenericRepository[EntityIDType, EntityType]) GetList(ctx context.Context, orderBy string, orderDirection string, page int, size int, queryBuilder interfaces.IQueryBuilder) ([]EntityType, error)

GetList of elements with filtering and pagination

Params

  • ctx - context is used only for logging
  • orderBy - order by string parameter
  • orderDirection - ascending or descending order
  • page - page number
  • size - page size
  • queryBuilder - query builder for filtering

Return

  • []EntityType - pointer to array of entities
  • error - if an error occurs, otherwise nil

func (*GormGenericRepository[EntityIDType, EntityType]) Insert

func (g *GormGenericRepository[EntityIDType, EntityType]) Insert(ctx context.Context, entity EntityType) (EntityType, error)

Insert entity to the repository

Params

  • ctx - context
  • entity - entity to save

Return

  • EntityType - created entity
  • error - if an error occurs, otherwise nil

func (*GormGenericRepository[EntityIDType, EntityType]) IsExist

func (g *GormGenericRepository[EntityIDType, EntityType]) IsExist(ctx context.Context, id EntityIDType, queryBuilder interfaces.IQueryBuilder) (bool, error)

IsExist checks that entity is existed in repository

Params

  • ctx - context is used only for logging
  • id - id of the entity
  • queryBuilder - query builder with addition conditions, can be nil

Return

  • bool - true if existed, otherwise false
  • error - if an error occurs, otherwise nil

func (*GormGenericRepository[EntityIDType, EntityType]) NewQueryBuilder

func (g *GormGenericRepository[EntityIDType, EntityType]) NewQueryBuilder(ctx context.Context) interfaces.IQueryBuilder

NewQueryBuilder gets new query builder

Params

  • ctx - context is used only for logging

Return

  • interfaces.IQueryBuilder - new query builder

func (*GormGenericRepository[EntityIDType, EntityType]) Update

func (g *GormGenericRepository[EntityIDType, EntityType]) Update(ctx context.Context, entity EntityType) (EntityType, error)

Update save the changes to the existing entity in the repository

Params

  • ctx - context
  • entity - updated entity to save

Return

  • EntityType - updated entity
  • error - if an error occurs, otherwise nil

type GormQueryBuilder

type GormQueryBuilder struct {
	QueryString string
	Values      []interface{}
}

GormQueryBuilder query builder struct for gorm

func NewGormQueryBuilder

func NewGormQueryBuilder() *GormQueryBuilder

NewGormQueryBuilder Gets new query builder instance

func (*GormQueryBuilder) Build

func (g *GormQueryBuilder) Build() (interface{}, error)

Build build a slice of query arguments Return

  • interface{} - slice of interface{}
  • error - if error occurs return error, otherwise nil

func (*GormQueryBuilder) Or

func (g *GormQueryBuilder) Or(fieldName, comparator string, value interface{}) interfaces.IQueryBuilder

Or add new OR condition to the query Params

  • fieldName - name of the field
  • comparator - comparison logical operator
  • value - value of the field

Return

  • interfaces.IQueryBuilder - updated query builder

func (*GormQueryBuilder) OrQuery

OrQuery add new complicated OR condition to the query based on another query Params

  • interfaces.IQueryBuilder - a ready-builder to the query

Return

  • interfaces.IQueryBuilder - updated query builder

func (*GormQueryBuilder) Where

func (g *GormQueryBuilder) Where(fieldName, comparator string, value interface{}) interfaces.IQueryBuilder

Where add new AND condition to the query Params

  • fieldName - name of the field
  • comparator - comparison logical operator
  • value - value of the field

Return

  • interfaces.IQueryBuilder - updated query builder

func (*GormQueryBuilder) WhereQuery

WhereQuery add new complicated AND condition to the query based on another query Params

  • interfaces.IQueryBuilder - a ready-builder to the query

Return

  • interfaces.IQueryBuilder - updated query builder

type GormSQLiteGenericRepositoryTester added in v0.0.6

type GormSQLiteGenericRepositoryTester[IDType comparable, EntityType ITestEntity[IDType]] struct {
	*GenericRepositoryTester[IDType, EntityType]
	// contains filtered or unexported fields
}

GormSQLiteGenericRepositoryTester tester for gorm sqlite repository

func NewGormSQLiteGenericRepositoryTester added in v0.0.6

func NewGormSQLiteGenericRepositoryTester[IDType comparable, EntityType ITestEntity[IDType]]() *GormSQLiteGenericRepositoryTester[IDType, EntityType]

NewGormSQLiteGenericRepositoryTester constructor for gorm sqlite repository tester

func (*GormSQLiteGenericRepositoryTester[IDType, EntityType]) Dispose added in v0.0.6

func (t *GormSQLiteGenericRepositoryTester[IDType, EntityType]) Dispose() error

Dispose all tester stuff

type IGenericRepositoryTester added in v0.0.6

type IGenericRepositoryTester interface {
	SetTesting(testing *testing.T)
	Dispose() error
	TestInsertAndDelete()
	TestDeleteAll()
	TestGetByID()
	TestGetByIDExtended()
	TestDelete()
	TestIsExist()
	TestCount()
}

IGenericRepositoryTester interface that need to implement for each repository implementation

type ITestEntity added in v0.0.6

type ITestEntity[IDType comparable] interface {
	interfaces.IEntity[IDType]
	GetDataFields() TestEntityFields
}

ITestEntity default interface that we need for repository entity testing

type TestEntityFields added in v0.0.6

type TestEntityFields struct {
	String       string
	SecondString string
	SearchString string
	Yesterday    time.Time
	Tomorrow     time.Time
	NullDate     *time.Time
	NullableDate *time.Time
	Number       int
	Iterator     int
}

TestEntityFields fields for test entity

func GetTestEntityFields added in v0.0.6

func GetTestEntityFields() TestEntityFields

GetTestEntityFields gets preset of entity test fields

func GetTestEntityFieldsWithIteration added in v0.0.6

func GetTestEntityFieldsWithIteration(iteration int) TestEntityFields

GetTestEntityFieldsWithIteration gets preset of entity test fields with selected iterator value

func (TestEntityFields) Equals added in v0.0.6

func (f TestEntityFields) Equals(fields TestEntityFields) bool

Equals checks equivalence of th fields

type TestEntityInt added in v0.0.6

type TestEntityInt struct {
	BaseTestEntity[int]
}

TestEntityInt base test entity struct with int id

type TestEntityUUID added in v0.0.6

type TestEntityUUID struct {
	BaseTestEntity[uuid.UUID]
}

TestEntityUUID base test entity struct with uuid id

Jump to

Keyboard shortcuts

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