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
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"}
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)
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)
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
$\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()
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!
Distributed under the AGPL License. See LICENSE for more information.
Lenra - @lenra_dev - contact@lenra.io
Project Link: https://github.com/lenra-io/query-parser