Lenra Query Parser
This repository provides a tool that can parse a JSON query into an AST tree and parse this tree into an Ecto query that can be executed within elixir.
Report Bug
ยท
Request Feature
Getting Started
Prerequisites
You need to add this lib into your phoenix app :
{:query_parser, git: "https://github.com/lenra-io/query-parser.git", tag: "v1.0.0-beta.X"}Parser
JSON to AST
With the parser you can parse a JSON query into an AST tree, for more information about the format of the JSON query consult this README. To parse JSON:
$\qquad$ Add the alias:
alias QueryParser.AST.Parser$\qquad$ Parse query with function:
Parser.from_json(q)$\qquad$ with q the JSON query
AST to Ecto
Once you parse a JSON query into an AST tree you will be able to parse this AST into an Ecto query:
$\qquad$ Add the alias:
alias QueryParser.AST.EctoParser$\qquad$ Parse query with function:
EctoParser.to_ecto(query, env_id, user_data_id)
$\qquad$ with:
$\qquad$ $\qquad$ - query the AST tree
$\qquad$ $\qquad$ - env_id the environement id
$\qquad$ $\qquad$ - user_data_id the id of the user's user data
you can now execute the query with ecto:
Repo.all(ecto_query)
Repo.one(ecto_query)with ecto_query the query is parsed before, the all function returns a list of all matching results, the one function returns only one element (beware: if there is more than one result the function returns an error), to learn more about the ecto repository function
Two step together:
$\qquad$ Add the alias:
alias QueryParser.AST.{EctoParser, Parser}$\qquad$ Parse query with function:
Query
|> Parser.from_json()
|> EctoParser.to_ecto(env_id, user_data_id)
|> Repo.all()Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please open an issue with the tag "enhancement" or "bug". Don't forget to give the project a star! Thanks again!
License
Distributed under the AGPL License. See LICENSE for more information.
Contact
Lenra - @lenra_dev - contact@lenra.io
Project Link: https://github.com/lenra-io/query-parser