Skip to content

Commit a373afd

Browse files
Implement more basic operators
Co-authored-by: Koen Bollen <[email protected]>
1 parent 9c68597 commit a373afd

File tree

5 files changed

+71
-13
lines changed

5 files changed

+71
-13
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Let's filter some lobbies in a multiplayer game:
1313
"$and": [
1414
{
1515
"$or": [ // match two maps
16-
{ "map": { "$contains": "aztec" } },
17-
{ "map": { "$contains": "nuke" } }
16+
{ "map": { "$regex": "aztec" } },
17+
{ "map": { "$regex": "nuke" } }
1818
]
1919
},
2020
{ "password": "" }, // no password set
@@ -27,15 +27,15 @@ Let's filter some lobbies in a multiplayer game:
2727
Converts to:
2828
```sql
2929
(
30-
"customdata"->>"map" LIKE ?
30+
"customdata"->>"map" ~* $1
3131
OR
32-
"customdata"->>"map" LIKE ?
33-
)
34-
AND "password" = ?
32+
"customdata"->>"map" ~* $2
33+
)
34+
AND "password" = $3
3535
AND (
36-
"playerCount" >= ?
36+
"playerCount" >= $4
3737
AND
38-
"playerCount" < ?
38+
"playerCount" < $5
3939
)
4040
```
4141
And values:

examples/basic_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ func ExampleNewConverter() {
2121
}
2222

2323
fmt.Println(conditions)
24-
fmt.Println(values)
24+
fmt.Printf("%#v\n", values)
2525
// Output:
26-
// "meta"->>'name' = $1 AND "created_at" >= $2
27-
// [John 2020-01-01T00:00:00Z]
26+
// (("created_at" >= $1) AND ("meta"->>'name' = $2))
27+
// []interface {}{"2020-01-01T00:00:00Z", "John"}
2828
}

examples/readme_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package examples
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/poki/mongodb-filter-to-postgres/filter"
7+
)
8+
9+
func ExampleNewConverter_README() {
10+
converter := filter.NewConverter(filter.WithNestedJSONB("meta", "created_at", "updated_at"))
11+
12+
mongoFilterQuery := `{
13+
"$and": [
14+
{
15+
"$or": [
16+
{ "map": { "$regex": "aztec" } },
17+
{ "map": { "$regex": "nuke" } }
18+
]
19+
},
20+
{ "password": "" },
21+
{
22+
"playerCount": { "$gte": 2, "$lt": 10 }
23+
}
24+
]
25+
}`
26+
conditions, values, err := converter.Convert([]byte(mongoFilterQuery))
27+
if err != nil {
28+
panic(err)
29+
// handle error
30+
}
31+
32+
fmt.Println(conditions)
33+
fmt.Printf("%#v\n", values)
34+
// Output:
35+
// ((("meta"->>'map' ~* ?) OR ("meta"->>'map' ~* ?)) AND ("meta"->>'password' = $3) AND (("meta"->>'playerCount' >= $4) AND ("meta"->>'playerCount' < $5)))
36+
// []interface {}{"aztec", "nuke", "", 2, 10}
37+
}

filter/converter.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@ import (
88
)
99

1010
var BasicOperatorMap = map[string]string{
11-
"$gt": ">",
12-
"$gte": ">=",
11+
"$gt": ">",
12+
"$gte": ">=",
13+
"$lt": "<",
14+
"$lte": "<=",
15+
"$eq": "=",
16+
"$ne": "!=",
17+
"$regex": "~*",
1318
}
1419

1520
type Converter struct {

filter/converter_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,22 @@ func TestConverter_Convert(t *testing.T) {
153153
[]any{"John", float64(3)},
154154
nil,
155155
},
156+
{
157+
"basic contains operator",
158+
nil,
159+
`{"name": {"$regex": "John"}}`,
160+
`("name" ~* $1)`,
161+
[]any{"John"},
162+
nil,
163+
},
164+
{
165+
"complex contains operator",
166+
nil,
167+
`{"$or": [{"name": {"$regex": "John"}}, {"name": {"$regex": "Jane"}}]}`,
168+
`(("name" ~* $1) OR ("name" ~* $2))`,
169+
[]any{"John", "Jane"},
170+
nil,
171+
},
156172
}
157173
for _, tt := range tests {
158174
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)