postgres

package
v0.0.0-...-7a2aee4 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrKeyDuplicate        = errors.New("duplicate key found")
	ErrForeignKeyViolation = errors.New("foreign key violation")
	ErrUniqueViolation     = errors.New("unique key violation")
	ErrRecordNotFound      = errors.New("record not found")
)

Predefined error variables for specific PostgreSQL error codes.

Functions

func FromContext

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

FromContext retrieves the *gorm.DB instance (database connection) from the context. If the database connection is not found in the context, it returns the provided defaultDB.

func IsPgxError

func IsPgxError(err error) error

IsPgxError checks if the given error is a PostgreSQL error and returns a corresponding custom error.

func NewDatabase

func NewDatabase(cfg *config.Config) (*gorm.DB, error)

NewDatabase creates and configures a new database connection using GORM.

func WithDB

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

WithDB adds a *gorm.DB instance (database connection) to the given context. This allows us to pass the context around in our application, and wherever we have the context, we can access the database connection.

Types

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is a custom logger that implements GORM's logging interface. It wraps a zap.SugaredLogger for structured logging.

func NewLogger

func NewLogger(slowThreshold time.Duration, ignoreRecordNotFoundError bool, level zapcore.Level) *Logger

NewLogger creates and returns a new Logger instance for GORM. It takes the slow SQL threshold, whether to ignore "record not found" errors, and the log level as inputs.

func (*Logger) Error

func (l *Logger) Error(ctx context.Context, msg string, args ...interface{})

Error logs error messages. It only logs if the log level is set to Error or higher.

func (*Logger) Info

func (l *Logger) Info(ctx context.Context, msg string, args ...interface{})

Info logs informational messages. It only logs if the log level is set to Info or higher.

func (*Logger) LogMode

func (l *Logger) LogMode(level glogger.LogLevel) glogger.Interface

LogMode sets the log level for the logger and returns a new logger instance with this configuration.

func (*Logger) Trace

func (l *Logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)

Trace logs SQL queries and their execution times, as well as any errors that occurred. It is used by GORM to log the details of each SQL operation.

func (*Logger) Warn

func (l *Logger) Warn(ctx context.Context, msg string, args ...interface{})

Warn logs warning messages. It only logs if the log level is set to Warn or higher.

type TransactionManager

type TransactionManager interface {
	// Begins a transaction and returns a new context with the transaction.
	Begin(ctx context.Context) (context.Context, error)
	// Commits the current transaction associated with the context.
	Commit(ctx context.Context) error
	// Rolls back the current transaction associated with the context.
	Rollback(ctx context.Context) error
}

TransactionManager defines an interface for managing database transactions. It includes methods to begin a transaction, commit it, or roll it back.

func NewTransactionManager

func NewTransactionManager(db *gorm.DB) TransactionManager

NewTransactionManager creates a new instance of transactionManagerImpl. It accepts a gorm.DB instance and returns it as a TransactionManager interface.

Jump to

Keyboard shortcuts

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