repository

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entity

type Entity interface {
	// TableName returns the database table name for this entity
	// This should match GORM's table naming convention
	// If not implemented, GORM's default naming will be used
	TableName() string

	// GetPrimaryKeyValue returns the actual value of the primary key
	// Used for cache invalidation and dependency tracking
	// If not implemented, reflection will be used to find 'ID' field
	GetPrimaryKeyValue() interface{}
}

Entity interface defines the minimal contract for repository entities GORM models should implement this for optimal caching and relationship detection If not implemented, the repository will use reflection as fallback

type GenericRepository

type GenericRepository[T Entity] struct {
	// contains filtered or unexported fields
}

GenericRepository provides comprehensive CRUD operations with intelligent caching It automatically handles cache-first reads and relationship-aware invalidation

func (*GenericRepository[T]) Count

func (r *GenericRepository[T]) Count(ctx context.Context) (int64, error)

Count counts records with caching

func (*GenericRepository[T]) Create

func (r *GenericRepository[T]) Create(ctx context.Context, entity *T) error

Create creates a new record with automatic cache invalidation

func (*GenericRepository[T]) CreateBatch

func (r *GenericRepository[T]) CreateBatch(ctx context.Context, entities []*T) error

CreateBatch creates multiple records in batch with cache invalidation

func (*GenericRepository[T]) Delete

func (r *GenericRepository[T]) Delete(ctx context.Context, id interface{}) error

Delete deletes a record by ID with cache invalidation

func (*GenericRepository[T]) Exists

func (r *GenericRepository[T]) Exists(ctx context.Context, id interface{}) (bool, error)

Exists checks if a record exists by ID

func (*GenericRepository[T]) FindAll

func (r *GenericRepository[T]) FindAll(ctx context.Context) ([]T, error)

FindAll finds all records with caching

func (*GenericRepository[T]) FindByID

func (r *GenericRepository[T]) FindByID(ctx context.Context, id interface{}) (*T, error)

FindByID finds a record by ID with cache-first strategy

func (*GenericRepository[T]) FindWhere

func (r *GenericRepository[T]) FindWhere(ctx context.Context, query interface{}, args ...interface{}) ([]T, error)

FindWhere finds records with conditions and caching

func (*GenericRepository[T]) First

func (r *GenericRepository[T]) First(ctx context.Context, query interface{}, args ...interface{}) (*T, error)

First finds the first record matching conditions

func (*GenericRepository[T]) InvalidateCache

func (r *GenericRepository[T]) InvalidateCache(ctx context.Context) error

InvalidateCache invalidates all caches for this entity type in this database

func (*GenericRepository[T]) Joins

func (r *GenericRepository[T]) Joins(ctx context.Context, query string, args ...interface{}) Repository[T]

Joins specifies joins to perform

func (*GenericRepository[T]) Limit

func (r *GenericRepository[T]) Limit(ctx context.Context, limit int) Repository[T]

Limit specifies limit

func (*GenericRepository[T]) Offset

func (r *GenericRepository[T]) Offset(ctx context.Context, offset int) Repository[T]

Offset specifies offset

func (*GenericRepository[T]) Order

func (r *GenericRepository[T]) Order(ctx context.Context, value interface{}) Repository[T]

Order specifies ordering

func (*GenericRepository[T]) Preload

func (r *GenericRepository[T]) Preload(ctx context.Context, associations ...string) Repository[T]

Preload specifies associations to preload (returns new repository instance)

func (*GenericRepository[T]) Update

func (r *GenericRepository[T]) Update(ctx context.Context, entity *T) error

Update updates a record with relationship-aware cache invalidation

func (*GenericRepository[T]) UpdateBatch

func (r *GenericRepository[T]) UpdateBatch(ctx context.Context, entities []*T) error

UpdateBatch updates multiple records in batch with cache invalidation

func (*GenericRepository[T]) WarmCache

func (r *GenericRepository[T]) WarmCache(ctx context.Context) error

WarmCache preloads commonly accessed data

type RelatedEntity

type RelatedEntity struct {
	EntityType string      // The related entity type (table name)
	EntityID   interface{} // The related entity ID (nil for has_many without specific ID)
}

RelatedEntity represents a relationship to another entity

type RelationshipAware

type RelationshipAware interface {
	Entity

	// GetRelationships returns a map of relationship types to related entity info
	// Format: map[relationType][]RelatedEntity
	// Example: {"belongs_to": [{"customer", customerID}], "has_many": [{"orders", nil}]}
	GetRelationships() map[string][]RelatedEntity
}

RelationshipAware allows entities to define their relationships for cache invalidation When implemented, the repository can automatically invalidate related caches

type Repository

type Repository[T any] interface {
	// Queries (Read Operations - Cache-First)
	FindByID(ctx context.Context, id interface{}) (*T, error)
	FindAll(ctx context.Context) ([]T, error)
	FindWhere(ctx context.Context, query interface{}, args ...interface{}) ([]T, error)
	First(ctx context.Context, query interface{}, args ...interface{}) (*T, error)
	Count(ctx context.Context) (int64, error)
	Exists(ctx context.Context, id interface{}) (bool, error)

	// GORM Query Methods (Cached)
	Preload(ctx context.Context, associations ...string) Repository[T]
	Joins(ctx context.Context, query string, args ...interface{}) Repository[T]
	Order(ctx context.Context, value interface{}) Repository[T]
	Limit(ctx context.Context, limit int) Repository[T]
	Offset(ctx context.Context, offset int) Repository[T]

	// Commands (Write Operations - Relationship-Aware Cache Invalidation)
	Create(ctx context.Context, entity *T) error
	Update(ctx context.Context, entity *T) error
	Delete(ctx context.Context, id interface{}) error

	// Batch Operations
	CreateBatch(ctx context.Context, entities []*T) error
	UpdateBatch(ctx context.Context, entities []*T) error

	// Cache Management
	InvalidateCache(ctx context.Context) error
	WarmCache(ctx context.Context) error
}

Repository defines the generic repository interface

func NewGenericRepository

func NewGenericRepository[T Entity](dbManager *db.Manager, redisManager *redis.Manager) Repository[T]

NewGenericRepository creates a new generic repository with GORM and Redis integration

func NewGenericRepositoryDBOnly

func NewGenericRepositoryDBOnly[T Entity](manager *db.Manager) Repository[T]

NewGenericRepositoryDBOnly creates a repository without Redis (database only) For cases where caching is not needed

Jump to

Keyboard shortcuts

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