gormx

package
v3.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2025 License: MIT Imports: 32 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultContainerConfig = func() *ContainerConfig {
	return &ContainerConfig{
		Image:        "postgres:17.4-alpine3.21",
		Username:     "postgres",
		Password:     "postgres",
		DatabaseName: "postgres",
		HostPort:     "",
	}
}

DefaultContainerConfig is the default configuration for starting a PostgreSQL test container.

View Source
var DefaultContainerFailCleanupTimeout = 10 * time.Second

DefaultContainerFailCleanupTimeout is used only for abnormal cleanup when container startup fails in OpenContainer. It does not affect normal termination flows managed by the lifecycle or callers.

View Source
var DefaultCreateBatchSize = 100
View Source
var DefaultDatabaseConfig = func() *DatabaseConfig {
	def, err := confx.Read[*struct {
		Database DatabaseConfig `confx:"database"`
	}]("yaml", strings.NewReader(defaultDatabaseConfigYAML))
	if err != nil {
		panic(err)
	}
	return &def.Database
}
View Source
var OmitAssociationsPlugin gorm.Plugin = &omitAssociationsPlugin{}

OmitAssociationsPlugin automatically omits associations for Create, Update and Delete operations

View Source
var SetupDatabase = SetupDatabaseFactory("database")
View Source
var TimePrecision = time.Microsecond

Functions

func NewDefaultDialector

func NewDefaultDialector(dsn string) (gorm.Dialector, error)

func NewIAMDialector

func NewIAMDialector(dsn string, region string, credProvider aws.CredentialsProvider) (gorm.Dialector, error)

func NewTracingPlugin

func NewTracingPlugin(conf *TracingConfig) gorm.Plugin

func Open

func Open(ctx context.Context, conf *DatabaseConfig, opts ...gorm.Option) (*gorm.DB, io.Closer, error)

func ParseSchema

func ParseSchema(db *gorm.DB, model any) (*schema.Schema, error)

func SetupDatabaseFactory

func SetupDatabaseFactory(name string, opts ...gorm.Option) func(ctx context.Context, lc *lifecycle.Lifecycle, conf *DatabaseConfig) (*gorm.DB, error)

func SetupTestSuiteFactory added in v3.2.0

func SetupTestSuiteFactory(opts ...TestSuiteOption) func(ctx context.Context, lc *lifecycle.Lifecycle) (*TestSuite, error)

SetupTestSuiteFactory creates a factory function for creating test suites.

func WithMaxQueryLength added in v3.2.0

func WithMaxQueryLength(maxLength int) func(db *gorm.DB) *gorm.DB

func WithSpanName

func WithSpanName(spanName string) func(db *gorm.DB) *gorm.DB

Types

type AuthMethod

type AuthMethod string
const (
	AuthMethodPassword AuthMethod = "password"
	AuthMethodIAM      AuthMethod = "iam"
)

type Container added in v3.2.0

type Container struct {
	testcontainers.Container
	DSN string
}

Container wraps the started test container and exposes a DSN for connecting with PostgreSQL clients.

func OpenContainer added in v3.2.0

func OpenContainer(ctx context.Context, conf *ContainerConfig) (_ *Container, xerr error)

OpenContainer starts a PostgreSQL test container and returns a Container with a ready-to-use DSN. Call Terminate on the underlying container to stop and clean up resources when finished. If startup fails, the container is terminated using ContainerFailCleanupTimeout.

func SetupContainer added in v3.2.0

func SetupContainer(ctx context.Context, lc *lifecycle.Lifecycle, conf *ContainerConfig) (*Container, error)

SetupContainer starts a PostgreSQL test container and registers a lifecycle cleanup actor to terminate the container on shutdown.

func (*Container) Terminate added in v3.2.0

func (c *Container) Terminate(ctx context.Context, opts ...testcontainers.TerminateOption) error

Terminate terminates the PostgreSQL container.

type ContainerConfig added in v3.2.0

type ContainerConfig struct {
	Image        string `confx:"image" usage:"PostgreSQL image to use"`
	Username     string `confx:"username" usage:"PostgreSQL username"`
	Password     string `confx:"password" usage:"PostgreSQL password"`
	DatabaseName string `confx:"databaseName" usage:"PostgreSQL database name"`
	HostPort     string `confx:"hostPort" usage:"PostgreSQL host port"`
}

