{"id":23773370,"url":"https://github.com/beenotung/better-sql","last_synced_at":"2025-06-27T21:36:46.662Z","repository":{"id":49945899,"uuid":"516036528","full_name":"beenotung/better-sql","owner":"beenotung","description":"Generate sql query from a concise query syntax inspired from EdgeDB and GraphQL","archived":false,"fork":false,"pushed_at":"2024-03-13T17:53:12.000Z","size":148,"stargazers_count":26,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-09T11:56:19.263Z","etag":null,"topics":["code-generation","query-language","sql","syntax"],"latest_commit_sha":null,"homepage":"https://better-sql.surge.sh","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/beenotung.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":"2022-07-20T15:28:55.000Z","updated_at":"2024-07-27T22:10:46.000Z","dependencies_parsed_at":"2022-08-12T20:50:45.090Z","dependency_job_id":null,"html_url":"https://github.com/beenotung/better-sql","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/beenotung/better-sql","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beenotung%2Fbetter-sql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beenotung%2Fbetter-sql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beenotung%2Fbetter-sql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beenotung%2Fbetter-sql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beenotung","download_url":"https://codeload.github.com/beenotung/better-sql/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beenotung%2Fbetter-sql/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262337154,"owners_count":23296031,"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":["code-generation","query-language","sql","syntax"],"created_at":"2025-01-01T05:39:26.533Z","updated_at":"2025-06-27T21:36:46.627Z","avatar_url":"https://github.com/beenotung.png","language":"TypeScript","readme":"# better-sql.ts\n\nGenerate sql query from a concise query syntax inspired from [EdgeDB](https://www.edgedb.com/blog/edgedb-1-0) and [GraphQL](https://graphql.org/).\n\n[![npm Package Version](https://img.shields.io/npm/v/better-sql.ts)](https://www.npmjs.com/package/better-sql.ts)\n\nOnline Playground: https://better-sql.surge.sh\n\n\u003c!-- [![npm Package Downloads](https://img.shields.io/npm/dm/better-sql.ts)](https://www.npmtrends.com/better-sql-lang) --\u003e\n\n## Supported Features\n\n- [x] output typical sql query, compatible with mysql, postgres, sqlite, e.t.c.\n- [x] automatically add table name on columns if not specified\n- [x] inner join with nested `table {fields}`\n- [x] left join with nested `table [fields]`\n- [x] nested `select` sub-query\n- [x] `where` statement\n- [x] `having` statement\n- [x] `group by` statement\n- [x] aggregate function, e.g. `sum(score)`\n- [x] `order by` statement\n- [x] `limit` and `offset` statement\n\n## TypeScript Signature\n\n```typescript\nexport function queryToSQL(query: string): string\n```\n\n## Usage\n\n```typescript\nimport { queryToSQL } from 'better-sql.ts'\nimport { db } from './db'\n\nlet keyword = '%script%'\nlet query = 'select post [...] where title like :keyword'\nlet sql = queryToSQL(query)\nlet result = db.query(sql, { keyword })\n```\n\n## Example\n\n\u003ctable\u003e\n\u003ctbody\u003e\n\u003ctr\u003e\n\u003ctd\u003e\nA query in better-sql:\n\n```sql\nselect post [\n  id as post_id\n  title\n  author_id\n  user as author { nickname, avatar } where delete_time is null\n  type_id\n  post_type {\n    name as type\n    is_hidden\n  } where is_hidden = 0 or user.is_admin = 1\n] where created_at \u003e= :since\n    and delete_time is null\n    and title like :keyword\n  order by created_at desc\n  limit 25\n\n\n\n\n\n\n\n```\n\n\u003c/td\u003e\n\u003ctd\u003e\nis converted into formatted sql as below:\n\n```sql\nselect\n  post.id as post_id\n, post.title\n, post.author_id\n, author.nickname\n, author.avatar\n, post.type_id\n, post_type.name as type\n, post_type.is_hidden\nfrom post\ninner join user as author on author.id = post.author_id\ninner join post_type on post_type.id = post.post_type_id\nwhere author.delete_time is null\n  and (post_type.is_hidden = 0\n   or user.is_admin = 1)\n  and post.created_at \u003e= :since\n  and post.delete_time is null\n  and post.title like :keyword\norder by\n  post.created_at desc\nlimit 25\n```\n\n\u003c/td\u003e\n\u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\nDetails refers to [sample.ts](./test/sample.ts) and [lang.spec.ts](./test/lang.spec.ts)\n\n## License\n\nThis project is licensed with [BSD-2-Clause](./LICENSE)\n\nThis is free, libre, and open-source software. It comes down to four essential freedoms [[ref]](https://seirdy.one/2021/01/27/whatsapp-and-the-domestication-of-users.html#fnref:2):\n\n- The freedom to run the program as you wish, for any purpose\n- The freedom to study how the program works, and change it so it does your computing as you wish\n- The freedom to redistribute copies so you can help others\n- The freedom to distribute copies of your modified versions to others\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeenotung%2Fbetter-sql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeenotung%2Fbetter-sql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeenotung%2Fbetter-sql/lists"}