mpostgres

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMaxLimit int64 = 50

DefaultMaxLimit is the default max limit for any query using db.LimitOffsetQuery It can be overrideable by LimitOffsetQuery.MaxLimit

Variables

View Source
var WithLimit = func(limit int64) SQLQueryBuilderOption {
	return func(b *SQLQueryBuilder) {
		b.Limit = fmt.Sprintf("LIMIT %d", limit)
	}
}

WithLimit adds limit to a SQL query builder

View Source
var WithSort = func(field string, order string) SQLQueryBuilderOption {
	return func(b *SQLQueryBuilder) {
		b.Sorts = append(b.Sorts, fmt.Sprintf("%s %s", field, strings.ToUpper(order)))
	}
}

WithSort sorts the results by given sort argument

View Source
var WithTextSearch = func(field, text string) SQLQueryBuilderOption {
	return func(b *SQLQueryBuilder) {
		if text != "" {
			b.Where = append(b.Where, fmt.Sprintf("%s @@ to_tsquery('%s')", field, text))
		}
	}
}

WithTextSearch searches for docs using PostgreSQL full-text search

Functions

func Count

func Count(ctx context.Context, t *Table, conditions string, args ...any) (int64, error)

Count returns the number of rows in the table

func Create

func Create(ctx context.Context, t *Table, data map[string]any) (int64, error)

Create inserts a new record into the specified table.

func Delete

func Delete(ctx context.Context, t *Table, id int64) error

Delete removes a record identified by its ID.

func FindAll

func FindAll(ctx context.Context, t *Table, dest any, conditions string, args ...any) error

FindAll fetches records from a PostgreSQL table

func FindByID

func FindByID(ctx context.Context, t *Table, id int64, dest any) error

FindByID finds a row by ID

func RegexIgnoreAccents

func RegexIgnoreAccents(regex string) string

RegexIgnoreAccents receives a regex, than, for each char it's adds the accents variations to expression Ex: Given "a" -> "aáàãâ" Ex: Given "c" -> "ç"

func RemoveChars

func RemoveChars(str string, chars map[string]bool) string

RemoveChars from a string

func Update

func Update(ctx context.Context, t *Table, id int64, data map[string]any) error

Update safely updates records in a specified table

Types

type PostgresConnection

type PostgresConnection struct {
	ConnectionStringPrimary string
	ConnectionStringReplica string
	PrimaryDBName           string
	ReplicaDBName           string
	ConnectionDB            *dbresolver.DB
	Connected               bool
}

PostgresConnection is a hub which deal with postgres connections.

func (*PostgresConnection) Connect

func (pc *PostgresConnection) Connect() error

Connect keeps a singleton connection with postgres.

func (*PostgresConnection) GetDB

func (pc *PostgresConnection) GetDB(ctx context.Context) (dbresolver.DB, error)

GetDB returns a pointer to the postgres connection, initializing it if necessary.

type SQLQueryBuilder

type SQLQueryBuilder struct {
	Params []any
	Where  []string
	Sorts  []string
	Table  string
	Limit  string
	Offset string
}

SQLQueryBuilder builds a query for PostgreSQL

func NewSQLQueryBuilder

func NewSQLQueryBuilder(table string, opts ...SQLQueryBuilderOption) *SQLQueryBuilder

NewSQLQueryBuilder creates an instance of SQLQueryBuilder

func (*SQLQueryBuilder) With

With adds a new option to query builder

type SQLQueryBuilderOption

type SQLQueryBuilderOption func(b *SQLQueryBuilder)

SQLQueryBuilderOption is a kind of interface for build options

func WithFilter

func WithFilter(column string, value any) SQLQueryBuilderOption

WithFilter adds a new filter condition to the query builder

func WithLimitOffset

func WithLimitOffset(limit, offset int64) SQLQueryBuilderOption

WithLimitOffset adds limit and offset pagination style to a SQL query builder

type Table

type Table struct {
	Name string

	Columns []string
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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