datafx

package
v0.6.27 Latest Latest
Warning

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

Go to latest
Published: May 22, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

README

ajan/datafx

Overview

The datafx package is a flexible database access package that provides a unified interface for different SQL database backends. Currently, it supports PostgreSQL, MySQL, and SQLite as database backends.

The documentation below provides an overview of the package, its types, functions, and usage examples. For more detailed information, refer to the source code and tests.

Configuration

Configuration struct for the database:

type Config struct {
  Sources map[string]ConfigDatasource `conf:"SOURCES"`
}

type ConfigDatasource struct {
  Provider string `conf:"PROVIDER"`
  DSN      string `conf:"DSN"`
}

Example configuration:

config := &datafx.Config{
  Sources: map[string]datafx.ConfigDatasource{
    "default": {
      Provider: "postgres",
      DSN:      "postgres://user:pass@localhost:5432/dbname",
    },
    "readonly": {
      Provider: "mysql",
      DSN:      "mysql://user:pass@localhost:3306/dbname",
    },
  },
}

Features

  • Multiple SQL database backend support (PostgreSQL, MySQL, SQLite)
  • Configurable database dialects
  • Managing multiple database instances
  • Unit of Work pattern for transaction management
  • Easy to extend for additional database backends

API

Usage
import "github.com/eser/ajan/datafx"

// Create a new PostgreSQL database instance
db, err := datafx.NewSqlDatasource(ctx, datafx.DialectPostgres, "postgres://localhost:5432/mydb")
if err != nil {
  log.Fatal(err)
}

// Get the database connection
conn := db.GetConnection()

// Use Unit of Work for transaction management
uow, err := db.CreateUnitOfWork(ctx)
if err != nil {
  log.Fatal(err)
}

// Perform operations within transaction
err = uow.Execute(func(tx *sql.Tx) error {
  // Your database operations here
  return nil
})
Supported Dialects
  • PostgreSQL (postgres://)
  • MySQL (mysql://)
  • SQLite (sqlite://)
Registry

The package includes a registry pattern for managing multiple database instances. This allows you to register and retrieve database connections by name:

// Register a database instance
err := datafx.Register("main", db)

// Get a registered database instance
db, err := datafx.Get("main")

Documentation

Index

Constants

View Source
const DefaultDatasource = "default"

Variables

View Source
var (
	ErrUnknownProvider          = errors.New("unknown provider")
	ErrUnableToDetermineDialect = errors.New("unable to determine dialect")
)
View Source
var (
	ErrNoTransaction             = errors.New("no transaction in progress")
	ErrTransactionAlreadyStarted = errors.New("transaction already started")
	ErrTransactionBeginFailed    = errors.New("transaction begin failed")
	ErrTransactionCommitFailed   = errors.New("transaction commit failed")
	ErrTransactionRollbackFailed = errors.New("transaction rollback failed")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Sources map[string]ConfigDatasource `conf:"SOURCES"`
}

type ConfigDatasource added in v0.6.7

type ConfigDatasource struct {
	Provider string `conf:"PROVIDER"`
	DSN      string `conf:"DSN"`
}

type ContextKey

type ContextKey string
const (
	ContextKeyUnitOfWork ContextKey = "unit-of-work"
)

type Datasource added in v0.6.9

type Datasource interface {
	GetDialect() Dialect
	GetConnection() SqlExecutor
	ExecuteUnitOfWork(ctx context.Context, fn func(uow *UnitOfWork) error) error
}

type Dialect

type Dialect string
const (
	// DialectPostgresPgx Dialect = "pgx".
	DialectPostgres Dialect = "postgres"
	DialectSQLite   Dialect = "sqlite"
	DialectMySQL    Dialect = "mysql"
)

func DetermineDialect

func DetermineDialect(provider string, dsn string) (Dialect, error)

type Registry

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

func NewRegistry

func NewRegistry(logger *logfx.Logger) *Registry

func (*Registry) AddConnection

func (registry *Registry) AddConnection(ctx context.Context, name string, provider string, dsn string) error

func (*Registry) GetDefault added in v0.6.9

func (registry *Registry) GetDefault() Datasource

func (*Registry) GetNamed added in v0.6.9

func (registry *Registry) GetNamed(name string) Datasource

func (*Registry) LoadFromConfig

func (registry *Registry) LoadFromConfig(ctx context.Context, config *Config) error

type SqlDatasource added in v0.6.7

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

func NewSqlDatasource added in v0.6.9

func NewSqlDatasource(ctx context.Context, dialect Dialect, dsn string) (*SqlDatasource, error)

func (*SqlDatasource) ExecuteUnitOfWork added in v0.6.21

func (datasource *SqlDatasource) ExecuteUnitOfWork(ctx context.Context, fn func(uow *UnitOfWork) error) error

func (*SqlDatasource) GetConnection added in v0.6.7

func (datasource *SqlDatasource) GetConnection() SqlExecutor

func (*SqlDatasource) GetDialect added in v0.6.7

func (datasource *SqlDatasource) GetDialect() Dialect

type SqlExecutor

type SqlExecutor interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

type TransactionController added in v0.6.21

type TransactionController interface {
	Rollback() error
	Commit() error
}

type TransactionStarter

type TransactionStarter interface {
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
}

type UnitOfWork

type UnitOfWork struct {
	Context context.Context //nolint:containedctx

	TransactionStarter    TransactionStarter
	TransactionController TransactionController
}

func CurrentUnitOfWork added in v0.6.21

func CurrentUnitOfWork(ctx context.Context) *UnitOfWork

func NewUnitOfWork added in v0.6.18

func NewUnitOfWork(transactionStarter TransactionStarter) *UnitOfWork

func (*UnitOfWork) Begin added in v0.6.21

func (uow *UnitOfWork) Begin(ctx context.Context) error

func (*UnitOfWork) Commit

func (uow *UnitOfWork) Commit() error

func (*UnitOfWork) Execute added in v0.6.21

func (uow *UnitOfWork) Execute(ctx context.Context, fn func(uow *UnitOfWork) error) error

func (*UnitOfWork) Rollback added in v0.6.21

func (uow *UnitOfWork) Rollback() error

Jump to

Keyboard shortcuts

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