Documentation ¶
Index ¶
- Variables
- func FromContext(ctx context.Context, defaultDB *gorm.DB) *gorm.DB
- func IsPgxError(err error) error
- func NewDatabase(cfg *config.Config) (*gorm.DB, error)
- func WithDB(ctx context.Context, db *gorm.DB) context.Context
- type Logger
- func (l *Logger) Error(ctx context.Context, msg string, args ...interface{})
- func (l *Logger) Info(ctx context.Context, msg string, args ...interface{})
- func (l *Logger) LogMode(level glogger.LogLevel) glogger.Interface
- func (l *Logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)
- func (l *Logger) Warn(ctx context.Context, msg string, args ...interface{})
- type TransactionManager
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
IsPgxError checks if the given error is a PostgreSQL error and returns a corresponding custom error.
func NewDatabase ¶
NewDatabase creates and configures a new database connection using GORM.
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 ¶
Error logs error messages. It only logs if the log level is set to Error or higher.
func (*Logger) Info ¶
Info logs informational messages. It only logs if the log level is set to Info or higher.
func (*Logger) LogMode ¶
LogMode sets the log level for the logger and returns a new logger instance with this configuration.
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.