filter

package
v0.0.0-...-0b1842c Latest Latest
Warning

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

Go to latest
Published: May 3, 2024 License: ISC Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BasicOperatorMap = map[string]string{
	"$gt":    ">",
	"$gte":   ">=",
	"$lt":    "<",
	"$lte":   "<=",
	"$eq":    "=",
	"$ne":    "!=",
	"$regex": "~*",
}

Functions

This section is empty.

Types

type Converter

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

func NewConverter

func NewConverter(options ...Option) *Converter

NewConverter creates a new Converter with optional nested JSONB field mapping.

Note: When using github.com/lib/pq, the filter.WithArrayDriver should be set to pq.Array.

func (*Converter) Convert

func (c *Converter) Convert(query []byte, startAtParameterIndex int) (conditions string, values []any, err error)

Convert converts a MongoDB filter query into SQL conditions and values.

startAtParameterIndex is the index to start the parameter numbering at. Passing X will make the first indexed parameter $X, the second $X+1, and so on.

type Option

type Option func(*Converter)

func WithArrayDriver

func WithArrayDriver(f func(a any) interface {
	driver.Valuer
	sql.Scanner
}) Option

WithArrayDriver is an option to specify a custom driver to convert array values to Postgres driver compatible types. An example for github.com/lib/pq is:

c := filter.NewConverter(filter.WithArrayDriver(pq.Array))

For github.com/jackc/pgx this option is not needed.

func WithEmptyCondition

func WithEmptyCondition(condition string) Option

WithEmptyCondition is an option to specify the condition to be used when the input query filter is empty. (e.g. you have a query with no conditions)

The default value is `FALSE`, because it's the safer choice in most cases.

func WithNestedJSONB

func WithNestedJSONB(column string, exemption ...string) Option

WithNestedJSONB is an option to specify the column name that contains the nested JSONB object. (e.g. you have a column named `metadata` that contains a nested JSONB object)

When this option is set, all keys in the query will be directed to the nested column, you can exempt some keys by providing them as the second argument.

Example:

c := filter.NewConverter(filter.WithNestedJSONB("metadata", "created_at", "updated_at"))

Jump to

Keyboard shortcuts

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