core

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: May 2, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func DatabaseNow

func DatabaseNow() time.Time

DatabaseNow returns time.Now() in UTC, truncated to Milliseconds.

func DatabaseNowPointer

func DatabaseNowPointer() *time.Time

DatabaseNowPointer returns a pointer to a time.Time created by DatabaseNow

func FromContext

func FromContext(ctx context.Context) (*gorm.DB, bool)

FromContext returns the gorm.DB value stored in ctx, if any.

func NewContext

func NewContext(ctx context.Context, db *gorm.DB) context.Context

NewContext returns a new Context that carries value db.

func NewDriver

func NewDriver(dialector gorm.Dialector) (*gorm.DB, error)

NewDriver creates a standard *gorm.DB for the database dialect passed in.

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

func TranslateError(err error) error

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.

func (*BaseRepoImpl) Count

func (b *BaseRepoImpl) Count(ctx context.Context) (int, error)

Count returns the number of rows in the table.

func (*BaseRepoImpl) DeleteAll

func (b *BaseRepoImpl) DeleteAll(ctx context.Context) error

DeleteAll deletes all rows in the table (useful in testing).

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

func (b *RawBaseRepoImpl) Tx(ctx context.Context, block func(ctxTx context.Context) error) error

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) LogMode

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.

func (ZeroLogAdapter) Warn

func (l ZeroLogAdapter) Warn(ctx context.Context, msg string, opts ...interface{})

Jump to

Keyboard shortcuts

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