Documentation
¶
Overview ¶
Package core provides core functionalities for database repository implementations. This package includes base implementations for repositories that can be extended to fit specific use cases. It supports transaction management and context-based database operations.
Package core provides core functionalities for database repository implementations. This package includes base implementations for repositories that can be extended to fit specific use cases. It supports transaction management and context-based database operations.
ZeroLogAdapter is a custom logger adapter for GORM that uses zerolog for logging.
Index ¶
- func DatabaseNow() time.Time
- func DatabaseNowPointer() *time.Time
- func FromContext(ctx context.Context) (*gorm.DB, bool)
- func NewContext(ctx context.Context, db *gorm.DB) context.Context
- func NewDriver(dialector gorm.Dialector) (*gorm.DB, error)
- func NewID() string
- func TranslateError(err error) error
- type BaseRepoImpl
- type RawBaseRepoImpl
- type ZeroLogAdapter
- func (l ZeroLogAdapter) Error(ctx context.Context, msg string, opts ...interface{})
- func (l ZeroLogAdapter) Info(ctx context.Context, msg string, opts ...interface{})
- func (l ZeroLogAdapter) LogMode(logger.LogLevel) logger.Interface
- func (l ZeroLogAdapter) Trace(ctx context.Context, begin time.Time, f func() (string, int64), err error)
- func (l ZeroLogAdapter) Warn(ctx context.Context, msg string, opts ...interface{})
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DatabaseNow ¶
DatabaseNow returns time.Now() in UTC, truncated to Milliseconds.
func DatabaseNowPointer ¶
DatabaseNowPointer returns a pointer to a time.Time created by DatabaseNow
func FromContext ¶
FromContext returns the gorm.DB value stored in ctx, if any.
func NewContext ¶
NewContext returns a new Context that carries value db.
func NewID ¶
func NewID() string
NewID generates a new unique identifier string using UUID version 4. It returns the UUID as a string.
func TranslateError ¶
TranslateError maps GORM errors to application-specific errors. If the error does not match any known GORM errors, it returns the original error.
Types ¶
type BaseRepoImpl ¶
type BaseRepoImpl struct {
RawBaseRepoImpl
// contains filtered or unexported fields
}
BaseRepoImpl adds core behaviors applicable to any database repository implementation. Repositories should be defined as follows:
type MyAwesomeRepoImpl struct {
core.BaseRepoImpl
}
And constructed as follows:
func NewMyAwesomeRepoImpl(db *gorm.DB) *MyAwesomeRepoImpl {
return &MyAwesomeRepoImpl{BaseRepoImpl:
core.NewBaseRepoImpl(db, &MyAwesomeModel)}
}
Any database operations should be invoked using the DB() function:
r.DB(ctx).Where(...)
Which ensures the proper *gorm.DB instance is used (this enables transaction support).
func NewBaseRepoImpl ¶
func NewBaseRepoImpl(db *gorm.DB, model interface{}) BaseRepoImpl
NewBaseRepoImpl creates a new BaseRepoImpl for use in a concrete instance of a repository, as shown above. The `model` parameter is used to specify the struct that this repository is associated with. It is used, for example, in the Count() function.
type RawBaseRepoImpl ¶
type RawBaseRepoImpl struct {
// contains filtered or unexported fields
}
RawBaseRepoImpl adds core behaviors applicable to any database repository implementation and does not assume a simple table model.
func NewRawBaseRepoImpl ¶
func NewRawBaseRepoImpl(db *gorm.DB) RawBaseRepoImpl
NewRawBaseRepoImpl creates a new RawBaseRepoImpl for use in a concrete instance of a repository.
func (*RawBaseRepoImpl) DB ¶
func (b *RawBaseRepoImpl) DB(ctx context.Context) *gorm.DB
DB returns a *gorm.DB for use in any database calls. It first looks for any *gorm.DB in the context, which allows ongoing transactions to be used. Otherwise, it uses the default *gorm.DB passed into NewRawBaseRepoImpl. In both cases, the found *gorm.DB is augmented using WithContext(ctx).
func (*RawBaseRepoImpl) Tx ¶
Tx starts a new database transaction and saves it in a new context, ctxTx. This context is passed into the block function. Any calls to repository methods that take this context and are built on this RawBaseRepoImpl will operate in the context of this transaction. If the block returns an error, the transaction is rolled back. If no error is returned, the transaction is committed. Note: this mechanism allows for nesting of transactions.
type ZeroLogAdapter ¶
type ZeroLogAdapter struct{}
func (ZeroLogAdapter) Error ¶
func (l ZeroLogAdapter) Error(ctx context.Context, msg string, opts ...interface{})
func (ZeroLogAdapter) Info ¶
func (l ZeroLogAdapter) Info(ctx context.Context, msg string, opts ...interface{})
func (ZeroLogAdapter) Trace ¶
func (l ZeroLogAdapter) Trace(ctx context.Context, begin time.Time, f func() (string, int64), err error)
Trace logs the execution of a SQL query using zerolog. It logs the duration of the query execution and additional information such as the SQL statement and the number of rows affected.
Parameters:
- ctx: The context for the logging operation.
- begin: The time when the query execution started.
- f: A function that returns the SQL statement and the number of rows affected.
- err: An error that occurred during the query execution, if any.