Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
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"))