{"id":17042624,"url":"https://github.com/fdietze/sqlc-gen-from-template","last_synced_at":"2025-04-12T14:50:46.131Z","repository":{"id":251603901,"uuid":"837740737","full_name":"fdietze/sqlc-gen-from-template","owner":"fdietze","description":"sqlc plugin to generate type-safe code for SQL queries using a template.","archived":false,"fork":false,"pushed_at":"2025-03-04T13:06:00.000Z","size":31,"stargazers_count":16,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-04T22:45:36.755Z","etag":null,"topics":["sql","sqlc"],"latest_commit_sha":null,"homepage":"","language":"Go","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/fdietze.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}},"created_at":"2024-08-03T22:08:46.000Z","updated_at":"2025-03-20T10:55:15.000Z","dependencies_parsed_at":null,"dependency_job_id":"d75f25f0-5e33-4458-ac95-2becf1be625e","html_url":"https://github.com/fdietze/sqlc-gen-from-template","commit_stats":null,"previous_names":["fdietze/sqlc-gen-from-template"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fdietze%2Fsqlc-gen-from-template","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fdietze%2Fsqlc-gen-from-template/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fdietze%2Fsqlc-gen-from-template/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fdietze%2Fsqlc-gen-from-template/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fdietze","download_url":"https://codeload.github.com/fdietze/sqlc-gen-from-template/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248586161,"owners_count":21128982,"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":["sql","sqlc"],"created_at":"2024-10-14T09:18:04.189Z","updated_at":"2025-04-12T14:50:46.125Z","avatar_url":"https://github.com/fdietze.png","language":"Go","readme":"# sqlc-gen-from-template\n\n[sqlc](https://sqlc.dev/) plugin to generate type-safe code for SQL queries using a template. More specifically, it provides the [protobuf data structures provided by sqlc](https://github.com/sqlc-dev/sqlc/blob/main/protos/plugin/codegen.proto) to a [go template](https://pkg.go.dev/text/template) to turn [every query in a SQL file](https://docs.sqlc.dev/en/stable/tutorials/getting-started-sqlite.html#schema-and-queries) into a function and return type data structure of your preferred programming language and database library.\n\n## Installation\n\nI recommend using [devbox](https://www.jetpack.io/devbox) and installing this plugin into your project using a [nix flake](https://zero-to-nix.com/concepts/flakes):\n\n```bash\n# replace \u003crev\u003e with the latest commit hash from this repo\ndevbox add sqlc github:fdietze/sqlc-gen-from-template/\u003crev\u003e\n```\n\nAlternatively, you can build a binary yourself and put it into your `PATH`:\n\n```bash\ngo build\n```\n\nAfterwards you can generate code with:\n```bash\nsqlc generate\n```\n\n## Contributions\n\nDon't hesitate to ask any questions or present your ideas in the issues. Contributions are welcome!\n\n## Language Support\n\nSee the [templates](./templates) directory for supported languages and database library combinations.\n\n## Usage\n\nExample usage for [Scala](https://www.scala-lang.org/) with the [magnum](https://github.com/AugustNagro/magnum) database library:\n\n`sqlc.yml`\n\n```yml\nversion: \"2\"\nplugins:\n- name: sqlc-gen-from-template\n  process:\n    cmd: sqlc-gen-from-template # https://github.com/fdietze/sqlc-gen-from-template\nsql:\n  - engine: \"sqlite\"\n    queries: \"queries.sql\"\n    schema: \"schema.sql\"\n    codegen:\n    - out: backend/src/backend/queries\n      plugin: sqlc-gen-from-template\n      options:\n        template: \"query_template.go.tmpl\"\n        filename: \"Queries.scala\"\n        # optional formatter command to format generated code\n        formatter_cmd: \".devbox/nix/profile/default/bin/scalafmt --stdin\"\n```\n\n`schema.sql`\n\n```sql\ncreate table post(\n  id integer primary key autoincrement -- rowid\n  , parent_id integer\n) strict;\n\n```\n\n`queries.sql`\n\n```sql\n-- name: getReplyIds :many\nselect id\nfrom post\nwhere parent_id = ?;\n```\n\n`query_template.go.tmpl`\n\n```tmpl\n{{- /* \nhttps://pkg.go.dev/text/template\nhttps://github.com/sqlc-dev/sqlc/blob/main/protos/plugin/codegen.proto\nhttps://github.com/AugustNagro/magnum?tab=readme-ov-file\n*/ -}}\n\n{{- define \"ScalaType\" -}}\n{{- $scalaType := .Type.Name -}}\n{{- if eq .Type.Name \"INTEGER\"}}{{ $scalaType = \"Long\" }}\n{{- else if eq .Type.Name \"TEXT\"}}{{ $scalaType = \"String\" }}\n{{- end -}}\n{{- $scalaType }}\n{{- end -}}\n\npackage backend.queries\n\nimport com.augustnagro.magnum\nimport com.augustnagro.magnum.*\n\n{{- range .Queries }}\n\n{{range .Comments}}// {{.}}\n{{end}}\n\n{{$rowType := printf \"Row_%s\" .Name -}}\n{{- if or (eq .Cmd \":many\") (eq .Cmd \":one\") }}\n  {{- if gt (len .Columns) 1 -}}\n    case class {{ $rowType }}({{- range .Columns}}\n    {{.Name}}:\n    {{- if not .NotNull }}Option[{{end}}\n    {{- template \"ScalaType\" .}}\n    {{- if not .NotNull }}]{{end}},\n    {{- end}}\n)\n  {{- else -}}\n\n\n    type {{ $rowType }} = \n    {{- if not (index .Columns 0).NotNull }}Option[{{end}}\n    {{- template \"ScalaType\" (index .Columns 0) }}\n    {{- if not (index .Columns 0).NotNull }}]{{end}}\n  {{- end}}\n\n{{end}}\n\n\n{{- $returnType := \"__DEFAULT__\" -}}\n{{- if eq .Cmd \":exec\" }}\n  {{- $returnType = \"Unit\" -}}\n{{- else if eq .Cmd \":many\" }}\n  {{- $returnType = printf \"Vector[%s]\" $rowType -}}\n{{- else if eq .Cmd \":one\" }}\n  {{- $returnType = $rowType -}}\n{{- else -}}\n  {{- $returnType = \"__UNKNOWN_QUERY_ANNOTATION__\" -}}\n{{- end -}}\n\n\ndef {{.Name}}({{range .Params}}\n  {{.Column.Name}}:{{template \"ScalaType\" .Column}},\n{{- end}}\n)(using con: DbCon): {{ $returnType }} = {\n  Frag(\"\"\"\n  {{ .Text }}\n  \"\"\", params = IArray({{range .Params}}\n  {{.Column.Name}},\n  {{end}}))\n  {{- if eq .Cmd \":exec\" }}.update.run(){{end}}\n  {{- if eq .Cmd \":many\" }}.query[{{ $rowType }}].run(){{end}}\n  {{- if eq .Cmd \":one\" }}.query[{{ $rowType }}].run().head{{end}}\n}\n\n{{- end -}}\n```\n\nRunning `sqlc generate` generates:\n\n`Queries.scala`\n\n```scala\npackage backend.queries\n\nimport com.augustnagro.magnum\nimport com.augustnagro.magnum.*\n\ntype Row_getReplyIds = Long\n\ndef getReplyIds(\n  parent_id: Long\n)(using con: DbCon): Vector[Row_getReplyIds] = {\n  Frag(\n    \"\"\"\n  \nselect id\nfrom post\nwhere parent_id = ?\n  \"\"\",\n    params = IArray(\n      parent_id\n    ),\n  ).query[Row_getReplyIds].run()\n}\n```\n\n# Related Projects\n\n- [cornerman/scala-db-codegen](https://github.com/cornerman/scala-db-codegen)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffdietze%2Fsqlc-gen-from-template","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffdietze%2Fsqlc-gen-from-template","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffdietze%2Fsqlc-gen-from-template/lists"}