yuriitatur/query-builder

A simple package to build query node tree

v1.0.1 2025-08-01 21:48 UTC

This package is auto-updated.

Last update: 2025-08-14 05:09:29 UTC


README

Quality Gate Status Coverage

Query builder

This is a simple AST nodes set that comes with a very simple query lexer and parser. It uses doctrine/lexer project to deal with text tokens.

Installation

Composer:

composer require yuriitatur/query-builder

Testing

composer test

Why?

This package is needed to abstract a query construction process when dealing with repositories. After building an ast tree, this tree will be passed to a corresponding db driver to perform actual query.

Syntax

Although this package is purposely designed to expose it as ast, as a side effect, it's possible to write queries as a string. It's very basic, don't use it in production. Example: FILTER={columnName = "some value"} ORDER={column desc, date asc} PAGINATION={page=1, limit=10} META={key="value"}. As you can see, it features 4 top-level keywords:

  • filter - defines a set of criteria, that can be bound together with and and or keywords, depending on context. It can be grouped with () to create more complex queries. The supported operators are = < > <= >= != in !in like !like
  • order - a list of columns followed by order asc or desc, separated by comma
  • pagination - a key-value pair of certain keywords like page, limit and offset that expects an integers with proper data. I know, using page and offset in the same context may seem odd, but this is only a language tokens parser, no more.
  • meta - a simple key-value storage for some query-specific stuff, e.g., a config for db driver.

License

This code is under MIT license, read more in the LICENSE file.