postgres

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddToBuffer

func AddToBuffer(ctx context.Context, model interface{}, bufferSize int) error

If the buffer reaches the specified size, it flushes the buffer to the database.

func BatchInsertData

func BatchInsertData(ctx context.Context, records interface{}, batchSize int, opts ...ConflictOption) error

BatchInsertData inserts multiple records in batches with optional conflict handling records must be a slice of structs, batchSize determines how many records per batch (0 = default)

func Connect

func Connect(cfg DBConfig) (*gorm.DB, error)

Connect establishes a connection to the database using the given config.

func CreateTable

func CreateTable(ctx context.Context, model interface{}, tableName ...string) error

CreateTable creates a table in the database based on the given model.

func DeleteData

func DeleteData(ctx context.Context, model interface{}, conditions map[string]interface{}) error

DeleteData deletes records from the specified table based on conditions.

func ExecuteCustomQuery

func ExecuteCustomQuery(ctx context.Context, query string, args ...interface{}) error

ExecuteCustomQuery allows executing custom SQL queries.

func ExecuteQueryWithContext

func ExecuteQueryWithContext(ctx context.Context, query string, args []interface{}) error

ExecuteQueryWithContext allows executing custom SQL queries with a context-based timeout.

func FetchData

func FetchData(ctx context.Context, model interface{}, conditions map[string]interface{}) error

FetchData retrieves records from the specified table based on conditions.

func FlushBufferToDB

func FlushBufferToDB(ctx context.Context) error

FlushBufferToDB flushes the buffer to the database in a batch insert.

func GetDB

func GetDB() *gorm.DB

GetDB returns the connected database instance.

func InsertData

func InsertData(ctx context.Context, record interface{}) error

InsertData inserts a record into the specified table

func IsTableExists

func IsTableExists(ctx context.Context, model interface{}, tableName ...string) bool

IsTableExists checks if a table exists for a given model.

func RunMigrations

func RunMigrations(cfg DBConfig, migrationsDir string) error

RunMigrations applies migrations from the specified directory.

func StartBufferFlusher

func StartBufferFlusher(ctx context.Context, interval time.Duration)

StartBufferFlusher starts a background goroutine to periodically flush the buffer to the database.

func UpdateData

func UpdateData(ctx context.Context, model interface{}, updates map[string]interface{}, conditions map[string]interface{}) error

UpdateData updates records in the specified table based on conditions.

Types

type BatchInsertConfig

type BatchInsertConfig struct {
	ConflictColumns []string
	DoNothing       bool
	Updates         map[string]interface{}
}

BatchInsertConfig holds configuration for batch insert operations

type ConflictOption

type ConflictOption func(*BatchInsertConfig)

ConflictOption defines a function type for configuring conflict handling

func WithOnConflictDoNothing

func WithOnConflictDoNothing(columns []string) ConflictOption

WithOnConflictDoNothing specifies columns to watch for conflicts and do nothing on conflict

func WithOnConflictDoUpdate

func WithOnConflictDoUpdate(columns []string, updates map[string]interface{}) ConflictOption

WithOnConflictDoUpdate specifies columns to watch for conflicts and perform updates

type DBConfig

type DBConfig struct {
	Host            string
	Port            int
	User            string
	Password        string
	DBName          string
	MaxConns        int
	MinConns        int
	MaxConnLifetime time.Duration
	MaxConnIdleTime time.Duration
}

type Postgres

type Postgres interface {
	Connect(cfg DBConfig) (*gorm.DB, error)
	GetDB() *gorm.DB
	RunMigrations(cfg DBConfig, migrationsDir string) error
}

Jump to

Keyboard shortcuts

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