postgres

package
v1.1.59 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2026 License: AGPL-3.0 Imports: 15 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

Types

type Client

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

Client wraps ent.Client to provide transaction management and read/write routing

func (*Client) Close added in v1.0.32

func (c *Client) Close() error

Close closes the database connection

func (*Client) LockWithWait added in v1.0.60

func (c *Client) LockWithWait(ctx context.Context, req LockRequest) error

LockWithWait acquires an advisory lock based on the provided request. If Timeout is nil, defaults to 30 seconds. The key should be the entity ID (e.g., wallet ID). Auto released on tx commit/rollback. Must be called inside a transaction.

func (*Client) Reader added in v1.0.32

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

Reader returns the appropriate client for read operations. Intelligently routes to ensure consistency when needed.

Use this for: Get, List, Count, Query operations

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 Transactions ALWAYS use the writer connection to ensure consistency

func (*Client) Writer added in v1.0.32

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

Writer returns the writer client for write operations. Always routes to the primary database.

Use this for: Create, Update, Delete, Save, Exec operations

type EntClients added in v1.0.32

type EntClients struct {
	Writer    *ent.Client
	Reader    *ent.Client
	HasReader bool
}

EntClients holds both writer and reader ENT clients

func NewEntClients added in v1.0.32

func NewEntClients(config *config.Configuration, logger *logger.Logger) (*EntClients, error)

NewEntClients creates both writer and reader Ent clients

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

	// Writer returns the writer client for write operations.
	// Always routes to the primary database (writer endpoint).
	//
	// Routing:
	// - Inside transaction: returns transaction client (writer)
	// - Outside transaction: returns writer client
	//
	// Use for: Create, Update, Delete, Save, Exec operations
	Writer(ctx context.Context) *ent.Client

	// Reader returns the appropriate client for read operations.
	// Intelligently routes based on context to ensure consistency when needed.
	//
	// Routing:
	// - Inside transaction: returns transaction client (writer) for read-your-writes consistency
	// - Force writer flag set: returns writer client for read-after-write consistency
	// - Otherwise: returns reader client (read replica if available)
	//
	// Use for: Get, List, Count, Query operations
	Reader(ctx context.Context) *ent.Client

	// LockWithWait acquires an advisory lock with a default timeout of 30 seconds.
	// The key should be the entity ID (e.g., wallet ID).
	// Must be called inside a transaction. Lock is automatically released on commit/rollback.
	LockWithWait(ctx context.Context, req LockRequest) error

	// Close closes the database connection
	Close() error
}

IClient defines the interface for postgres client operations

func NewClient

func NewClient(clients *EntClients, logger *logger.Logger, sentry *sentry.Service) IClient

NewClient creates a new ent client wrapper with transaction management

func NewSentryClient added in v1.0.17

func NewSentryClient(client IClient, sentry *sentryService.Service, logger *logger.Logger) IClient

NewSentryClient creates a new Sentry-instrumented Postgres client

type LockRequest added in v1.0.60

type LockRequest struct {
	// Key is the entity ID (e.g., wallet ID) to lock
	Key string
	// Timeout is the maximum time to wait for the lock.
	// If nil, defaults to 30 seconds. Must be positive.
	Timeout *time.Duration
}

LockRequest represents a request to acquire an advisory lock

func (*LockRequest) GetTimeout added in v1.0.60

func (r *LockRequest) GetTimeout() time.Duration

GetTimeout returns the timeout duration, defaulting to 30 seconds if nil. Ensures the returned duration is always positive (minimum 1ms).

type SentryClient added in v1.0.17

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

SentryClient wraps the standard postgres client with Sentry monitoring

func (*SentryClient) Close added in v1.0.32

func (c *SentryClient) Close() error

Close closes the database connection

func (*SentryClient) LockWithWait added in v1.0.60

func (c *SentryClient) LockWithWait(ctx context.Context, req LockRequest) error

LockWithWait acquires an advisory lock with wait

func (*SentryClient) Reader added in v1.0.32

func (c *SentryClient) Reader(ctx context.Context) *ent.Client

Reader returns the appropriate client for read operations

func (*SentryClient) TxFromContext added in v1.0.17

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

TxFromContext returns the transaction from context if it exists

func (*SentryClient) WithTx added in v1.0.17

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

WithTx wraps the given function in a transaction with Sentry span tracking

func (*SentryClient) Writer added in v1.0.32

func (c *SentryClient) Writer(ctx context.Context) *ent.Client

Writer returns the writer client for write operations

Jump to

Keyboard shortcuts

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