Documentation ¶
Index ¶
- Constants
- Variables
- func Count(ctx context.Context, t *Table, conditions string, args ...any) (int64, error)
- func Create(ctx context.Context, t *Table, data map[string]any) (int64, error)
- func Delete(ctx context.Context, t *Table, id int64) error
- func FindAll(ctx context.Context, t *Table, dest any, conditions string, args ...any) error
- func FindByID(ctx context.Context, t *Table, id int64, dest any) error
- func RegexIgnoreAccents(regex string) string
- func RemoveChars(str string, chars map[string]bool) string
- func Update(ctx context.Context, t *Table, id int64, data map[string]any) error
- type PostgresConnection
- type SQLQueryBuilder
- type SQLQueryBuilderOption
- type Table
Constants ¶
const DefaultMaxLimit int64 = 50
DefaultMaxLimit is the default max limit for any query using db.LimitOffsetQuery It can be overrideable by LimitOffsetQuery.MaxLimit
Variables ¶
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
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
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 RegexIgnoreAccents ¶
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 ¶
RemoveChars from a string
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.
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 ¶
func (q *SQLQueryBuilder) With(opt SQLQueryBuilderOption)
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