{"id":24319530,"url":"https://github.com/ryym/geq","last_synced_at":"2026-05-26T20:31:31.172Z","repository":{"id":192225501,"uuid":"685566042","full_name":"ryym/geq","owner":"ryym","description":"Yet another query builder for Go with moderate type safety.","archived":false,"fork":false,"pushed_at":"2023-10-25T00:25:10.000Z","size":351,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-10T20:44:41.412Z","etag":null,"topics":["go","sql"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ryym.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2023-08-31T14:13:29.000Z","updated_at":"2023-09-10T05:15:53.000Z","dependencies_parsed_at":"2023-09-27T20:04:46.696Z","dependency_job_id":"3156b8de-aa9e-4ceb-9228-aa67c001c5eb","html_url":"https://github.com/ryym/geq","commit_stats":null,"previous_names":["ryym/geq"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ryym/geq","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryym%2Fgeq","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryym%2Fgeq/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryym%2Fgeq/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryym%2Fgeq/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ryym","download_url":"https://codeload.github.com/ryym/geq/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryym%2Fgeq/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33538659,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"ssl_error","status_checked_at":"2026-05-26T15:22:15.568Z","response_time":63,"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":["go","sql"],"created_at":"2025-01-17T15:33:40.173Z","updated_at":"2026-05-26T20:31:31.157Z","avatar_url":"https://github.com/ryym.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"🚧 WIP\n\n# Geq\n\nYet another SQL query builder for Go, with moderate type safety powered by generics and code generation.\n\n```go\nq := geq.SelectFrom(d.Users).Where(d.Users.Name.Eq(\"foo\")).OrderBy(d.Users.ID)\nusers, err := q.Load(ctx, db)\nfmt.Println(users, err)\n```\n\n## Features\n\n- SQL friendly (query builder rather than ORM)\n- Performative (no runtime reflections)\n\nUnsupported:\n\n- Schema migration\n- Fixture file loading\n\n## Quick start\n\n### Hello world\n\nAs a first step, you can try using Geq without any database or code generation.\n\n```bash\nmkdir geqsample \u0026\u0026 cd geqsample\ngo mod init example.com/geqsample\n```\n\n`main.go`:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/ryym/geq\"\n)\n\nfunc main() {\n\tq := geq.Select(geq.Raw(\"*\")).From(geq.Raw(\"users\")).Where(geq.Raw(\"name\").Eq(\"foo\"))\n\tsql, err := q.Build()\n\tfmt.Println(sql.Query, sql.Args, err)\n}\n```\n\n```bash\n% go mod tidy\n% go run .\nSELECT * FROM users WHERE name = ? [foo] \u003cnil\u003e\n```\n\nThis may be somewat useful already, but defining a data model makes it more convenient and safe for writing queries.\n\n### Define data model\n\nDefine a struct that corresponds to records in your database tables, and write it in `geqbld.go` .\nThis is a configuration file for Geq.\n\n`geqbld.go`:\n\n```go\npackage main\n\nimport \"example.com/geqsample/mdl\"\n\ntype GeqTables struct {\n\tUsers mdl.User\n}\n```\n\n`mdl/models.go`:\n\n```go\npackage mdl\n\ntype User struct {\n    ID   uint64\n    Name string\n}\n```\n\n```bash\ngo install github.com/ryym/geq/cmd/geq@latest\ngeq .\n```\n\nThe above command generates a query helper package in `./d` by default.\nNow you can rewrite the query in `main.go` like this:\n\n```diff\n  import (\n  \t\"fmt\"\n  \n+ \t\"example.com/geqsample/d\"\n  \t\"github.com/ryym/geq\"\n  )\n func main() {\n-\tq := geq.Select(geq.Raw(\"*\")).From(geq.Raw(\"users\")).Where(geq.Raw(\"name\").Eq(\"foo\"))\n+\tq := geq.SelectFrom(d.Users).Where(d.Users.Name.Eq(\"foo\"))\n \tsql, err := q.Build()\n \tfmt.Println(sql.Query, sql.Args, err)\n }\n```\n\n```bash\n% go run .\nSELECT users.id, users.name FROM users WHERE users.name = ? [\"foo\"] \u003cnil\u003e\n```\n\n### Run query\n\nFinally you can actually execute the query and retrieve records once you have a database corresponding to the definitions in `geqbld.go` .\nLet's try it with PostgreSQL.\n\n`docker-compose.yml`:\n\n```yml\nversion: '3'\nservices:\n  pg:\n    image: postgres:15.4\n    ports:\n      - '5499:5432'\n    environment:\n      - POSTGRES_USER=geqsample\n      - POSTGRES_PASSWORD=geqsample\n```\n\n`main.go`:\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"database/sql\"\n\t\"fmt\"\n\n\t\"example.com/geqsample/d\"\n\t_ \"github.com/lib/pq\"\n\t\"github.com/ryym/geq\"\n)\n\nconst initSQL = `\nDROP TABLE IF EXISTS users;\nCREATE TABLE users (id serial NOT NULL, name varchar(30) NOT NULL);\nINSERT INTO users VALUES (1, 'foo'), (2, 'bar'), (3, 'foo');\n`\n\nfunc main() {\n\t// Connect to DB.\n\tdb, err := sql.Open(\"postgres\", \"port=5499 user=geqsample password=geqsample sslmode=disable\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tdefer db.Close()\n\n\t// Prepare the data.\n\t_, err = db.Exec(initSQL)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Specify the database type for query building.\n\tgeq.SetDefaultDialect(\u0026geq.DialectPostgres{})\n\n\t// Write a query.\n\tq := geq.SelectFrom(d.Users).Where(d.Users.Name.Eq(\"foo\")).OrderBy(d.Users.ID)\n\n\t// Load the data.\n\tusers, err := q.Load(context.Background(), db)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tfmt.Printf(\"%#+v\\n\", users) // users: []mdl.User\n}\n```\n\n```bash\n% docker-compose up --build -d\n% go mod tidy\n% go run .\n[]mdl.User{mdl.User{ID:0x1, Name:\"foo\"}, mdl.User{ID:0x3, Name:\"foo\"}}\n```\n\n# Guides\n\n## Table relationships management\n\nOptionally you can define and utilize table relationships.\n\n`mdl/models.go`:\n\n```go\npackage mdl\n\ntype User struct {\n\tID   uint64\n\tName string\n}\n\n// User has-many Posts (users.id = posts.author_id)\ntype Post struct {\n\tID       uint64\n\tAuthorID uint64\n\tTitle    string\n}\n```\n\n`geqbld.go`:\n\n```go\npackage main\n\nimport \"example.com/geqsample/mdl\"\n\ntype GeqTables struct {\n\tUsers mdl.User\n\tPosts mdl.Post\n}\n\n// Define table relationships here.\ntype GeqRelationships struct {\n\tUsers struct {\n\t\tPosts mdl.Post `geq:\"Users.ID = Posts.AuthorID\"`\n\t}\n\tPosts struct {\n\t\tAuthor mdl.User `geq:\"Posts.AuthorID = Users.ID\"`\n\t}\n}\n```\n\nThe relationship definitions make it easier to build join queries or load relevant data.\n\n```go\n// Use in data loading.\ngeq.SelectFrom(d.Posts).Where(d.Posts.Author.In(users))\n\n// Use in table join.\ngeq.SelectFrom(d.Users).Joins(d.Users.Posts).Where(d.Users.Posts.T().Title.Eq(\"\"))\n```\n\n### Relevant models retrieval\n\nUnlike typical ORM libraries, Geq does not support nested relation loading such as below:\n\n```go\ntype User struct {\n    ID    uint64\n    Name  string\n    Posts []mdl.Post // NOT supported\n}\n```\n\nNested relation loading is powerful but sometimes make things so complicated.\nInstead, you can load relevant records in two ways:\n\n#### Load by one query\n\n```go\nvar posts []mdl.Post\nvar userMap map[int64]mdl.User\n\nerr := geq.SelectFrom(d.Posts).Joins(d.Posts.Author).OrderBy(d.Posts.ID).WillScan(\n\tgeq.ToSlice(d.Posts, \u0026posts),\n\tgeq.ToMap(d.Posts.Author, d.Posts.Author.T().ID, \u0026userMap),\n).Load(ctx, db)\n\nfor _, p := range posts {\n\tauthor := userMap[p.AuthorID]\n\tfmt.Println(p, author)\n}\n```\n\n- It requires only one round-trip to the database.\n- It may load duplicate records if the relationship is not 1:1.\n\n#### Load individually\n\n```go\nusers, err := geq.SelectFrom(d.Users).OrderBy(d.Users.ID).Limit(50).Load(ctx, db)\n\npostsMap, err := geq.AsSliceMap(\n\td.Posts.ID,\n\tgeq.SelectFrom(d.Posts).Where(d.Posts.Author.In(users)).OrderBy(d.Posts.ID),\n).Load(ctx, db)\n\nfor _, u := range users {\n\tposts := postsMap[u.ID]\n\tfmt.Println(u, posts)\n}\n```\n\n- It requires multiple round-trips to the database.\n- It retrieves records without duplicate due to table joining.\n\n## Non-table result mapping\n\nWhen you want to load rows not corresponding to database tables, you generate row mappers.\n\n```go\npackage mdl\n\ntype PostStat struct {\n\tAuthorID  int64\n\tPostCount int64\n}\n```\n\nDefine `GeqMappers` in `geqbld.go`:\n\n```go\npackage main\n\n// ...\n\ntype GeqMappers struct {\n    PostStats mdl.PostStat\n}\n```\n\n```bash\n# Re-generate your query helper with row mappers.\ngeq .\n```\n\nThen you can load results into `mdl.PostStat` using `SelectAs` .\n\n```go\nq := geq.SelectAs(\u0026d.PostStats{\n\t// Specify what you want to load for each field.\n\tAuthorID: d.Posts.AuthorID,\n\tPostCount: geq.Count(d.Posts.ID),\n}).From(d.Posts).GroupBy(d.Posts.AuthorID)\n\n// stats: []mdl.PostStat\nstats, err := q.Load(ctx, db)\n```\n\nOr when you want to select single values, use `SelectOnly` .\n\n```go\n// userIDs: []uint64\nuserIDs, err := geq.SelectOnly(d.Users.ID).OrderBy(d.Users.ID).Load(ctx, db)\n```\n\n## Data retrieval patterns\n\nYou can retrieve rows in various way by combining them:\n\n- Specify row type\n    - `SelectFrom` ... table record\n    - `SelectAs` ... non-table record\n    - `SelectOnly` ... single value\n- Specify data structure\n    - `query.Load` ... slice of rows\n    - `AsMap(key, query).Load` ... map of rows\n    - `AsSliceMap(key, query).Load` ... map of slice of rows\n\nExamples:\n\n```go\n// []User, error\nusers, err := geq.SelectFrom(d.Users).OrderBy(d.Users.ID).Load(ctx, db)\n\n// []PostStat, error\nstats, err := geq.SelectAs(\u0026d.PostStats{\n\tAuthorID: d.Posts.AuthorID,\n\tPostCount: geq.Count(d.Posts.ID),\n}).From(d.Posts).GroupBy(d.Posts.AuthorID).Load(ctx, db)\n\n// map[uint64]User, error\nuserMap, err := geq.AsMap(d.Users.ID, geq.SelectFrom(d.Users)).Load(ctx, db)\n\n// map[uint64][]string, error\nnamesMap, err := geq.AsSliceMap(\n\td.Users.ID,\n\tgeq.SelectOnly(d.Users.Name).OrderBy(d.Users.Name),\n).Load(ctx, db)\n```\n\n### Retrieve multiple results\n\nYou can also retrieve multiple results at once by scanning:\n\n```go\nvar posts []mdl.Post\nvar userMap map[int64]mdl.User\n\nerr := geq.SelectFrom(d.Posts).Joins(d.Posts.Author).OrderBy(d.Posts.ID).WillScan(\n\tgeq.ToSlice(d.Posts, \u0026posts),\n\tgeq.ToMap(d.Posts.Author, d.Posts.Author.T().ID, \u0026userMap),\n).Load(ctx, db)\n\nfor _, p := range posts {\n\tauthor := userMap[p.AuthorID]\n\tfmt.Println(p, author)\n}\n```\n\n### Other utilities\n\n`LoadRows` - Load as `*sql.Rows`:\n\n```go\n// *sql.Rows, error\nrows, err = geq.SelectFrom(d.Users).LoadRows(ctx, db)\n```\n\n`Select` - Use sub queries:\n\n```go\ngeq.SelectFrom(d.Users).Where(\n\td.Users.ID.InAny(geq.Select(d.Posts.AuthorID).From(d.Posts)),\n)\n```\n\n`SelectVia` - Filter by prefetched rows via table relationship:\n\n```go\nusers, err := geq.SelectFrom(d.Users).Load(ctx, db)\n\n// Same as: geq.SelectFrom(d.Posts).Where(d.Posts.Author.In(users)).Load(ctx, db)\nposts, err := geq.SelectVia(users, d.Posts, d.Posts.Author).Load(ctx, db)\n\nconfigMap, err := geq.AsMap(\n\td.Configs.UserID,\n\tgeq.SelectVia(users, d.Configs, d.Configs.User),\n).Load(ctx, db)\n\nfor _, u := range users {\n\tfmt.Println(u.Name, configMap[u.ID])\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryym%2Fgeq","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryym%2Fgeq","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryym%2Fgeq/lists"}