{"id":20801534,"url":"https://github.com/harish2704/knex-json-query","last_synced_at":"2025-06-12T17:31:56.965Z","repository":{"id":48602284,"uuid":"81320025","full_name":"harish2704/knex-json-query","owner":"harish2704","description":"A high-level utility which will will generate Knex query from a single JSON object.","archived":false,"fork":false,"pushed_at":"2021-07-18T19:30:10.000Z","size":20,"stargazers_count":20,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-08T21:51:47.639Z","etag":null,"topics":["knex","knexjs","query"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/harish2704.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-08T10:54:39.000Z","updated_at":"2024-02-17T16:17:02.000Z","dependencies_parsed_at":"2022-09-07T23:51:44.176Z","dependency_job_id":null,"html_url":"https://github.com/harish2704/knex-json-query","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/harish2704/knex-json-query","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harish2704%2Fknex-json-query","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harish2704%2Fknex-json-query/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harish2704%2Fknex-json-query/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harish2704%2Fknex-json-query/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/harish2704","download_url":"https://codeload.github.com/harish2704/knex-json-query/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harish2704%2Fknex-json-query/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259512027,"owners_count":22869328,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["knex","knexjs","query"],"created_at":"2024-11-17T18:18:53.855Z","updated_at":"2025-06-12T17:31:56.936Z","avatar_url":"https://github.com/harish2704.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/harish2704/knex-json-query.svg?branch=master)](https://travis-ci.org/harish2704/knex-json-query)\n# knex-json-query\nA high-level utility which will will generate Knex query from a single JSON object.\n\nit is a simple function with just ~80 lines of code.\n\n## Usage\n\n```javascript\nvar knex = require('knex');\n//var KnexJsonQuery = require('knex-json-query');\nvar KnexJsonQuery = require('./');\nvar jsonInput = {f1:{ $like: 'test%' }, $and: [{f2:22, f3: 33}] };\nvar queryBuilderFunction = KnexJsonQuery( jsonInput );\n\nvar sql = knex.where( queryBuilderFunction ).toString()\nconsole.log( sql );\n\n```\n\n**Output**\n```\nselect * where (\"f1\" LIKE 'test%' and ((\"f2\" = 22 and \"f3\" = 33)))\n```\n\n## Examples\n\nFor more details, see the [test.js](./test.js)\n\n**Examples from Unit test file:**\n\n1. simple and conditions\n    - *input JSON*: `{\"f1\":10,\"f2\":20,\"f3\":30}`\n    - *output SQL*: `select * where (\"f1\" = 10 and \"f2\" = 20 and \"f3\" = 30)`\n\n2. simple and conditions\n    - *input JSON*: `{\"firstName\":[[\"LIKE\",\"H%\"],[\"OR\",\"hemanth\"]]}`\n    - *output SQL*: `select * where ((\"firstName\" LIKE 'H%' or \"firstName\" = 'hemanth'))`\n\n3. regex query: MongoQuery\n    - *input JSON*: `{\"firstName\":{\"$regex\":\"H*\"}}`\n    - *output SQL*: `select * where (\"firstName\" REGEXP 'H*')`\n\n4. regex query and should omit $options: MongoQuery\n    - *input JSON*: `{\"firstName\":{\"$regex\":\"H*\",\"$options\":\"i\"}}`\n    - *output SQL*: `select * where (\"firstName\" REGEXP 'H*')`\n\n5. simple and conditions\n    - *input JSON*: `{\"f1\":[[\"A\"],[\"LIKE\",\"B\"]],\"f2\":20,\"f3\":30}`\n    - *output SQL*: `select * where ((\"f1\" = 'A' and \"f1\" LIKE 'B') and \"f2\" = 20 and \"f3\" = 30)`\n\n6. basic operators :: greater-than less-than\n    - *input JSON*: `{\"f1\":[\"\u003e\",10],\"f2\":[\"\u003c\",20],\"f3\":30}`\n    - *output SQL*: `select * where (\"f1\" \u003e 10 and \"f2\" \u003c 20 and \"f3\" = 30)`\n\n7. basic operators :: in\n    - *input JSON*: `{\"f1\":[\"IN\",[10,50]],\"f2\":[\"\u003c\",20],\"f3\":30}`\n    - *output SQL*: `select * where (\"f1\" in (10, 50) and \"f2\" \u003c 20 and \"f3\" = 30)`\n\n8. basic operators :: notin\n    - *input JSON*: `{\"f1\":[\"NOTIN\",[10,50]],\"f2\":[\"\u003c\",20],\"f3\":30}`\n    - *output SQL*: `select * where (\"f1\" not in (10, 50) and \"f2\" \u003c 20 and \"f3\" = 30)`\n\n9. multiple conditions in one field\n    - *input JSON*: `{\"f1\":[[\"LIKE\",20],21,[\"AND_BETWEEN\",[40,45]]],\"f2\":20,\"f3\":30}`\n    - *output SQL*: `select * where ((\"f1\" LIKE 20 and \"f1\" = 21 and \"f1\" between 40 and 45) and \"f2\" = 20 and \"f3\" = 30)`\n\n10. simple and condition with like statement\n    - *input JSON*: `{\"f1\":[\"LIKE\",20],\"f2\":20,\"f3\":30}`\n    - *output SQL*: `select * where (\"f1\" LIKE 20 and \"f2\" = 20 and \"f3\" = 30)`\n\n11. simple and condition with between statement\n    - *input JSON*: `{\"f1\":[\"BETWEEN\",[50,60]],\"f2\":20,\"f3\":30}`\n    - *output SQL*: `select * where (\"f1\" between 50 and 60 and \"f2\" = 20 and \"f3\" = 30)`\n\n12. simple and condition with in statement\n    - *input JSON*: `{\"f1\":[\"IN\",[50,60]],\"f2\":20,\"f3\":30}`\n    - *output SQL*: `select * where (\"f1\" in (50, 60) and \"f2\" = 20 and \"f3\" = 30)`\n\n13. simple and condition with not in statement\n    - *input JSON*: `{\"f1\":[\"NOTIN\",[50,60]],\"f2\":20,\"f3\":30}`\n    - *output SQL*: `select * where (\"f1\" not in (50, 60) and \"f2\" = 20 and \"f3\" = 30)`\n\n14. simple and condition with raw statement\n    - *input JSON*: `{\"f1\":{\"$raw\":\"@\u003e ANY(ARRAY(1,2,3))\"},\"f2\":20}`\n    - *output SQL*: `select * where (\"f1\" @\u003e ANY(ARRAY(1,2,3)) and \"f2\" = 20)`\n\n15. $and grouping\n    - *input JSON*: `{\"f1\":10,\"f2\":20,\"f3\":30,\"$and\":[{\"f4\":55},{\"f5\":66}]}`\n    - *output SQL*: `select * where (\"f1\" = 10 and \"f2\" = 20 and \"f3\" = 30 and ((\"f4\" = 55) or (\"f5\" = 66)))`\n\n16. simple or condition\n    - *input JSON*: `[{\"f1\":10},{\"f2\":20},{\"f3\":30}]`\n    - *output SQL*: `select * where ((\"f1\" = 10) or (\"f2\" = 20) or (\"f3\" = 30))`\n\n17. multiple conditions in one field\n    - *input JSON*: `{\"f1\":[[\"LIKE\",20],[\"OR\",21],[\"OR_BETWEEN\",[40,45]]],\"f2\":20,\"f3\":30}`\n    - *output SQL*: `select * where ((\"f1\" LIKE 20 or \"f1\" = 21 or \"f1\" between 40 and 45) and \"f2\" = 20 and \"f3\" = 30)`\n\n18. multiple conditions in one field: MongoQuery\n    - *input JSON*: `{\"f1\":{\"$like\":20,\"$or\":21,\"$or_between\":[40,45]},\"f2\":20,\"f3\":30}`\n    - *output SQL*: `select * where ((\"f1\" LIKE 20 or \"f1\" = 21 or \"f1\" between 40 and 45) and \"f2\" = 20 and \"f3\" = 30)`\n\n19. simple or condition with like statement\n    - *input JSON*: `[{\"f1\":[\"LIKE\",10]},{\"f2\":20},{\"f3\":30}]`\n    - *output SQL*: `select * where ((\"f1\" LIKE 10) or (\"f2\" = 20) or (\"f3\" = 30))`\n\n20. simple or condition with like statement: MongoQuery\n    - *input JSON*: `[{\"f1\":{\"$like\":10}},{\"f2\":20},{\"f3\":30}]`\n    - *output SQL*: `select * where ((\"f1\" LIKE 10) or (\"f2\" = 20) or (\"f3\" = 30))`\n\n21. simple or condition with between statement\n    - *input JSON*: `[{\"f1\":[\"BETWEEN\",[50,60]]},{\"f2\":20},{\"f3\":30}]`\n    - *output SQL*: `select * where ((\"f1\" between 50 and 60) or (\"f2\" = 20) or (\"f3\" = 30))`\n\n22. simple or condition with between statement :MongoQuery\n    - *input JSON*: `[{\"f1\":{\"$between\":[50,60]}},{\"f2\":20},{\"f3\":30}]`\n    - *output SQL*: `select * where ((\"f1\" between 50 and 60) or (\"f2\" = 20) or (\"f3\" = 30))`\n\n23. simple or condition with in statement\n    - *input JSON*: `[{\"f1\":[\"IN\",[50,60]]},{\"f2\":20},{\"f3\":30}]`\n    - *output SQL*: `select * where ((\"f1\" in (50, 60)) or (\"f2\" = 20) or (\"f3\" = 30))`\n\n24. simple or condition with not in statement\n    - *input JSON*: `[{\"f1\":[\"NOTIN\",[50,60]]},{\"f2\":20},{\"f3\":30}]`\n    - *output SQL*: `select * where ((\"f1\" not in (50, 60)) or (\"f2\" = 20) or (\"f3\" = 30))`\n\n25. simple or condition with in statement :MongoQuery\n    - *input JSON*: `[{\"f1\":{\"$in\":[50,60]}},{\"f2\":20},{\"f3\":30}]`\n    - *output SQL*: `select * where ((\"f1\" in (50, 60)) or (\"f2\" = 20) or (\"f3\" = 30))`\n\n26. simple or condition with nin statement :MongoQuery\n    - *input JSON*: `[{\"f1\":{\"$nin\":[50,60]}},{\"f2\":20},{\"f3\":30}]`\n    - *output SQL*: `select * where ((\"f1\" not in (50, 60)) or (\"f2\" = 20) or (\"f3\" = 30))`\n\n27. simple or condition with raw statement\n    - *input JSON*: `[{\"f1\":{\"$raw\":\"@\u003e ANY(ARRAY(1,2,3))\"}},{\"f2\":20}]`\n    - *output SQL*: `select * where ((\"f1\" @\u003e ANY(ARRAY(1,2,3))) or (\"f2\" = 20))`\n\n28. simple or condition with conditional array\n    - *input JSON*: `{\"f1\":[[\"ILIKE\",\"awesome\"],[\"OR_ILIKE\",\"%super%\"]]}`\n    - *output SQL*: `select * where ((\"f1\" ILIKE 'awesome' or \"f1\" ILIKE '%super%'))`\n\n29. $or grouping\n    - *input JSON*: `{\"f1\":10,\"f2\":20,\"f3\":30,\"$or\":[{\"f4\":55},{\"f5\":66}]}`\n    - *output SQL*: `select * where (\"f1\" = 10 and \"f2\" = 20 and \"f3\" = 30 or ((\"f4\" = 55) or (\"f5\" = 66)))`\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fharish2704%2Fknex-json-query","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fharish2704%2Fknex-json-query","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fharish2704%2Fknex-json-query/lists"}