{"id":27301116,"url":"https://github.com/cadawg/gormqueryfromjson","last_synced_at":"2026-05-05T21:33:06.114Z","repository":{"id":252308345,"uuid":"840048169","full_name":"CADawg/GormQueryFromJson","owner":"CADawg","description":"Gorm Query from JSON allows you to take a JSON query and convert it into SQL via GORM","archived":false,"fork":false,"pushed_at":"2024-08-10T17:26:23.000Z","size":24,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-03-01T18:51:58.235Z","etag":null,"topics":["gorm","json","jsonquery","jsontosql","mongodb","query","sql"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/CADawg.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-08-08T21:36:01.000Z","updated_at":"2024-08-10T17:26:26.000Z","dependencies_parsed_at":"2024-08-08T23:43:03.275Z","dependency_job_id":"02a620fe-9029-4135-98f1-63f87731707b","html_url":"https://github.com/CADawg/GormQueryFromJson","commit_stats":null,"previous_names":["cadawg/gormqueryfromjson"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/CADawg/GormQueryFromJson","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CADawg%2FGormQueryFromJson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CADawg%2FGormQueryFromJson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CADawg%2FGormQueryFromJson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CADawg%2FGormQueryFromJson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CADawg","download_url":"https://codeload.github.com/CADawg/GormQueryFromJson/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CADawg%2FGormQueryFromJson/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32669428,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-05T11:29:49.557Z","status":"ssl_error","status_checked_at":"2026-05-05T11:29:48.587Z","response_time":54,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["gorm","json","jsonquery","jsontosql","mongodb","query","sql"],"created_at":"2025-04-12T01:35:25.039Z","updated_at":"2026-05-05T21:33:06.099Z","avatar_url":"https://github.com/CADawg.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Gorm Query from JSON\n\nWant your users to be able to perform advanced searches on your database without all the hassle of handling each individual input - just provide a schema and the library will validate that your users are only doing queries you permit.\n\n# Install\n\n`go get github.com/CADawg/GormQueryFromJson`\n\n# Usage\n\nSet maximum levels of recursion\n\n```go\nGormQueryFromJson.MaxDepth = 10\n```\n\nDefine a schema\n```go\nvar mainSchema = []GormQueryFromJson.AcceptableQueryTypes{\n\t{\n\t\tColumnName: \"id\",\n\t\tQueryTypes: []query.TypeIdentifier{GormQueryFromJson.TypeNumber},\n\t\tLimit:      1,\n\t},\n\t{\n\t\tColumnName: \"actor\",\n\t\tQueryTypes: []query.TypeIdentifier{GormQueryFromJson.TypeString, GormQueryFromJson.TypeStrictString},\n\t\tLimit:      10,\n\t},\n\t{\n\t\tColumnName: \"target\",\n\t\tQueryTypes: []query.TypeIdentifier{GormQueryFromJson.TypeString, GormQueryFromJson.TypeStrictString},\n\t\tLimit:      10,\n\t}\n}\n```\n\n- Limit sets the maximum times this column can be targeted in a query.\n- QueryTypes defines what queries can be run against the column\n    - Or Is not a type of query for this purpose, only its children count\n\nDefine an endpoint like so\n\n```go\nhttp.HandleFunc(\"POST /testQuery\", func(res http.ResponseWriter, req *http.Request) {\n    var queries []query.JSON\n\n    err := json.NewDecoder(req.Body).Decode(\u0026queries)\n\n    if err != nil {\n        http.Error(res, fmt.Sprintf(\"Error: %v\", err), http.StatusBadRequest)\n        return\n    }\n\n    statement := db.ToSQL(func(tx *gorm.DB) *gorm.DB {\n        // use gorm to create a sql query and print out the statement\n        tx = tx.Model(\u0026types.HistoryEntry{}).Limit(100)\n\n        // !!! This is our function\n        tx, _, err = GormQueryFromJson.DoQuery(queries, tx, mainSchema)\n\n        return tx.Find(\u0026[]types.HistoryEntry{})\n    })\n\n    if err != nil {\n        http.Error(res, fmt.Sprintf(\"Error: %v\", err), http.StatusBadRequest)\n        return\n    }\n\n    _, err = res.Write([]byte(statement))\n})\n```\n\nYou can use it however you like, just make sure that you have not placed any existing where conditions on the query. You can do so after calling DoQuery\n\n# Queries (User Side)\n\n```json\n[\n    {\n        \"type\": \"or\",\n        \"query\": [\n            {\n                \"type\": \"string\",\n                \"query\": {\n                    \"column\": \"target\",\n                    \"contains\": \"user2\"\n                }\n            },\n            {\n                \"type\": \"string\",\n                \"query\": {\n                    \"column\": \"target\",\n                    \"contains\": \"user1\"\n                }\n            }\n        ]\n    },\n    {\n        \"type\": \"or\",\n        \"query\": [\n            {\n                \"type\": \"string\",\n                \"query\": {\n                    \"column\": \"actor\",\n                    \"contains\": \"user3\"\n                }\n            },\n            {\n                \"type\": \"string\",\n                \"query\": {\n                    \"column\": \"target\",\n                    \"contains\": \"user4\"\n                }\n            }\n        ]\n    },\n    {\n        \"type\": \"number\",\n        \"query\": {\n            \"column\": \"id\",\n            \"greaterThanOrEqual\": 5000000\n        }\n    }\n]\n```\n\n-\u003e SQL (Where Clauses generated by above)\n\n```sql\nSELECT * FROM `table` WHERE (target LIKE '%user2%' OR target LIKE '%user1%') AND (actor LIKE '%user3%' OR target LIKE '%user4%') AND id \u003e= 5000000\n```\n\nIf any columns you have not allowed are included or the amount is above the limit, those where clauses will be discarded.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcadawg%2Fgormqueryfromjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcadawg%2Fgormqueryfromjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcadawg%2Fgormqueryfromjson/lists"}