ContainerConfig defines the configuration for starting a PostgreSQL test container. If a field is left empty, a sensible default will be used.

HostPort binds the container's 5432/tcp to a specific host port when set. Leave it empty to let Docker allocate a random available port.

type DatabaseConfig

type DatabaseConfig struct {
	DSN             string        `confx:"dsn" usage:"Database connection string" validate:"required"`
	Debug           bool          `confx:"debug" usage:"Enable debug mode"`
	Tracing         TracingConfig `confx:"tracing" usage:"Tracing configuration"`
	MaxIdleConns    int           `confx:"maxIdleConns" usage:"Maximum number of idle connections" validate:"ltefield=MaxOpenConns"`
	MaxOpenConns    int           `confx:"maxOpenConns" usage:"Maximum number of open connections"`
	ConnMaxLifetime time.Duration `confx:"connMaxLifetime" usage:"Maximum connection lifetime"`
	ConnMaxIdleTime time.Duration `confx:"connMaxIdleTime" usage:"Maximum idle time for connections" validate:"ltefield=ConnMaxLifetime"`
	AuthMethod      AuthMethod    `confx:"authMethod" usage:"Authentication method: 'password' or 'iam'" validate:"required,oneof=password iam"`
}

type Model

type Model struct {
	ID        string         `gorm:"size:36;primaryKey" json:"id"`
	CreatedAt time.Time      `gorm:"not null" json:"createdAt"`
	UpdatedAt time.Time      `gorm:"not null" json:"updatedAt"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"`
}

func (*Model) BeforeCreate

func (m *Model) BeforeCreate(_ *gorm.DB) error

type TestSuite added in v3.2.0

type TestSuite struct {
	*lifecycle.Lifecycle
	*Container
	// contains filtered or unexported fields
}

TestSuite provides a test environment with a PostgreSQL container, database connection, and lifecycle management for integration tests.

func MustStartTestSuite added in v3.2.0

func MustStartTestSuite(ctx context.Context, opts ...TestSuiteOption) *TestSuite

MustStartTestSuite creates and starts a new test suite, panicking on error. This is a convenience wrapper around StartTestSuite for test code that prefers panics.

func StartTestSuite added in v3.2.0

func StartTestSuite(ctx context.Context, opts ...TestSuiteOption) (*TestSuite, error)

StartTestSuite creates and starts a new test suite with PostgreSQL container. Configuration can be customized using TestSuiteOption functions.

func (*TestSuite) DB added in v3.2.0

func (s *TestSuite) DB() *gorm.DB

DB returns the GORM database connection for the test suite.

func (*TestSuite) DSN added in v3.2.0

func (s *TestSuite) DSN() string

DSN returns the database connection string for the test suite.

func (*TestSuite) GetName added in v3.2.0

func (s *TestSuite) GetName() string

GetName returns the name of the test suite.

func (*TestSuite) ResetDB added in v3.2.0

func (s *TestSuite) ResetDB(ctx context.Context, models ...any) error

ResetDB drops and recreates all tables for the provided models. This is useful for cleaning up test data between test runs.

func (*TestSuite) Stop added in v3.2.0

func (s *TestSuite) Stop(ctx context.Context) error

Stop stops the test suite.

type TestSuiteOption added in v3.2.0

type TestSuiteOption func(*testSuiteOptions)

TestSuiteOption configures TestSuite creation

func WithContainerConfig added in v3.2.0

func WithContainerConfig(config *ContainerConfig) TestSuiteOption

WithContainerConfig sets a custom container configuration

func WithProviders added in v3.2.0

func WithProviders(providers ...any) TestSuiteOption

WithProviders adds additional dependency injection providers

type TracingConfig

type TracingConfig struct {
	ExcludeQuery     bool                      `confx:"excludeQuery" usage:"Exclude query"`
	ExcludeQueryVars bool                      `confx:"excludeQueryVars" usage:"Exclude query vars"`
	MaxQueryLength   int                       `confx:"maxQueryLength" usage:"Maximum query length for tracing, 0 uses default (4096)"`
	QueryFormatter   func(query string) string `confx:"-" json:"-"`
	Logger           *kitlog.Logger            `confx:"-" json:"-" inject:"optional"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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