{"id":25043454,"url":"https://github.com/nmfr/sqlc-template","last_synced_at":"2025-07-13T11:43:14.481Z","repository":{"id":275023497,"uuid":"923608630","full_name":"NMFR/sqlc-template","owner":"NMFR","description":"A sqlc plugin that generates code from SQL into any language by using a user provided Golang template.","archived":false,"fork":false,"pushed_at":"2025-02-02T13:34:17.000Z","size":46,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-09T22:17:25.405Z","etag":null,"topics":["code-generation","golang-template","plugin","sql","sqlc","template"],"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/NMFR.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":"2025-01-28T14:57:26.000Z","updated_at":"2025-02-02T13:34:21.000Z","dependencies_parsed_at":"2025-01-30T19:25:21.877Z","dependency_job_id":"2e13f710-37ff-4ac5-85aa-6694ac0b8e0c","html_url":"https://github.com/NMFR/sqlc-template","commit_stats":null,"previous_names":["nmfr/sqlc-template"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NMFR%2Fsqlc-template","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NMFR%2Fsqlc-template/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NMFR%2Fsqlc-template/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NMFR%2Fsqlc-template/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NMFR","download_url":"https://codeload.github.com/NMFR/sqlc-template/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246393540,"owners_count":20769962,"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","golang-template","plugin","sql","sqlc","template"],"created_at":"2025-02-06T04:55:44.507Z","updated_at":"2025-03-30T23:25:14.956Z","avatar_url":"https://github.com/NMFR.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sqlc-template\n\nsqlc-template is a [sqlc](https://github.com/sqlc-dev/sqlc) plugin that generates code from SQL into any language by using a user provided [Golang template](https://pkg.go.dev/text/template).\nThis plugin was inspired by the [sqlc-gen-from-template](https://github.com/fdietze/sqlc-gen-from-template) plugin.\n\n## Usage\n\nTo use the plugin the following `options` must be defined in the [sqlc](https://github.com/sqlc-dev/sqlc) configuration file (usually named `sqlc.yaml`):\n\n-   `filename`: The generated code output file name.\n-   `template`: The [Golang template](https://pkg.go.dev/text/template).\n\nUsage example:\n\n`sqlc.yaml`:\n\n```yaml\nversion: \"2\"\nplugins:\n    - name: sqlc-template\n      wasm:\n          url: https://github.com/NMFR/sqlc-template/releases/download/v1.1.0/sqlc-template.wasm\n          sha256: b66ad58f7468aa1f14b4afe4b432ca405f4b64ea14e4562744e1c48adb1b3a43\nsql:\n    - engine: \"postgresql\"\n      queries: \"example/database/postgresql/query.sql\"\n      schema: \"example/database/postgresql/schema.sql\"\n      codegen:\n          - out: example/test/\n            plugin: sqlc-template\n            options:\n                filename: queries.yaml\n                template: |\n                    queries:\n                    {{- range .Queries }}\n                    - name: {{ .Name | ToLowerCamel }}\n                      cmd: {{ .Cmd }}\n                      text: |{{ .Text | trim | nindent 4 }}\n                      params: {{ if (eq (len .Params) 0) }}[]{{ end }}\n                      {{- range .Params }}\n                      - name: {{ .Column.Name | ToLowerCamel }}\n                        type: {{ .Column.Type.Name -}}\n                      {{ end }}\n                      columns: {{ if (eq (len .Columns) 0) }}[]{{ end }}\n                      {{- range .Columns }}\n                      - name: {{ .Name | ToLowerCamel }}\n                        type: {{ .Type.Name -}}\n                      {{ end }}\n                    {{ end -}}\n```\n\nRunning the `sqlc generate` command will create the following file:\n\n`queries.yaml`:\n\n```yaml\nqueries:\n    - name: getAuthor\n      cmd: :one\n      text: |\n          SELECT id, name, bio FROM authors\n          WHERE id = $1 LIMIT 1\n      params:\n          - name: id\n            type: bigserial\n      columns:\n          - name: id\n            type: bigserial\n          - name: name\n            type: text\n          - name: bio\n            type: text\n\n    - name: listAuthors\n      cmd: :many\n      text: |\n          SELECT id, name, bio FROM authors\n          ORDER BY name\n      params: []\n      columns:\n          - name: id\n            type: bigserial\n          - name: name\n            type: text\n          - name: bio\n            type: text\n\n    - name: createAuthor\n      cmd: :one\n      text: |\n          INSERT INTO authors (\n            name, bio\n          ) VALUES (\n            $1, $2\n          )\n          RETURNING id, name, bio\n      params:\n          - name: name\n            type: text\n          - name: bio\n            type: text\n      columns:\n          - name: id\n            type: bigserial\n          - name: name\n            type: text\n          - name: bio\n            type: text\n\n    - name: updateAuthor\n      cmd: :exec\n      text: |\n          UPDATE authors\n            set name = $2,\n            bio = $3\n          WHERE id = $1\n      params:\n          - name: id\n            type: bigserial\n          - name: name\n            type: text\n          - name: bio\n            type: text\n      columns: []\n\n    - name: deleteAuthor\n      cmd: :exec\n      text: |\n          DELETE FROM authors\n          WHERE id = $1\n      params:\n          - name: id\n            type: bigserial\n      columns: []\n```\n\n## Template\n\nThe template uses the [Golang template](https://pkg.go.dev/text/template) \"language\".\n\nThe data object available at the root of the template (`{{ . }}`) is the sqlc [`GenerateRequest`](internal/protos/plugin/codegen.pb.go#L967) object that provides access to the SQL schema, queries and some sqlc configuration fields.\n\nAll of the [sprig](https://masterminds.github.io/sprig/) functions are available to be called from within the template with the exception of:\n\n-   osBase\n-   osDir\n-   osClean\n-   osExt\n-   env\n-   expandenv\n-   kindOf\n-   kindIs\n-   typeOf\n-   typeIs\n-   typeIsLike\n-   deepEqual\n-   getHostByName\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnmfr%2Fsqlc-template","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnmfr%2Fsqlc-template","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnmfr%2Fsqlc-template/lists"}