Documentation
¶
Overview ¶
Code generated by interfacer, DO NOT EDIT.
Index ¶
- Variables
- func ExecQuery(ctx context.Context, dbtx DBTX, query string, args ...any) error
- func SelectRow[T any](ctx context.Context, dbtx DBTX, query string, args ...any) (T, error)
- func SelectRows[T any](ctx context.Context, dbtx DBTX, query string, args ...any) ([]T, error)
- type DBTX
- type Database
- type Page
- type PageRequest
Constants ¶
This section is empty.
Variables ¶
var ErrNoRows = pgx.ErrNoRows
Functions ¶
func ExecQuery ¶
ExecQuery executes a query that does not return rows (e.g., INSERT, UPDATE, DELETE). It uses the provided dbtx to execute the query and returns an error if the execution fails.
func SelectRow ¶
SelectRow executes a query and returns a single row as a struct of type T. It uses the provided dbtx to execute the query and collects the result into a struct of type T. If the query fails or no rows are returned, it returns an error.
func SelectRows ¶
SelectRows executes a query and returns multiple rows as a slice of structs of type T. It uses the provided dbtx to execute the query and collects the results into a slice of type T. If the query fails or no rows are returned, it returns an error.
Types ¶
type DBTX ¶
type DBTX interface {
Exec(context.Context, string, ...any) (pgconn.CommandTag, error)
Query(context.Context, string, ...any) (pgx.Rows, error)
QueryRow(context.Context, string, ...any) pgx.Row
}
DBTX is an interface that defines the methods for executing queries and transactions. only supports pgx package related methods.
type Database ¶
type Database interface {
// Disconnect closes the database connection pool and sets the teardown flag to true.
// This method should be called when the application is shutting down to ensure all resources are released properly.
// It sets the isTeardown flag to true to indicate that the database connection is being torn down.
// This prevents any further operations on the database connection pool after it has been closed.
//
// If `noTeardown` is true, it will not set the teardown flag,
// allowing the client to be reused later.
// // If `noTeardown` is false or not provided, it will set the teardown flag
// and close the client connection, preventing any further operations.
Disconnect(noTeardown ...bool)
// WithReadTX executes a function within a read-only database transaction context.
// If an existing transaction is provided via existingQ, it uses that instead of creating a new transaction.
// Otherwise, it begins a new read-only transaction, executes the provided function with the transaction-aware dbtx,
// and commits the transaction on success or rolls back on error.
// The function automatically handles transaction cleanup through deferred rollback.
// This method is optimized for read operations and may provide better performance for queries that don't modify data.
//
// Parameters:
// - ctx: Context for the transaction operation
// - fn: Function to execute within the transaction, receives a transaction interface
// - existingQ: Optional existing transaction to reuse instead of creating a new transaction
//
// Returns:
// - error: Any error from transaction operations or the executed function
WithReadTX(ctx context.Context, fn func(tx DBTX) error, existingQ ...DBTX) error
// WithTX executes a function within a database transaction context.
// If an existing transaction is provided via existingQ, it uses that instead of creating a new transaction.
// Otherwise, it begins a new read-write transaction, executes the provided function with the transaction-aware dbtx,
// and commits the transaction on success or rolls back on error.
// The function automatically handles transaction cleanup through deferred rollback.
//
// Parameters:
// - ctx: Context for the transaction operation
// - fn: Function to execute within the transaction, receives a transaction interface
// - existingQ: Optional existing transaction to reuse instead of creating a new transaction
//
// Returns:
// - error: Any error from transaction operations or the executed function
WithTX(ctx context.Context, fn func(tx DBTX) error, existingQ ...DBTX) error
}
Database defines the public interface for dbo.
func NewDatabase ¶
func NewDatabase( ctx context.Context, cfg config.DatabaseConfig, migrations fs.FS, ) (Database, error)
NewDatabase creates a new database connection pool and runs migrations. It takes a context for the connection, a connection URL, and a filesystem containing migration files. It returns a Database interface or an error if the connection or migration fails.
type Page ¶ added in v1.1.5
type Page[T any] struct { Content []T `json:"content"` Pageable PageRequest `json:"pageable"` TotalElements int64 `json:"totalElements"` TotalPages int `json:"totalPages"` Number int `json:"number"` Size int `json:"size"` NumberOfElements int `json:"numberOfElements"` IsLast bool `json:"last"` IsFirst bool `json:"first"` IsEmpty bool `json:"empty"` }
func MapContent ¶ added in v1.1.5
func SelectRowsPageable ¶ added in v1.1.5
func SelectRowsPageable[T any]( ctx context.Context, dbtx DBTX, pageRequest PageRequest, query string, args ...any, ) (Page[T], error)
SelectRowsPageable executes a query and returns a paginated result set. It uses the provided dbtx to execute the query and collects the results into a Page[T]. The PageRequest parameter specifies the pagination details such as page number and size.
Note: make sure the query does not have a `;` at the end, as this function appends LIMIT and OFFSET for pagination.