database

package
v0.0.0-...-b5c153c Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2026 License: MIT Imports: 11 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = fmt.Errorf("record not found")
)

Custom errors

Functions

This section is empty.

Types

type PostgresDB

type PostgresDB struct {
	*gorm.DB
	// contains filtered or unexported fields
}

PostgresDB wraps gorm.DB with additional functionality Note: Name kept as PostgresDB to avoid breaking changes in all services, but it now supports multiple database drivers.

func NewPostgresDB

func NewPostgresDB(cfg *config.DatabaseConfig, log *logger.Logger) (*PostgresDB, error)

NewPostgresDB creates a new database connection (PostgreSQL or SQLite)

func (*PostgresDB) BeginTx

func (p *PostgresDB) BeginTx(ctx context.Context, opts *sql.TxOptions) *gorm.DB

BeginTx starts a new transaction with the given options

func (*PostgresDB) Close

func (p *PostgresDB) Close() error

Close closes the database connection

func (*PostgresDB) GetStats

func (p *PostgresDB) GetStats() sql.DBStats

GetStats returns database connection statistics

func (*PostgresDB) HealthCheck

func (p *PostgresDB) HealthCheck(ctx context.Context) error

HealthCheck performs a health check on the database

func (*PostgresDB) Migrate

func (p *PostgresDB) Migrate(models ...interface{}) error

Migrate runs database migrations for the given models

func (*PostgresDB) Ping

func (p *PostgresDB) Ping(ctx context.Context) error

Ping checks if the database connection is alive

func (*PostgresDB) WithTransaction

func (p *PostgresDB) WithTransaction(ctx context.Context, fn func(*gorm.DB) error) error

WithTransaction executes a function within a database transaction

type Repository

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

Repository provides common database operations

func NewRepository

func NewRepository(db *PostgresDB, logger *logger.Logger) *Repository

NewRepository creates a new repository instance

func (*Repository) Create

func (r *Repository) Create(ctx context.Context, model interface{}) error

Create creates a new record

func (*Repository) Delete

func (r *Repository) Delete(ctx context.Context, model interface{}) error

Delete deletes a record

func (*Repository) GetByID

func (r *Repository) GetByID(ctx context.Context, model interface{}, id interface{}) error

GetByID retrieves a record by ID

func (*Repository) Update

func (r *Repository) Update(ctx context.Context, model interface{}) error

Update updates a record

Jump to

Keyboard shortcuts

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