Skip to content

Commit de85a91

Browse files
Improve README and add LICENSE (#3)
To make the repo ready to be open sourced. Co-authored-by: Koen Bollen <[email protected]>
1 parent f804715 commit de85a91

File tree

2 files changed

+72
-8
lines changed

2 files changed

+72
-8
lines changed

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ISC License
2+
3+
Copyright 2024 Poki B.V.
4+
5+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

README.md

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,61 @@
11
# mongodb-filter-to-postgres
22

3-
A simple package that converts a [Mongodb Query Filter](https://www.mongodb.com/docs/compass/current/query/filter)
4-
to a Postgres WHERE clause. Aiming to be simple and safe.
3+
This package converts [MongoDB query filters](https://www.mongodb.com/docs/compass/current/query/filter) into PostgreSQL WHERE clauses.
4+
_It's designed to be simple, secure, and free of dependencies._
55

6-
**Project State:** Just a rough sketch and playground for Erik and Koen.
6+
### Why Use This Package?
7+
When filtering data based on user-generated inputs, you need a syntax that's both intuitive and reliable. MongoDB's query filter is an excellent choice because it's simple, widely understood, and battle-tested in real-world applications. Although this package doesn't interact with MongoDB, it uses the same syntax to simplify filtering.
78

8-
## Example:
9+
### Supported Features:
10+
- Basics: `$eq`, `$ne`, `$gt`, `$gte`, `$lt`, `$lte`, `$regex`
11+
- Logical operators: `$and`, `$or`
12+
- Array operators: `$in`
13+
14+
This package is intended for use with PostgreSQL drivers like [github.com/lib/pq](https://github.com/lib/pq) and [github.com/jackc/pgx](https://github.com/jackc/pgx). However, it can work with any driver that supports the database/sql package.
15+
16+
17+
## Basic Usage:
18+
19+
Install the package in your project:
20+
```sh
21+
go get -u github.com/poki/mongodb-filter-to-postgres
22+
```
23+
24+
Basic example:
25+
```go
26+
import (
27+
"github.com/poki/mongodb-filter-to-postgres/filter"
28+
29+
"github.com/lib/pq" // also works with github.com/jackc/pgx
30+
)
31+
32+
func main() {
33+
// Create a converter with options:
34+
// - WithArrayDriver: to convert arrays to the correct driver type, required when using lib/pq
35+
converter := filter.NewConverter(filter.WithArrayDriver(pq.Array))
36+
37+
// Convert a filter query to a WHERE clause and values:
38+
input := []byte(`{"title": "Jurassic Park"}`)
39+
where, values, err := converter.Convert(input)
40+
if err != nil {
41+
// handle error
42+
}
43+
fmt.Println(where, values) // ("title" = $1), ["Jurassic Park"]
44+
45+
db, _ := sql.Open("postgres", "...")
46+
db.QueryRow("SELECT * FROM movies WHERE " + where, values...)
47+
}
48+
```
49+
(See [examples/](examples/) for more examples)
50+
51+
52+
## Complex filter example:
53+
54+
This project was created and designed for the
55+
[poki/netlib](https://github.com/poki/netlib) project, where we needed to
56+
convert complex filters from the API to SQL queries. The following example
57+
shows a complex filter and how it is converted to a WHERE clause and values:
958

10-
Let's filter some lobbies in a multiplayer game:
1159
```json5
1260
{
1361
"$and": [
@@ -38,8 +86,17 @@ AND (
3886
"playerCount" < $5
3987
)
4088
```
41-
And values:
4289
```go
43-
values := []any{"%aztec%", "%nuke%", "", 2, 10}
90+
values := []any{"aztec", "nuke", "", 2, 10}
4491
```
45-
(given "map" is confugired to be in a jsonb column)
92+
(given "customdata" is configured with `filter.WithNestedJSONB("customdata", "password", "playerCount")`)
93+
94+
95+
## Contributing
96+
97+
If you have a feature request or discovered a bug, we'd love to hear from you! Please open an issue or submit a pull request. This project adheres to the [Poki Vulnerability Disclosure Policy](https://poki.com/en/c/vulnerability-disclosure-policy).
98+
99+
## Main Contributors
100+
101+
- [Koen Bollen](https://github.com/koenbollen)
102+
- [Erik Dubbelboer](https://github.com/erikdubbelboer)

0 commit comments

Comments
 (0)