Documentation
¶
Overview ¶
Package crud is a generated GoMock package.
Package crud is a generated GoMock package.
Index ¶
- func BetweenTime(col string, start, end time.Time) func(db *gorm.DB) *gorm.DB
- func DefaultOrder() func(*gorm.DB) *gorm.DB
- func ForUpdate(enable bool) func(*gorm.DB) *gorm.DB
- func GetTxFromContext(ctx context.Context) (*gorm.DB, error)
- func OrderBy(field string, ascending bool) func(db *gorm.DB) *gorm.DB
- func Paginate(page, limit int) func(db *gorm.DB) *gorm.DB
- func PreloadRelations(relations []string) func(db *gorm.DB) *gorm.DB
- func WhereBySpec[T any](spec T) func(db *gorm.DB) *gorm.DB
- type BaseEntity
- type MockRepository
- func (m *MockRepository[T]) Delete(ctx context.Context, model T) error
- func (m *MockRepository[T]) DeleteMany(ctx context.Context, models []T) error
- func (m *MockRepository[T]) EXPECT() *MockRepositoryMockRecorder[T]
- func (m *MockRepository[T]) FindAll(ctx context.Context, spec Specification[T]) ([]T, error)
- func (m *MockRepository[T]) FindFirst(ctx context.Context, spec Specification[T]) (T, error)
- func (m *MockRepository[T]) GetGormInstance(ctx context.Context) (*gorm.DB, error)
- func (m *MockRepository[T]) Insert(ctx context.Context, model T) (T, error)
- func (m *MockRepository[T]) InsertMany(ctx context.Context, models []T) ([]T, error)
- func (m *MockRepository[T]) SaveMany(ctx context.Context, models []T) ([]T, error)
- func (m *MockRepository[T]) Update(ctx context.Context, model T) (T, error)
- type MockRepositoryMockRecorder
- func (mr *MockRepositoryMockRecorder[T]) Delete(ctx, model any) *gomock.Call
- func (mr *MockRepositoryMockRecorder[T]) DeleteMany(ctx, models any) *gomock.Call
- func (mr *MockRepositoryMockRecorder[T]) FindAll(ctx, spec any) *gomock.Call
- func (mr *MockRepositoryMockRecorder[T]) FindFirst(ctx, spec any) *gomock.Call
- func (mr *MockRepositoryMockRecorder[T]) GetGormInstance(ctx any) *gomock.Call
- func (mr *MockRepositoryMockRecorder[T]) Insert(ctx, model any) *gomock.Call
- func (mr *MockRepositoryMockRecorder[T]) InsertMany(ctx, models any) *gomock.Call
- func (mr *MockRepositoryMockRecorder[T]) SaveMany(ctx, models any) *gomock.Call
- func (mr *MockRepositoryMockRecorder[T]) Update(ctx, model any) *gomock.Call
- type MockTransactor
- func (m *MockTransactor) Begin(ctx context.Context) (context.Context, error)
- func (m *MockTransactor) Commit(ctx context.Context) error
- func (m *MockTransactor) EXPECT() *MockTransactorMockRecorder
- func (m *MockTransactor) Rollback(ctx context.Context)
- func (m *MockTransactor) WithinTransaction(ctx context.Context, serviceFn func(context.Context) error) error
- type MockTransactorMockRecorder
- type Repository
- type Specification
- type Transactor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BetweenTime ¶
BetweenTime returns a GORM scope that filters records between two time values. It uses GetTimeRangeClause to generate the appropriate SQL WHERE clause. Handles open-ended ranges when either start or end time is zero.
func DefaultOrder ¶
DefaultOrder returns a GORM scope that applies default ordering by created_at DESC. This provides consistent ordering for queries that don't specify explicit ordering. Assumes the model has a created_at field.
func ForUpdate ¶
ForUpdate returns a GORM scope that conditionally adds FOR UPDATE locking to queries. When enable is true, it adds SELECT ... FOR UPDATE to prevent concurrent modifications. Used for pessimistic locking in transaction-critical operations.
func GetTxFromContext ¶
GetTxFromContext retrieves the current GORM transaction from the context. Returns an error if no transaction is found or if the stored value is not a *gorm.DB.
func OrderBy ¶
OrderBy returns a GORM scope that orders query results by the specified field. It uses internal.IsValidFieldName to validate the field name and prevent SQL injection. Set ascending to true for ascending order, false for descending.
func Paginate ¶
Paginate returns a GORM scope that applies pagination to a query. It calculates the appropriate offset based on the page number and limit. The page parameter is 1-indexed (minimum value of 1).
func PreloadRelations ¶
PreloadRelations returns a GORM scope that preloads the specified relations. It eager loads related data to avoid N+1 query problems.
Types ¶
type BaseEntity ¶ added in v0.2.0
type BaseEntity struct {
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:uuidv7()"`
CreatedAt time.Time
UpdatedAt time.Time `gorm:"autoUpdateTime"`
}
func (BaseEntity) IsZero ¶ added in v0.2.0
func (be BaseEntity) IsZero() bool
type MockRepository ¶ added in v1.1.0
type MockRepository[T any] struct { // contains filtered or unexported fields }
MockRepository is a mock of Repository interface.
func NewMockRepository ¶ added in v1.1.0
func NewMockRepository[T any](ctrl *gomock.Controller) *MockRepository[T]
NewMockRepository creates a new mock instance.
func (*MockRepository[T]) Delete ¶ added in v1.1.0
func (m *MockRepository[T]) Delete(ctx context.Context, model T) error
Delete mocks base method.
func (*MockRepository[T]) DeleteMany ¶ added in v1.1.0
func (m *MockRepository[T]) DeleteMany(ctx context.Context, models []T) error
DeleteMany mocks base method.
func (*MockRepository[T]) EXPECT ¶ added in v1.1.0
func (m *MockRepository[T]) EXPECT() *MockRepositoryMockRecorder[T]
EXPECT returns an object that allows the caller to indicate expected use.
func (*MockRepository[T]) FindAll ¶ added in v1.1.0
func (m *MockRepository[T]) FindAll(ctx context.Context, spec Specification[T]) ([]T, error)
FindAll mocks base method.
func (*MockRepository[T]) FindFirst ¶ added in v1.1.0
func (m *MockRepository[T]) FindFirst(ctx context.Context, spec Specification[T]) (T, error)
FindFirst mocks base method.
func (*MockRepository[T]) GetGormInstance ¶ added in v1.1.0
GetGormInstance mocks base method.
func (*MockRepository[T]) Insert ¶ added in v1.1.0
func (m *MockRepository[T]) Insert(ctx context.Context, model T) (T, error)
Insert mocks base method.
func (*MockRepository[T]) InsertMany ¶ added in v1.1.0
func (m *MockRepository[T]) InsertMany(ctx context.Context, models []T) ([]T, error)
InsertMany mocks base method.
type MockRepositoryMockRecorder ¶ added in v1.1.0
type MockRepositoryMockRecorder[T any] struct { // contains filtered or unexported fields }
MockRepositoryMockRecorder is the mock recorder for MockRepository.
func (*MockRepositoryMockRecorder[T]) Delete ¶ added in v1.1.0
func (mr *MockRepositoryMockRecorder[T]) Delete(ctx, model any) *gomock.Call
Delete indicates an expected call of Delete.
func (*MockRepositoryMockRecorder[T]) DeleteMany ¶ added in v1.1.0
func (mr *MockRepositoryMockRecorder[T]) DeleteMany(ctx, models any) *gomock.Call
DeleteMany indicates an expected call of DeleteMany.
func (*MockRepositoryMockRecorder[T]) FindAll ¶ added in v1.1.0
func (mr *MockRepositoryMockRecorder[T]) FindAll(ctx, spec any) *gomock.Call
FindAll indicates an expected call of FindAll.
func (*MockRepositoryMockRecorder[T]) FindFirst ¶ added in v1.1.0
func (mr *MockRepositoryMockRecorder[T]) FindFirst(ctx, spec any) *gomock.Call
FindFirst indicates an expected call of FindFirst.
func (*MockRepositoryMockRecorder[T]) GetGormInstance ¶ added in v1.1.0
func (mr *MockRepositoryMockRecorder[T]) GetGormInstance(ctx any) *gomock.Call
GetGormInstance indicates an expected call of GetGormInstance.
func (*MockRepositoryMockRecorder[T]) Insert ¶ added in v1.1.0
func (mr *MockRepositoryMockRecorder[T]) Insert(ctx, model any) *gomock.Call
Insert indicates an expected call of Insert.
func (*MockRepositoryMockRecorder[T]) InsertMany ¶ added in v1.1.0
func (mr *MockRepositoryMockRecorder[T]) InsertMany(ctx, models any) *gomock.Call
InsertMany indicates an expected call of InsertMany.
type MockTransactor ¶ added in v1.1.0
type MockTransactor struct {
// contains filtered or unexported fields
}
MockTransactor is a mock of Transactor interface.
func NewMockTransactor ¶ added in v1.1.0
func NewMockTransactor(ctrl *gomock.Controller) *MockTransactor
NewMockTransactor creates a new mock instance.
func (*MockTransactor) Commit ¶ added in v1.1.0
func (m *MockTransactor) Commit(ctx context.Context) error
Commit mocks base method.
func (*MockTransactor) EXPECT ¶ added in v1.1.0
func (m *MockTransactor) EXPECT() *MockTransactorMockRecorder
EXPECT returns an object that allows the caller to indicate expected use.
func (*MockTransactor) Rollback ¶ added in v1.1.0
func (m *MockTransactor) Rollback(ctx context.Context)
Rollback mocks base method.
func (*MockTransactor) WithinTransaction ¶ added in v1.1.0
func (m *MockTransactor) WithinTransaction(ctx context.Context, serviceFn func(context.Context) error) error
WithinTransaction mocks base method.
type MockTransactorMockRecorder ¶ added in v1.1.0
type MockTransactorMockRecorder struct {
// contains filtered or unexported fields
}
MockTransactorMockRecorder is the mock recorder for MockTransactor.
func (*MockTransactorMockRecorder) Begin ¶ added in v1.1.0
func (mr *MockTransactorMockRecorder) Begin(ctx any) *gomock.Call
Begin indicates an expected call of Begin.
func (*MockTransactorMockRecorder) Commit ¶ added in v1.1.0
func (mr *MockTransactorMockRecorder) Commit(ctx any) *gomock.Call
Commit indicates an expected call of Commit.
func (*MockTransactorMockRecorder) Rollback ¶ added in v1.1.0
func (mr *MockTransactorMockRecorder) Rollback(ctx any) *gomock.Call
Rollback indicates an expected call of Rollback.
func (*MockTransactorMockRecorder) WithinTransaction ¶ added in v1.1.0
func (mr *MockTransactorMockRecorder) WithinTransaction(ctx, serviceFn any) *gomock.Call
WithinTransaction indicates an expected call of WithinTransaction.
type Repository ¶ added in v1.0.0
type Repository[T any] interface { // Insert creates a new record in the database. Insert(ctx context.Context, model T) (T, error) // FindAll retrieves multiple records based on the specification. FindAll(ctx context.Context, spec Specification[T]) ([]T, error) // FindFirst retrieves the first record matching the specification. FindFirst(ctx context.Context, spec Specification[T]) (T, error) // Update modifies an existing record in the database. Update(ctx context.Context, model T) (T, error) // Delete removes a record from the database (hard delete). Delete(ctx context.Context, model T) error // InsertMany creates multiple records in a single database operation. InsertMany(ctx context.Context, models []T) ([]T, error) // DeleteMany removes multiple records in a single database operation (hard delete). DeleteMany(ctx context.Context, models []T) error // SaveMany saves multiple records in a single database operation. SaveMany(ctx context.Context, models []T) ([]T, error) // GetGormInstance returns the appropriate GORM DB instance (transaction-aware). GetGormInstance(ctx context.Context) (*gorm.DB, error) }
Repository defines a generic interface for basic CRUD operations on entities of type T. It provides standard database operations with context support and transaction awareness. The interface abstracts the underlying database implementation for easier testing and flexibility.
func NewRepository ¶ added in v1.0.0
func NewRepository[T any](db *gorm.DB) Repository[T]
NewRepository creates a new CRUD repository implementation using GORM. The repository provides transaction-aware database operations for the specified entity type T.
type Specification ¶
type Specification[T any] struct { Model T // Model with fields set for WHERE conditions PreloadRelations []string // Relations to eager load ForUpdate bool // Whether to use SELECT ... FOR UPDATE }
Specification defines query parameters for database operations. It includes the model for WHERE conditions, relations to preload, and locking options.
type Transactor ¶
type Transactor interface {
// Begin starts a new database transaction and returns a context containing the transaction.
Begin(ctx context.Context) (context.Context, error)
// Commit commits the current transaction in the context.
Commit(ctx context.Context) error
// Rollback rolls back the current transaction in the context without returning an error.
Rollback(ctx context.Context)
// WithinTransaction executes a service function within a database transaction.
WithinTransaction(ctx context.Context, serviceFn func(ctx context.Context) error) error
}
Transactor provides an interface for managing database transactions with context. It abstracts transaction operations to allow for easier testing and different implementations.
func NewTransactor ¶
func NewTransactor(db *gorm.DB) Transactor
NewTransactor creates a new Transactor implementation using GORM. The returned Transactor can be used to manage database transactions with context propagation.