Skip to content

Commit 8f5b088

Browse files
Update godocs for release
Co-authored-by: Erik Dubbelboer <[email protected]>
1 parent e9ebb28 commit 8f5b088

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

filter/converter.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ var basicOperatorMap = map[string]string{
2020
"$regex": "~*",
2121
}
2222

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

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

41-
// NewConverter creates a new Converter with optional nested JSONB field mapping.
42+
// NewConverter creates a new [Converter] with optional nested JSONB field mapping.
4243
//
43-
// Note: When using github.com/lib/pq, the filter.WithArrayDriver should be set to pq.Array.
44+
// Note: When using https://github.com/lib/pq, the [filter.WithArrayDriver] should be set to pq.Array.
4445
func NewConverter(options ...Option) *Converter {
4546
converter := &Converter{
4647
// don't set defaults, use the once.Do in #Convert()
@@ -63,7 +64,7 @@ func (c *Converter) Convert(query []byte, startAtParameterIndex int) (conditions
6364
c.emptyCondition = "FALSE"
6465
}
6566
if c.placeholderName == "" {
66-
c.placeholderName = DefaultPlaceholderName
67+
c.placeholderName = defaultPlaceholderName
6768
}
6869
})
6970

filter/converter_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11
package filter_test
22

33
import (
4+
"database/sql"
45
"fmt"
56
"reflect"
67
"testing"
78

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

12+
func ExampleNewConverter() {
13+
// Remeber to use `filter.WithArrayDriver(pg.Array)` when using github.com/lib/pq
14+
converter := filter.NewConverter(filter.WithNestedJSONB("meta", "created_at", "updated_at"))
15+
16+
mongoFilterQuery := `{
17+
"name": "John",
18+
"created_at": {
19+
"$gte": "2020-01-01T00:00:00Z"
20+
}
21+
}`
22+
conditions, values, err := converter.Convert([]byte(mongoFilterQuery), 1)
23+
if err != nil {
24+
// handle error
25+
}
26+
27+
var db *sql.DB // setup your database connection
28+
29+
db.Query("SELECT * FROM users WHERE "+conditions, values)
30+
// SELECT * FROM users WHERE (("created_at" >= $1) AND ("meta"->>'name' = $2)), 2020-01-01T00:00:00Z, "John"
31+
}
32+
1133
func TestConverter_Convert(t *testing.T) {
1234
tests := []struct {
1335
name string
@@ -427,3 +449,14 @@ func TestConverter_NoConstructor(t *testing.T) {
427449
t.Errorf("Converter.Convert() values = %v, want nil", values)
428450
}
429451
}
452+
453+
func TestConverter_CopyReference(t *testing.T) {
454+
c := filter.Converter{}
455+
conditions, _, err := c.Convert([]byte(``), 1)
456+
if err != nil {
457+
t.Fatal(err)
458+
}
459+
if want := "FALSE"; conditions != want {
460+
t.Errorf("Converter.Convert() conditions = %v, want %v", conditions, want)
461+
}
462+
}

filter/doc.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// This package converts MongoDB query filters into PostgreSQL WHERE clauses.
2+
// It's designed to be simple, secure, and free of dependencies.
3+
//
4+
// See: https://www.mongodb.com/docs/compass/current/query/filter
5+
package filter

0 commit comments

Comments
 (0)