Documentation
¶
Overview ¶
Package dbrx provides a dynamic database client that supports runtime configuration updates. It wraps dbx.Client with atomic pointer access, automatic metrics and tracing integration, and hot-reload capability.
Index ¶
- Variables
- type Client
- func (c *Client) Close() error
- func (c *Client) DB() (*dbx.Client, error)
- func (c *Client) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (c *Client) ExecNamed(ctx context.Context, query string, arg any) (sql.Result, error)
- func (c *Client) Healthcheck(ctx context.Context) error
- func (c *Client) RunInTransaction(ctx context.Context, txFunc db.TxFunc, opts ...db.TxOption) error
- func (c *Client) Select(ctx context.Context, ptr any, query string, args ...any) error
- func (c *Client) SelectRow(ctx context.Context, ptr any, query string, args ...any) error
- func (c *Client) Upgrade(ctx context.Context, config dbx.Config) error
Constants ¶
This section is empty.
Variables ¶
var (
ErrClientIsNotInitialized = errors.New("client is not initialized")
)
ErrClientIsNotInitialized is returned when the client has not been initialized.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client provides dynamic database client management with hot-reload support. It wraps a dbx.Client and allows configuration updates at runtime. It is safe for concurrent use.
func New ¶
New creates a new Client with the provided logger and options. The client is not initialized until Upgrade is called.
func (*Client) Close ¶
Close closes the database connection and resets the client configuration. Returns an error if the underlying client fails to close.
func (*Client) DB ¶
DB returns the underlying database client. Returns an error if the client has not been initialized.
func (*Client) Exec ¶
Exec executes a query that does not return rows. Returns an error if the client is not initialized or if the query fails.
func (*Client) ExecNamed ¶
ExecNamed executes a named-parameter query that does not return rows. Returns an error if the client is not initialized or if the query fails.
func (*Client) Healthcheck ¶
Healthcheck verifies that the database connection is alive. Executes a simple query with a 500ms timeout. Returns an error if the client is not initialized or if the query fails.
func (*Client) RunInTransaction ¶
RunInTransaction executes the provided function within a database transaction. Returns an error if the client is not initialized or if the transaction fails.
func (*Client) Select ¶
Select executes a query that returns multiple rows and scans them into the provided pointer. Returns an error if the client is not initialized or if the query fails.
func (*Client) SelectRow ¶
SelectRow executes a query that returns a single row and scans it into the provided pointer. Returns an error if the client is not initialized or if the query fails.
func (*Client) Upgrade ¶
Upgrade initializes or reinitializes the database client with the provided configuration. If the new configuration is identical to the previous one, initialization is skipped. It automatically adds metrics tracing, schema creation, and application name options. Returns an error if the connection fails or if the client is in read-only mode.