example_db

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2025 License: MIT Imports: 8 Imported by: 0

README

Database Package Examples

This directory contains examples demonstrating how to use the db package, which provides utilities for working with databases in Go applications. The package offers support for different database types, connection management, health checks, querying, transactions, and more.

Examples

1. Connection Example

connection_example.go

Demonstrates how to connect to different types of databases.

Key concepts:

  • Connecting to PostgreSQL using InitPostgresPool
  • Connecting to SQLite using InitSQLiteDB with connection pool settings
  • Connecting to MongoDB using InitMongoClient
  • Using custom timeouts for database connections
2. Health Check Example

health_check_example.go

Shows how to perform health checks on database connections.

Key concepts:

  • Checking PostgreSQL health using CheckPostgresHealth
  • Checking SQLite health using CheckSQLiteHealth
  • Checking MongoDB health using CheckMongoHealth
  • Setting up periodic health checks using a ticker
  • Performing health checks with a custom timeout
3. Query Example

query_example.go

Demonstrates how to execute queries with different database types.

Key concepts:

  • Executing basic queries with PostgreSQL
  • Executing queries with SQLite, showing the difference in parameter placeholders
  • Querying a single row using QueryRowContext
  • Using parameterized queries to prevent SQL injection
  • Handling NULL values from the database using sql.Null* types
4. Repository Example

repository_example.go

Shows how to implement the repository pattern with the db package.

Key concepts:

  • Defining a repository interface
  • Implementing the repository for SQL databases
  • Implementing the repository for MongoDB
  • Using the repository to perform CRUD operations
  • Benefits of using the repository pattern
5. Retry Example

retry_example.go

Demonstrates how to execute database queries with retries.

Key concepts:

  • Executing a basic query with retries using QueryWithRetries
  • Customizing retry behavior with different retry options
  • Retrying transactions using TransactionWithRetries
  • Handling specific errors with custom retry conditions
  • Using exponential backoff with jitter to prevent thundering herd problems
6. Transaction Example

transaction_example.go

Shows how to use transactions with different database types.

Key concepts:

  • Executing a transaction with PostgreSQL using ExecutePostgresTransaction
  • Executing a transaction with SQLite using ExecuteSQLTransaction
  • Handling errors within transactions
  • Information about nested transactions and savepoints
  • Information about transaction isolation levels

Running the Examples

To run any of the examples, use the go run command:

go run examples/db/connection_example.go

Additional Resources

For more information about the db package, see the db package documentation.

Documentation

Overview

Example of connecting to different database types

Example of checking database health

Example of executing queries with different database types

Example of implementing the repository pattern with the db package

Example of executing queries with retries

Example of using transactions with different database types

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MongoUserRepository

type MongoUserRepository struct {
}

MongoUserRepository implements UserRepository for MongoDB

func NewMongoUserRepository

func NewMongoUserRepository() *MongoUserRepository

NewMongoUserRepository creates a new MongoUserRepository

func (*MongoUserRepository) GetByID

func (r *MongoUserRepository) GetByID(ctx context.Context, id string) (*User, error)

GetByID retrieves a user by ID from MongoDB

type Product

type Product struct {
	ID    string
	Name  string
	Price float64
}

Product represents a product in the system

type SQLUserRepository

type SQLUserRepository struct {
}

SQLUserRepository implements UserRepository for SQL databases

func NewSQLUserRepository

func NewSQLUserRepository() *SQLUserRepository

NewSQLUserRepository creates a new SQLUserRepository

func (*SQLUserRepository) Create

func (r *SQLUserRepository) Create(ctx context.Context, user *User) error

Create inserts a new user

func (*SQLUserRepository) Delete

func (r *SQLUserRepository) Delete(ctx context.Context, id string) error

Delete removes a user by ID

func (*SQLUserRepository) FindActive

func (r *SQLUserRepository) FindActive(ctx context.Context) ([]User, error)

FindActive finds all active users

func (*SQLUserRepository) GetByID

func (r *SQLUserRepository) GetByID(ctx context.Context, id string) (*User, error)

GetByID retrieves a user by ID

func (*SQLUserRepository) Update

func (r *SQLUserRepository) Update(ctx context.Context, user *User) error

Update updates an existing user

type User

type User struct {
	ID       string
	Username string
	Email    string
	Active   bool
}

User represents a user in the system

type UserEntity

type UserEntity struct {
	ID       string
	Username string
	Email    string
	Active   bool
}

UserEntity represents a user in the system

type UserRepository

type UserRepository interface {
	GetByID(ctx context.Context, id string) (*User, error)
	Create(ctx context.Context, user *User) error
	Update(ctx context.Context, user *User) error
	Delete(ctx context.Context, id string) error
	FindActive(ctx context.Context) ([]User, error)
}

UserRepository defines the interface for user data access

Jump to

Keyboard shortcuts

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