db

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package db provides database connections and migrations for the Gate service. It includes a DynamoDB client singleton (see dynamo.go) and PostgreSQL/GORM connection and migration helpers (see sql.go). Both use package-level singletons protected by mutexes.

Index

Constants

View Source
const (
	// MigrateSource is the name passed to migrate.NewWithInstance for the migration source (iofs).
	MigrateSource = "iofs"
	// Postgres is the database driver name used for migrations and the embedded migration directory.
	Postgres = "postgres"
)

Variables

View Source
var (
	// ErrDynamoDBLoadConfig is returned when the AWS SDK fails to load default config.
	ErrDynamoDBLoadConfig = errors.New("failed to load AWS config")
	// ErrDynamoDBRegionConflict is returned when NewDynamoDB is called
	// with a different region after the singleton was already initialized.
	ErrDynamoDBRegionConflict = errors.New("DynamoDB singleton already initialized with a different region")
)
View Source
var (
	// ErrMigrationSource is returned when the embedded migration source cannot be read.
	ErrMigrationSource = errors.New("failed to read migration source")
	// ErrUnderlyingDB is returned when the underlying *sql.DB cannot be extracted from the GORM DB.
	ErrUnderlyingDB = errors.New("failed to extract underlying sql.DB")
	// ErrMigrateDriver is returned when the PostgreSQL migrate driver cannot be created.
	ErrMigrateDriver = errors.New("failed to create migrate driver")
)

Functions

func NewDB

func NewDB(ctx context.Context, dsn string, databaseType config.AuditBackendType) (*gorm.DB, error)

NewDB returns a singleton instance of the database connection.

func NewDBMigrator

func NewDBMigrator(ctx context.Context, dsn string, databaseType config.AuditBackendType) (*migrate.Migrate, error)

NewDBMigrator returns a migrate instance for the given database.

func NewDynamoDB

func NewDynamoDB(ctx context.Context, region string) (*dynamodb.Client, error)

NewDynamoDB returns a singleton DynamoDB client. If region is empty, the SDK resolves it from the environment/profile. Calling with a different non-empty region after initialization returns an error.

Types

type DynamoDBAPI

type DynamoDBAPI interface {
	// PutItem writes a single item to the table.
	PutItem(ctx context.Context, params *dynamodb.PutItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.PutItemOutput, error)
	// GetItem retrieves a single item by key.
	GetItem(ctx context.Context, params *dynamodb.GetItemInput, optFns ...func(*dynamodb.Options)) (*dynamodb.GetItemOutput, error)
	// Scan reads items from the table (optionally filtered).
	Scan(ctx context.Context, params *dynamodb.ScanInput, optFns ...func(*dynamodb.Options)) (*dynamodb.ScanOutput, error)
}

DynamoDBAPI defines the subset of DynamoDB operations used by the application. The real *dynamodb.Client satisfies this interface. Implementations must be safe for concurrent use.

type MockDynamoDB

type MockDynamoDB struct {
	mock.Mock
}

MockDynamoDB is a testify mock implementing DynamoDBAPI.

func (*MockDynamoDB) GetItem

func (m *MockDynamoDB) GetItem(
	ctx context.Context,
	params *dynamodb.GetItemInput,
	optFns ...func(*dynamodb.Options),
) (*dynamodb.GetItemOutput, error)

func (*MockDynamoDB) PutItem

func (m *MockDynamoDB) PutItem(
	ctx context.Context,
	params *dynamodb.PutItemInput,
	optFns ...func(*dynamodb.Options),
) (*dynamodb.PutItemOutput, error)

func (*MockDynamoDB) Scan

func (m *MockDynamoDB) Scan(
	ctx context.Context,
	params *dynamodb.ScanInput,
	optFns ...func(*dynamodb.Options),
) (*dynamodb.ScanOutput, error)

Directories

Path Synopsis
Package migrations embeds the database migration files for PostgreSQL.
Package migrations embeds the database migration files for PostgreSQL.

Jump to

Keyboard shortcuts

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