Documentation
¶
Index ¶
- func CreateTransaction(ctx context.Context, pool *pgxpool.Pool, fn TransactionFn) error
- type Config
- type DefaultHandler
- type DefaultService
- func (d *DefaultService) ClearStatTicker()
- func (d *DefaultService) CreateTransaction(ctx context.Context, fn TransactionFn) error
- func (d *DefaultService) Exec(query *string, params ...any) (*pgconn.CommandTag, error)
- func (d *DefaultService) ExecWithCtx(ctx context.Context, query *string, params ...any) (*pgconn.CommandTag, error)
- func (d *DefaultService) Query(query *string, params ...any) (pgx.Rows, error)
- func (d *DefaultService) QueryRow(query *string, params ...any) (pgx.Row, error)
- func (d *DefaultService) QueryRowWithCtx(ctx context.Context, query *string, params ...any) (pgx.Row, error)
- func (d *DefaultService) QueryWithCtx(ctx context.Context, query *string, params ...any) (pgx.Rows, error)
- func (d *DefaultService) ScanRow(row pgx.Row, destinations ...any) error
- func (d *DefaultService) SetStatTicker(ctx context.Context, duration time.Duration, fn func(*pgxpool.Stat))
- type Handler
- type Service
- type TransactionFn
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateTransaction ¶
CreateTransaction creates a transaction for the database with context
Parameters:
- ctx: The context for the transaction
- pool: The pgxpool.Pool instance
- fn: The function to execute within the transaction
Returns:
- error: An error if the transaction fails, otherwise nil
Types ¶
type Config ¶
type Config struct {
DataSourceName string
MaxOpenConnections int
MaxIdleConnections int
ConnectionMaxLifetime time.Duration
ConnectionMaxIdleTime time.Duration
HealthCheckPeriod time.Duration
ConnectionMaxLifetimeJitter time.Duration
}
Config struct
func NewConfig ¶ added in v0.7.1
func NewConfig( dataSourceName string, maxOpenConnections, maxIdleConnections int, connectionMaxIdleTime, connectionMaxLifetime, healthCheckPeriod, connectionMaxLifetimeJitter time.Duration, ) (*Config, error)
NewConfig creates a new pool configuration
Parameters:
- dataSourceName: the data source name
- maxOpenConnections: the maximum number of open connections
- maxIdleConnections: the maximum number of idle connections
- connectionMaxIdleTime: the maximum idle time for a connection
- connectionMaxLifetime: the maximum lifetime for a connection
- healthCheckPeriod: the period for health checks
- connectionMaxLifetimeJitter: the jitter for the maximum lifetime of a connection
Returns:
- *Config: the pool configuration
- error: if any error occurs
type DefaultHandler ¶ added in v0.7.1
type DefaultHandler struct {
// contains filtered or unexported fields
}
DefaultHandler struct
func NewDefaultHandler ¶ added in v0.7.1
func NewDefaultHandler( config *Config, ) (*DefaultHandler, error)
NewDefaultHandler creates a new connection
Parameters:
- config *Config: configuration for the connection
Returns:
- *DefaultHandler: connection handler
func (*DefaultHandler) Connect ¶ added in v0.7.1
func (d *DefaultHandler) Connect() (*pgxpool.Pool, error)
Connect returns a new connection pool
func (*DefaultHandler) Disconnect ¶ added in v0.7.1
func (d *DefaultHandler) Disconnect()
Disconnect closes the connection pool
func (*DefaultHandler) IsConnected ¶ added in v0.7.5
func (d *DefaultHandler) IsConnected() bool
IsConnected checks if the connection is established
Returns:
- bool: true if the connection is established, false otherwise
type DefaultService ¶
type DefaultService struct {
Handler
// contains filtered or unexported fields
}
DefaultService is the default service struct
func NewDefaultService ¶
func NewDefaultService(config *Config, logger *slog.Logger) ( instance *DefaultService, err error, )
NewDefaultService creates a new default service
Parameters:
- config *Config: configuration for the connection
- logger *slog.Logger: the logger instance
Returns:
- *DefaultService: the DefaultService instance
- error: if any error occurs
func (*DefaultService) ClearStatTicker ¶ added in v0.5.6
func (d *DefaultService) ClearStatTicker()
ClearStatTicker clears the stat ticker
func (*DefaultService) CreateTransaction ¶
func (d *DefaultService) CreateTransaction( ctx context.Context, fn TransactionFn, ) error
CreateTransaction creates a transaction for the database with a context
Parameters:
- ctx: the context to use
- fn: the function to execute within the transaction
Returns:
- error: if any error occurs
func (*DefaultService) Exec ¶
func (d *DefaultService) Exec(query *string, params ...any) ( *pgconn.CommandTag, error, )
Exec executes a query with parameters and returns the result
Parameters:
- query: the SQL query to execute
- params: the parameters for the SQL query
Returns:
- *pgconn.CommandTag: the command tag result
- error: if any error occurs
func (*DefaultService) ExecWithCtx ¶
func (d *DefaultService) ExecWithCtx( ctx context.Context, query *string, params ...any, ) ( *pgconn.CommandTag, error, )
ExecWithCtx executes a query with parameters and returns the result with a context
Parameters:
- ctx: the context to use
- query: the SQL query to execute
- params: the parameters for the SQL query
Returns:
- *pgconn.CommandTag: the command tag result
- error: if any error occurs
func (*DefaultService) Query ¶ added in v0.5.4
func (d *DefaultService) Query( query *string, params ...any, ) (pgx.Rows, error)
Query runs a query with parameters and returns the result
Parameters:
- query: the SQL query to execute
- params: the parameters for the SQL query
Returns:
- pgx.Rows: the result rows
- error: if any error occurs
func (*DefaultService) QueryRow ¶
func (d *DefaultService) QueryRow( query *string, params ...any, ) (pgx.Row, error)
QueryRow runs a query row with parameters and returns the result row
Parameters:
- query: the SQL query to execute
- params: the parameters for the SQL query
Returns:
- pgx.Row: the result row
- error: if any error occurs
func (*DefaultService) QueryRowWithCtx ¶
func (d *DefaultService) QueryRowWithCtx( ctx context.Context, query *string, params ...any, ) (pgx.Row, error)
QueryRowWithCtx runs a query row with parameters and returns the result row with a context
Parameters:
- ctx: the context to use
- query: the SQL query to execute
- params: the parameters for the SQL query
Returns:
- pgx.Row: the result row
- error: if any error occurs
func (*DefaultService) QueryWithCtx ¶ added in v0.5.4
func (d *DefaultService) QueryWithCtx( ctx context.Context, query *string, params ...any, ) (pgx.Rows, error)
QueryWithCtx runs a query with parameters and returns the result with a context
Parameters:
- ctx: the context to use
- query: the SQL query to execute
- params: the parameters for the SQL query
Returns:
- pgx.Rows: the result rows
func (*DefaultService) ScanRow ¶
func (d *DefaultService) ScanRow( row pgx.Row, destinations ...any, ) error
ScanRow scans a row
Parameters:
- row: the pgx.Row instance - destinations: the destinations to scan the row into
Returns:
- error: if any error occurs
func (*DefaultService) SetStatTicker ¶ added in v0.5.6
func (d *DefaultService) SetStatTicker( ctx context.Context, duration time.Duration, fn func(*pgxpool.Stat), )
SetStatTicker sets a stat ticker
Parameters:
- ctx: the context to use
- duration: the duration of the ticker
- fn: the function to execute on each tick, receiving the pgxpool.Stat
type Handler ¶ added in v0.7.1
type Handler interface {
Connect() (*pgxpool.Pool, error)
IsConnected() bool
Pool() (*pgxpool.Pool, error)
Disconnect()
}
Handler interface
type Service ¶
type Service interface {
Handler
CreateTransaction(ctx context.Context, fn TransactionFn) error
Exec(query *string, params ...any) (*pgconn.CommandTag, error)
ExecWithCtx(
ctx context.Context,
query *string,
params ...any,
) (*pgconn.CommandTag, error)
Query(query *string, params ...any) (pgx.Rows, error)
QueryWithCtx(
ctx context.Context,
query *string,
params ...any,
) (pgx.Rows, error)
QueryRow(query *string, params ...any) (pgx.Row, error)
QueryRowWithCtx(
ctx context.Context,
query *string,
params ...any,
) (pgx.Row, error)
ScanRow(row pgx.Row, destinations ...any) error
SetStatTicker(
ctx context.Context,
duration time.Duration,
fn func(*pgxpool.Stat),
)
ClearStatTicker()
}
Service is the interface for the service
type TransactionFn ¶ added in v0.7.5
TransactionFn is the function type for transactions with context