|
1 | 1 | # mongodb-filter-to-postgres
|
2 | 2 |
|
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._ |
5 | 5 |
|
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. |
7 | 8 |
|
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: |
9 | 58 |
|
10 |
| -Let's filter some lobbies in a multiplayer game: |
11 | 59 | ```json5
|
12 | 60 | {
|
13 | 61 | "$and": [
|
|
38 | 86 | "playerCount" < $5
|
39 | 87 | )
|
40 | 88 | ```
|
41 |
| -And values: |
42 | 89 | ```go
|
43 |
| -values := []any{"%aztec%", "%nuke%", "", 2, 10} |
| 90 | +values := []any{"aztec", "nuke", "", 2, 10} |
44 | 91 | ```
|
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