postgres

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Module

func Module() fx.Option

Module provides an fx.Option to integrate Ent client with the application

func NewEntClient

func NewEntClient(config *config.Configuration, logger *logger.Logger) (*ent.Client, error)

NewEntClient creates a new Ent client

Types

type Client

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

Client wraps ent.Client to provide transaction management

func (*Client) Querier

func (c *Client) Querier(ctx context.Context) *ent.Client

Querier returns the current transaction client if in a transaction, or the regular client

func (*Client) TxFromContext

func (c *Client) TxFromContext(ctx context.Context) *ent.Tx

TxFromContext returns the transaction from context if it exists

func (*Client) WithTx

func (c *Client) WithTx(ctx context.Context, fn func(ctx context.Context) error) error

WithTx wraps the given function in a transaction

type DB

type DB struct {
	*sqlx.DB
	// contains filtered or unexported fields
}

DB wraps sqlx.DB to provide transaction management

func NewDB

func NewDB(config *config.Configuration, logger *logger.Logger) (*DB, error)

NewDB creates a new DB instance

func (*DB) BeginTx

func (db *DB) BeginTx(ctx context.Context) (context.Context, *Tx, error)

BeginTx starts a new transaction

func (*DB) Close

func (db *DB) Close()

Close closes the database connection

func (*DB) CommitTx

func (db *DB) CommitTx(ctx context.Context) error

CommitTx commits the current transaction level

func (*DB) GetQuerier

func (db *DB) GetQuerier(ctx context.Context) Querier

GetQuerier returns either the transaction from context or the base DB

func (*DB) NamedExecContext

func (db *DB) NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error)

NamedExecContext is a helper method that wraps NamedExec with context

func (*DB) NamedQueryContext

func (db *DB) NamedQueryContext(ctx context.Context, query string, arg interface{}) (*sqlx.Rows, error)

NamedQueryContext is a helper method that wraps NamedQuery with context

func (*DB) QueryWithTenant

func (db *DB) QueryWithTenant(ctx context.Context, tenantID string, query string, args ...interface{}) (*sqlx.Rows, error)

func (*DB) RollbackTx

func (db *DB) RollbackTx(ctx context.Context) error

RollbackTx rolls back the current transaction level

func (*DB) WithTx

func (db *DB) WithTx(ctx context.Context, fn func(ctx context.Context) error) error

WithTx executes a function within a transaction

type IClient

type IClient interface {
	// WithTx wraps the given function in a transaction
	WithTx(ctx context.Context, fn func(context.Context) error) error

	// TxFromContext returns the transaction from context if it exists
	TxFromContext(ctx context.Context) *ent.Tx

	// Querier returns the current transaction client if in a transaction, or the regular client
	Querier(ctx context.Context) *ent.Client
}

IClient defines the interface for postgres client operations

func NewClient

func NewClient(client *ent.Client, logger *logger.Logger) IClient

NewClient creates a new ent client wrapper with transaction management

type Querier

type Querier interface {
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
	GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
	SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
	NamedExec(query string, arg interface{}) (sql.Result, error)
	NamedQuery(query string, arg interface{}) (*sqlx.Rows, error)
	PrepareNamed(query string) (*sqlx.NamedStmt, error)
	Preparex(query string) (*sqlx.Stmt, error)
}

Querier interface defines all database operations Both *sqlx.DB and *sqlx.Tx implement these methods

type QueryTracer

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

QueryTracer wraps database operations with tracing and logging

func NewQueryTracer

func NewQueryTracer(logger *logger.Logger, query string, params interface{}, txID string) *QueryTracer

NewQueryTracer creates a new query tracer

func (*QueryTracer) Done

func (qt *QueryTracer) Done(err error)

Done logs the query completion

type TracedQuerier

type TracedQuerier struct {
	Querier
	// contains filtered or unexported fields
}

TracedQuerier wraps a Querier with tracing

func NewTracedQuerier

func NewTracedQuerier(q Querier, logger *logger.Logger, txID string) *TracedQuerier

NewTracedQuerier creates a new traced querier

func (*TracedQuerier) ExecContext

func (tq *TracedQuerier) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext traces ExecContext calls

func (*TracedQuerier) NamedExec

func (tq *TracedQuerier) NamedExec(query string, arg interface{}) (sql.Result, error)

NamedExec traces NamedExec calls

func (*TracedQuerier) NamedQuery

func (tq *TracedQuerier) NamedQuery(query string, arg interface{}) (*sqlx.Rows, error)

NamedQuery traces NamedQuery calls

func (*TracedQuerier) QueryContext

func (tq *TracedQuerier) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryContext traces QueryContext calls

type Tx

type Tx struct {
	*sqlx.Tx

	ID string // Unique ID for tracing
	// contains filtered or unexported fields
}

Tx wraps sqlx.Tx to support nested transactions using savepoints

func GetTx

func GetTx(ctx context.Context) (*Tx, bool)

GetTx retrieves a transaction from the context if it exists

type TxKey

type TxKey struct{}

TxKey is the context key type for storing transaction

Jump to

Keyboard shortcuts

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