Documentation
¶
Index ¶
- Variables
- type EntityFilter
- type EntityUpdater
- type Filter
- type GormRepository
- func (r *GormRepository[Entity, Filter, Updater]) Count(ctx context.Context, filter Filter) (int64, error)
- func (r *GormRepository[Entity, Filter, Updater]) Create(ctx context.Context, records ...*Entity) error
- func (r *GormRepository[Entity, Filter, Updater]) CreateInBatches(ctx context.Context, batchSize int, records ...*Entity) error
- func (r *GormRepository[Entity, Filter, Updater]) Delete(ctx context.Context, record *Entity) error
- func (r *GormRepository[Entity, Filter, Updater]) DeleteByID(ctx context.Context, id int64) error
- func (r *GormRepository[Entity, Filter, Updater]) DeleteWithFilter(ctx context.Context, filter Filter) (int64, error)
- func (r *GormRepository[Entity, Filter, Updater]) Exists(ctx context.Context, filter Filter) (bool, error)
- func (r *GormRepository[Entity, Filter, Updater]) FindAll(ctx context.Context, filter Filter, options ...OptionFunc) ([]*Entity, error)
- func (r *GormRepository[Entity, Filter, Updater]) FindOne(ctx context.Context, filter Filter, options ...OptionFunc) (*Entity, bool, error)
- func (r *GormRepository[Entity, Filter, Updater]) FindOneByID(ctx context.Context, id int64) (*Entity, bool, error)
- func (r *GormRepository[Entity, Filter, Updater]) GetDB() *gorm.DB
- func (r *GormRepository[Entity, Filter, Updater]) Health(ctx context.Context) error
- func (r *GormRepository[Entity, Filter, Updater]) Update(ctx context.Context, record *Entity, updater Updater) error
- func (r *GormRepository[Entity, Filter, Updater]) UpdateWithFilter(ctx context.Context, filter Filter, updater Updater) (int64, error)
- func (r *GormRepository[Entity, Filter, Updater]) WithTransaction(ctx context.Context, fn func(*GormRepository[Entity, Filter, Updater]) error) error
- type Operator
- type OptionFunc
- type Options
- type Repository
- type SortField
Constants ¶
This section is empty.
Variables ¶
var ( // ErrEmptyInputFile indicates that an input file path was not provided ErrEmptyInputFile = errors.New("input file path cannot be empty") // ErrEmptyOutputFile indicates that an output file path was not provided ErrEmptyOutputFile = errors.New("output file path cannot be empty") // ErrNilParser indicates that a nil parser was provided to the generator ErrNilParser = errors.New("structs parser cannot be nil") // ErrInputFileNotFound indicates that the specified input file does not exist ErrInputFileNotFound = errors.New("input file does not exist") // ErrNoGoFiles indicates that no Go files were found in the specified directory ErrNoGoFiles = errors.New("no Go files found in directory") // ErrUnknownOperator indicates that an unknown operator was used in a filter ErrUnknownOperator = errors.New("unknown operator in filter") )
Input validation errors
var ( // ErrNoStructsProvided indicates that no structs were provided for generation ErrNoStructsProvided = errors.New("no structs provided for generation") // ErrNoAnnotatedStructs indicates that no structs with querybuilder annotations were found ErrNoAnnotatedStructs = errors.New("no structs with querybuilder annotations found") )
Generation errors
var ( // ErrNoRecordsProvided indicates that no records were provided for a batch operation ErrNoRecordsProvided = errors.New("no records provided for creation") // ErrEmptyFieldName indicates that a filter has an empty field name ErrEmptyFieldName = errors.New("empty field name in filter") // ErrCreateRecords indicates that record creation failed ErrCreateRecords = errors.New("failed to create records") // ErrFindRecord indicates that record lookup failed ErrFindRecord = errors.New("failed to find record") // ErrUpdateRecord indicates that record update failed ErrUpdateRecord = errors.New("failed to update record") // ErrDeleteRecord indicates that record deletion failed ErrDeleteRecord = errors.New("failed to delete record") // ErrNoRecordDeleted indicates that no record was deleted ErrNoRecordDeleted = errors.New("no record was deleted") // ErrBuildQuery indicates that query building failed ErrBuildQuery = errors.New("failed to build query") // ErrCountRecords indicates that record counting failed ErrCountRecords = errors.New("failed to count records") // ErrExistsCheck indicates that exists check failed ErrExistsCheck = errors.New("failed to check record existence") // ErrGetDatabase indicates that getting underlying database failed ErrGetDatabase = errors.New("failed to get underlying database") // ErrDatabasePing indicates that database ping failed ErrDatabasePing = errors.New("database ping failed") )
Repository operation errors
var ( // ErrTemplateExecution indicates that template execution failed ErrTemplateExecution = errors.New("failed to execute template") // ErrCodeFormatting indicates that code formatting failed ErrCodeFormatting = errors.New("failed to format generated code") )
Template and formatting errors
var ( // ErrCreateOutputDir indicates that the output directory could not be created ErrCreateOutputDir = errors.New("failed to create output directory") // ErrWriteGeneratedCode indicates that generated code could not be written to file ErrWriteGeneratedCode = errors.New("failed to write generated code") )
File operation errors
var ( // ErrParseFile indicates that a file could not be parsed ErrParseFile = errors.New("failed to parse file") // ErrLoadPackage indicates that a package could not be loaded ErrLoadPackage = errors.New("failed to load package") // ErrTooManyPackages indicates that more packages were found than expected ErrTooManyPackages = errors.New("found more packages than expected") // ErrGetAbsPath indicates that an absolute path could not be determined ErrGetAbsPath = errors.New("failed to get absolute path") )
Parser errors
Functions ¶
This section is empty.
Types ¶
type EntityFilter ¶
type EntityFilter interface {
ListFilters() []*Filter
}
type EntityUpdater ¶
type EntityUpdater interface {
GetChangeSet() map[string]interface{}
}
type GormRepository ¶
type GormRepository[Entity any, Filter EntityFilter, Updater EntityUpdater] struct { // contains filtered or unexported fields }
GormRepository provides a complete GORM-based repository implementation that integrates seamlessly with the existing filter and updater system
func NewGormRepository ¶
func NewGormRepository[Entity any, Filter EntityFilter, Updater EntityUpdater]( db *gorm.DB, ) *GormRepository[Entity, Filter, Updater]
NewGormRepository creates a new GORM-based repository
func (*GormRepository[Entity, Filter, Updater]) Count ¶
func (r *GormRepository[Entity, Filter, Updater]) Count( ctx context.Context, filter Filter, ) (int64, error)
Count implements record counting
func (*GormRepository[Entity, Filter, Updater]) Create ¶
func (r *GormRepository[Entity, Filter, Updater]) Create(ctx context.Context, records ...*Entity) error
Create implements efficient record creation
func (*GormRepository[Entity, Filter, Updater]) CreateInBatches ¶
func (r *GormRepository[Entity, Filter, Updater]) CreateInBatches( ctx context.Context, batchSize int, records ...*Entity, ) error
CreateInBatches implements batch creation
func (*GormRepository[Entity, Filter, Updater]) Delete ¶
func (r *GormRepository[Entity, Filter, Updater]) Delete( ctx context.Context, record *Entity, ) error
Delete implements single record deletion
func (*GormRepository[Entity, Filter, Updater]) DeleteByID ¶
func (r *GormRepository[Entity, Filter, Updater]) DeleteByID( ctx context.Context, id int64, ) error
DeleteByID implements single record deletion by ID
func (*GormRepository[Entity, Filter, Updater]) DeleteWithFilter ¶
func (r *GormRepository[Entity, Filter, Updater]) DeleteWithFilter( ctx context.Context, filter Filter, ) (int64, error)
DeleteWithFilter implements batch deletion using filters
func (*GormRepository[Entity, Filter, Updater]) Exists ¶
func (r *GormRepository[Entity, Filter, Updater]) Exists( ctx context.Context, filter Filter, ) (bool, error)
Exists checks if any records match the filter efficiently
func (*GormRepository[Entity, Filter, Updater]) FindAll ¶
func (r *GormRepository[Entity, Filter, Updater]) FindAll( ctx context.Context, filter Filter, options ...OptionFunc, ) ([]*Entity, error)
FindAll implements multiple record lookup with filters
func (*GormRepository[Entity, Filter, Updater]) FindOne ¶
func (r *GormRepository[Entity, Filter, Updater]) FindOne( ctx context.Context, filter Filter, options ...OptionFunc, ) (*Entity, bool, error)
FindOne implements single record lookup with filters
func (*GormRepository[Entity, Filter, Updater]) FindOneByID ¶
func (r *GormRepository[Entity, Filter, Updater]) FindOneByID( ctx context.Context, id int64, ) (*Entity, bool, error)
FindOneByID implements single record lookup by ID
func (*GormRepository[Entity, Filter, Updater]) GetDB ¶
func (r *GormRepository[Entity, Filter, Updater]) GetDB() *gorm.DB
GetDB returns the underlying GORM database instance for advanced operations
func (*GormRepository[Entity, Filter, Updater]) Health ¶
func (r *GormRepository[Entity, Filter, Updater]) Health(ctx context.Context) error
Health performs a health check on the database connection
func (*GormRepository[Entity, Filter, Updater]) Update ¶
func (r *GormRepository[Entity, Filter, Updater]) Update( ctx context.Context, record *Entity, updater Updater, ) error
Update implements record updates using updaters
func (*GormRepository[Entity, Filter, Updater]) UpdateWithFilter ¶
func (r *GormRepository[Entity, Filter, Updater]) UpdateWithFilter( ctx context.Context, filter Filter, updater Updater, ) (int64, error)
UpdateWithFilter implements batch updates using filters
func (*GormRepository[Entity, Filter, Updater]) WithTransaction ¶
func (r *GormRepository[Entity, Filter, Updater]) WithTransaction( ctx context.Context, fn func(*GormRepository[Entity, Filter, Updater]) error, ) error
WithTransaction executes a function within a database transaction
type Operator ¶
type Operator string
const ( OperatorEqual Operator = "=" OperatorNotEqual Operator = "!=" OperatorLessThan Operator = "<" OperatorLessThanOrEqual Operator = "<=" OperatorGreaterThan Operator = ">" OperatorGreaterThanOrEqual Operator = ">=" OperatorLike Operator = "LIKE" OperatorNotLike Operator = "NOT_LIKE" OperatorIsNull Operator = "IS_NULL" OperatorIsNotNull Operator = "IS_NOT_NULL" OperatorIn Operator = "IN" OperatorNotIn Operator = "NOT_IN" )
Enum values for Operator
type OptionFunc ¶
type OptionFunc interface {
Apply(*Options)
}
func WithLimit ¶
func WithLimit(limit int) OptionFunc
func WithOffset ¶
func WithOffset(offset int) OptionFunc
type Repository ¶
type Repository[Entity any, Filter EntityFilter, Updater EntityUpdater] interface { // Create creates one or more records Create(ctx context.Context, records ...*Entity) error // FindOneByID finds a single record by its ID FindOneByID(ctx context.Context, id int64) (*Entity, bool, error) // FindOne finds a single record matching the filter FindOne(ctx context.Context, filter Filter, options ...OptionFunc) (*Entity, bool, error) // FindAll finds all records matching the filter FindAll(ctx context.Context, filter Filter, options ...OptionFunc) ([]*Entity, error) // Update updates a single record using the updater Update(ctx context.Context, record *Entity, updater Updater) error // Delete deletes a single record Delete(ctx context.Context, record *Entity) error // DeleteByID deletes a single record by its ID DeleteByID(ctx context.Context, id int64) error // CreateInBatches creates records in batches CreateInBatches(ctx context.Context, batchSize int, records ...*Entity) error // UpdateWithFilter updates all records matching the filter UpdateWithFilter(ctx context.Context, filter Filter, updater Updater) (int64, error) // DeleteWithFilter deletes all records matching the filter DeleteWithFilter(ctx context.Context, filter Filter) (int64, error) // Count counts records matching the filter Count(ctx context.Context, filter Filter) (int64, error) // Exists checks if any records match the filter Exists(ctx context.Context, filter Filter) (bool, error) // GetDB returns the underlying GORM database instance GetDB() *gorm.DB // Health performs a health check on the database connection Health(ctx context.Context) error }
Repository defines the contract for a generic repository that provides CRUD operations and query capabilities