|
1 |
| -## Usage |
| 1 | +# sqlc-gen-go |
2 | 2 |
|
3 | 3 | > [!IMPORTANT]
|
4 | 4 | > This repository is read-only. It contains a working Go codegen plugin extracted from https://github.com/sqlc-dev/sqlc which you can fork and modify to meet your needs.
|
5 | 5 |
|
| 6 | +See [Building from source](#building-from-source) and [Migrating from sqlc's built-in Go codegen](#migrating-from-sqlcs-built-in-go-codegen) if you want to use a modified fork in your project. |
| 7 | + |
| 8 | +## Usage |
| 9 | + |
6 | 10 | ```yaml
|
7 | 11 | version: '2'
|
8 | 12 | plugins:
|
|
21 | 25 | package: db
|
22 | 26 | sql_package: pgx/v5
|
23 | 27 | ```
|
| 28 | +
|
| 29 | +## Building from source |
| 30 | +
|
| 31 | +Assuming you have the Go toolchain set up, from the project root you can simply `make all`. |
| 32 | + |
| 33 | +```sh |
| 34 | +make all |
| 35 | +``` |
| 36 | + |
| 37 | +This will produce a standalone binary and a WASM blob in the `bin` directory. |
| 38 | +They don't depend on each other, they're just two different plugin styles. You can |
| 39 | +use either with sqlc, but we recommend WASM and all of the configuration examples |
| 40 | +here assume you're using a WASM plugin. |
| 41 | + |
| 42 | +To use a local WASM build with sqlc, just update your configuration with a `file://` |
| 43 | +URL pointing at the WASM blob in your `bin` directory: |
| 44 | + |
| 45 | +```yaml |
| 46 | +plugins: |
| 47 | +- name: golang |
| 48 | + wasm: |
| 49 | + url: file:///path/to/bin/sqlc-gen-go.wasm |
| 50 | + sha256: "" |
| 51 | +``` |
| 52 | + |
| 53 | +As-of sqlc v1.24.0 the `sha256` is optional, but without it sqlc won't cache your |
| 54 | +module internally which will impact performance. |
| 55 | + |
| 56 | +## Migrating from sqlc's built-in Go codegen |
| 57 | + |
| 58 | +We’ve worked hard to make switching to sqlc-gen-go as seamless as possible. Let’s say you’re generating Go code today using a sqlc.yaml configuration that looks something like this: |
| 59 | + |
| 60 | +```yaml |
| 61 | +version: 2 |
| 62 | +sql: |
| 63 | +- schema: "query.sql" |
| 64 | + queries: "query.sql" |
| 65 | + engine: "postgresql" |
| 66 | + gen: |
| 67 | + go: |
| 68 | + package: "db" |
| 69 | + out: "db" |
| 70 | + emit_json_tags: true |
| 71 | + emit_pointers_for_null_types: true |
| 72 | + query_parameter_limit: 5 |
| 73 | + overrides: |
| 74 | + - column: "authors.id" |
| 75 | + go_type: "your/package.SomeType" |
| 76 | + rename: |
| 77 | + foo: "bar" |
| 78 | +``` |
| 79 | + |
| 80 | +To use the sqlc-gen-go WASM plugin for Go codegen, your config will instead look something like this: |
| 81 | + |
| 82 | +```yaml |
| 83 | +version: 2 |
| 84 | +plugins: |
| 85 | +- name: golang |
| 86 | + wasm: |
| 87 | + url: "https://downloads.sqlc.dev/plugin/sqlc-gen-go_1.0.0.wasm" |
| 88 | + sha256: "dbe302a0208afd31118fffcc268bd39b295655dfa9e3f385d2f4413544cfbed1" |
| 89 | +sql: |
| 90 | +- schema: "query.sql" |
| 91 | + queries: "query.sql" |
| 92 | + engine: "postgresql" |
| 93 | + codegen: |
| 94 | + - plugin: golang |
| 95 | + out: "db" |
| 96 | + options: |
| 97 | + package: "db" |
| 98 | + emit_json_tags: true |
| 99 | + emit_pointers_for_null_types: true |
| 100 | + query_parameter_limit: 5 |
| 101 | + overrides: |
| 102 | + - column: "authors.id" |
| 103 | + go_type: "your/package.SomeType" |
| 104 | + rename: |
| 105 | + foo: "bar" |
| 106 | +``` |
| 107 | + |
| 108 | +The differences are: |
| 109 | +* An additional top-level `plugins` list with an entry for the Go codegen WASM plugin. If you’ve built the plugin from source you’ll want to use a `file://` URL. The `sha256` field is required, but will be optional in the upcoming sqlc v1.24.0 release. |
| 110 | +* Within the `sql` block, rather than `gen` with `go` nested beneath you’ll have a `codegen` list with an entry referencing the plugin name from the top-level `plugins` list. All options from the current `go` configuration block move as-is into the `options` block within `codegen`. The only special case is `out`, which moves up a level into the `codegen` configuration itself. |
| 111 | + |
| 112 | +### Global overrides and renames |
| 113 | + |
| 114 | +If you have global overrides or renames configured, you’ll need to move those to the new top-level `options` field. Replace the existing `go` field name with the name you gave your plugin in the `plugins` list. We’ve used `"golang"` in this example. |
| 115 | + |
| 116 | +If your existing configuration looks like this: |
| 117 | + |
| 118 | +```yaml |
| 119 | +version: "2" |
| 120 | +overrides: |
| 121 | + go: |
| 122 | + rename: |
| 123 | + id: "Identifier" |
| 124 | + overrides: |
| 125 | + - db_type: "timestamptz" |
| 126 | + nullable: true |
| 127 | + engine: "postgresql" |
| 128 | + go_type: |
| 129 | + import: "gopkg.in/guregu/null.v4" |
| 130 | + package: "null" |
| 131 | + type: "Time" |
| 132 | +... |
| 133 | +``` |
| 134 | + |
| 135 | +Then your updated configuration would look something like this: |
| 136 | + |
| 137 | +```yaml |
| 138 | +version: "2" |
| 139 | +plugins: |
| 140 | +- name: golang |
| 141 | + wasm: |
| 142 | + url: "https://downloads.sqlc.dev/plugin/sqlc-gen-go_1.0.0.wasm" |
| 143 | + sha256: "dbe302a0208afd31118fffcc268bd39b295655dfa9e3f385d2f4413544cfbed1" |
| 144 | +options: |
| 145 | + golang: |
| 146 | + rename: |
| 147 | + id: "Identifier" |
| 148 | + overrides: |
| 149 | + - db_type: "timestamptz" |
| 150 | + nullable: true |
| 151 | + engine: "postgresql" |
| 152 | + go_type: |
| 153 | + import: "gopkg.in/guregu/null.v4" |
| 154 | + package: "null" |
| 155 | + type: "Time" |
| 156 | +... |
| 157 | +``` |
0 commit comments