Skip to content

Commit

Permalink
Update godocs for release
Browse files Browse the repository at this point in the history
Co-authored-by: Erik Dubbelboer <[email protected]>
  • Loading branch information
koenbollen and erikdubbelboer committed Jun 21, 2024
1 parent e9ebb28 commit 8f5b088
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
11 changes: 6 additions & 5 deletions filter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ var basicOperatorMap = map[string]string{
"$regex": "~*",
}

// DefaultPlaceholderName is the default placeholder name used in the generated SQL query.
// defaultPlaceholderName is the default placeholder name used in the generated SQL query.
// This name should not be used in the database or any JSONB column. It can be changed using
// the WithPlaceholderName option.
const DefaultPlaceholderName = "__filter_placeholder"
const defaultPlaceholderName = "__filter_placeholder"

// Converter converts MongoDB filter queries to SQL conditions and values. Use [filter.NewConverter] to create a new instance.
type Converter struct {
nestedColumn string
nestedExemptions []string
Expand All @@ -38,9 +39,9 @@ type Converter struct {
once sync.Once
}

// NewConverter creates a new Converter with optional nested JSONB field mapping.
// 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.
// Note: When using https://github.com/lib/pq, the [filter.WithArrayDriver] should be set to pq.Array.
func NewConverter(options ...Option) *Converter {
converter := &Converter{
// don't set defaults, use the once.Do in #Convert()
Expand All @@ -63,7 +64,7 @@ func (c *Converter) Convert(query []byte, startAtParameterIndex int) (conditions
c.emptyCondition = "FALSE"
}
if c.placeholderName == "" {
c.placeholderName = DefaultPlaceholderName
c.placeholderName = defaultPlaceholderName
}
})

Expand Down
33 changes: 33 additions & 0 deletions filter/converter_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
package filter_test

import (
"database/sql"
"fmt"
"reflect"
"testing"

"github.com/poki/mongodb-filter-to-postgres/filter"
)

func ExampleNewConverter() {
// Remeber to use `filter.WithArrayDriver(pg.Array)` when using github.com/lib/pq
converter := filter.NewConverter(filter.WithNestedJSONB("meta", "created_at", "updated_at"))

mongoFilterQuery := `{
"name": "John",
"created_at": {
"$gte": "2020-01-01T00:00:00Z"
}
}`
conditions, values, err := converter.Convert([]byte(mongoFilterQuery), 1)
if err != nil {
// handle error
}

var db *sql.DB // setup your database connection

db.Query("SELECT * FROM users WHERE "+conditions, values)

Check failure on line 29 in filter/converter_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `db.Query` is not checked (errcheck)
// SELECT * FROM users WHERE (("created_at" >= $1) AND ("meta"->>'name' = $2)), 2020-01-01T00:00:00Z, "John"
}

func TestConverter_Convert(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -427,3 +449,14 @@ func TestConverter_NoConstructor(t *testing.T) {
t.Errorf("Converter.Convert() values = %v, want nil", values)
}
}

func TestConverter_CopyReference(t *testing.T) {
c := filter.Converter{}
conditions, _, err := c.Convert([]byte(``), 1)
if err != nil {
t.Fatal(err)
}
if want := "FALSE"; conditions != want {
t.Errorf("Converter.Convert() conditions = %v, want %v", conditions, want)
}
}
5 changes: 5 additions & 0 deletions filter/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// This package converts MongoDB query filters into PostgreSQL WHERE clauses.
// It's designed to be simple, secure, and free of dependencies.
//
// See: https://www.mongodb.com/docs/compass/current/query/filter
package filter

0 comments on commit 8f5b088

Please sign in to comment.