postgres

package module
v0.0.19-dev Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClientNotInitialized = errors.New("db client not initialized; call NewConnection first")
)

internal shared client singleton

Functions

func CloseConnection

func CloseConnection() error

CloseConnection closes the default client connection.

func FilterSearch

func FilterSearch(q *bun.SelectQuery, search string, fields ...string)

FilterSearch adds a case-insensitive search filter for the given fields.

func GetDB

func GetDB() *bun.DB

GetDB returns the globally initialized *bun.DB instance. You should call NewConnection first before calling GetDB.

func NewConnection

func NewConnection(c *Config, l *zap.Logger) (err error)

NewConnection initializes a new Bun DB connection and sets it as the default. It also adds a zap logger hook and optionally a verbose logger if debug is true.

func RequestSort

func RequestSort(sort []string) string

Types

type BaseRepository

type BaseRepository[T any] struct {
	DB      *bun.DB
	Context context.Context
	// contains filtered or unexported fields
}

func NewBaseRepository

func NewBaseRepository[T any](db *bun.DB, table string, searchFields, defaultRelations []string, enableSoftDelete bool) *BaseRepository[T]

func (*BaseRepository[T]) FindAll

func (r *BaseRepository[T]) FindAll(opts *common.QueryOption, customQuery CustomQueryFn) ([]*T, int64, error)

func (*BaseRepository[T]) FindByID

func (r *BaseRepository[T]) FindByID(id any) (*T, error)

func (*BaseRepository[T]) FindOne

func (r *BaseRepository[T]) FindOne(customQuery CustomQueryFn) (*T, error)

func (*BaseRepository[T]) Insert

func (r *BaseRepository[T]) Insert(entity *T) error

func (*BaseRepository[T]) SoftDelete

func (r *BaseRepository[T]) SoftDelete(id any) error

func (*BaseRepository[T]) Update

func (r *BaseRepository[T]) Update(entity *T, fields ...string) error

func (*BaseRepository[T]) WithContext

func (r *BaseRepository[T]) WithContext(ctx context.Context) common.BaseRepositoryInterface[T]

type Client

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

func NewClient

func NewClient(cfg *Config, l *zap.Logger) (*Client, error)

func (*Client) Close

func (c *Client) Close() error

func (*Client) GetDB

func (c *Client) GetDB() *bun.DB

type Config

type Config struct {
	Server     string // Host or IP of the Postgres server
	Username   string // Database username
	Password   string // Database password
	Database   string // Database name
	Datasource string // Full DSN string (overrides Server/Username/Password/Database)
}

Config holds the configuration parameters for connecting to a PostgreSQL database.

func ConfigDefault

func ConfigDefault(db string) *Config

ConfigDefault creating an config readed from .env file make sure you load the env file in your init app

type CustomQueryFn

type CustomQueryFn func(q *bun.SelectQuery) *bun.SelectQuery

type ZapQueryHook

type ZapQueryHook struct {
	Logger *zap.Logger
}

func (*ZapQueryHook) AfterQuery

func (h *ZapQueryHook) AfterQuery(ctx context.Context, event *bun.QueryEvent)

func (*ZapQueryHook) BeforeQuery

func (h *ZapQueryHook) BeforeQuery(ctx context.Context, event *bun.QueryEvent) context.Context

Jump to

Keyboard shortcuts

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