https://github.com/pgx-contrib/pgxmql
pgx QueryRewriter that injects hashicorp/mql dynamic filters as safe, parameterised WHERE clauses
https://github.com/pgx-contrib/pgxmql
dynamic-filtering go golang hashicorp mql pgx postgres postgresql query-rewriter sqlc where-clause
Last synced: about 1 month ago
JSON representation
pgx QueryRewriter that injects hashicorp/mql dynamic filters as safe, parameterised WHERE clauses
- Host: GitHub
- URL: https://github.com/pgx-contrib/pgxmql
- Owner: pgx-contrib
- License: mit
- Created: 2025-02-23T06:07:01.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2026-04-27T05:35:02.000Z (about 1 month ago)
- Last Synced: 2026-04-27T06:32:26.909Z (about 1 month ago)
- Topics: dynamic-filtering, go, golang, hashicorp, mql, pgx, postgres, postgresql, query-rewriter, sqlc, where-clause
- Language: Go
- Homepage: https://pkg.go.dev/github.com/pgx-contrib/pgxmql
- Size: 273 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pgxmql
[](https://github.com/pgx-contrib/pgxmql/actions/workflows/ci.yml)
[](https://github.com/pgx-contrib/pgxmql/releases)
[](https://pkg.go.dev/github.com/pgx-contrib/pgxmql)
[](LICENSE)
[](https://go.dev)
[](https://github.com/jackc/pgx)
[](https://github.com/hashicorp/mql)
A [pgx](https://github.com/jackc/pgx) `QueryRewriter` adapter for
[hashicorp/mql](https://github.com/hashicorp/mql) dynamic filtering. Write
your SQL with a void placeholder and let `WhereClause` inject a safe,
parameterised filter at query time.
## Installation
```bash
go get github.com/pgx-contrib/pgxmql
```
## Usage
Define your model with `db` struct tags to map fields to table columns. Use
`$1::void IS NULL` as the filter placeholder in your SQL:
```go
type User struct {
ID int `db:"id"`
Name string `db:"name"`
Role string `db:"role"`
}
rows, err := pool.Query(ctx,
"SELECT * FROM users WHERE $1::void IS NULL",
&pgxmql.WhereClause{
Condition: "role = 'admin'",
Model: User{},
},
)
```
The placeholder is replaced by the parsed MQL condition before the query
reaches PostgreSQL. When `Condition` is empty the placeholder becomes `TRUE`,
returning all rows.
## Development
### DevContainer
Open in VS Code with the Dev Containers extension. The environment provides Go,
PostgreSQL 18, and Nix automatically.
```
PGX_DATABASE_URL=postgres://vscode@postgres:5432/pgxmql?sslmode=disable
```
### Nix
```bash
nix develop # enter shell with Go
go tool ginkgo run -r
```
### Run tests
```bash
# Unit tests only (no database required)
go tool ginkgo run -r
# With integration tests
export PGX_DATABASE_URL="postgres://localhost/pgxmql?sslmode=disable"
go tool ginkgo run -r
```
## License
[MIT](LICENSE)