You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+54-25Lines changed: 54 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# sqlc-gen-go-server
2
2
3
-
[Sqlc plugin](https://sqlc.dev) to generate [gRPC](https://grpc.io/) or [Connect](https://connectrpc.com/) server from SQL.
3
+
[Sqlc plugin](https://sqlc.dev) to generate [gRPC](https://grpc.io/), [Connect](https://connectrpc.com/) or [HTTP](https://pkg.go.dev/net/http) server from SQL.
4
4
5
5
## Usage
6
6
@@ -24,7 +24,58 @@ sql:
24
24
server_type: grpc
25
25
```
26
26
27
-
## Post-process
27
+
All [plugins options](https://github.com/walterwanderley/sqlc-gen-go-server?tab=readme-ov-file#plugin-options).
28
+
29
+
### Customizing HTTP endpoints
30
+
31
+
You can customize the HTTP endpoints (server_type: http or grpc) by adding comments to the queries.
32
+
33
+
```sql
34
+
-- http: Method Path
35
+
```
36
+
37
+
Here’s an example of a queries file that has custom HTTP endpoints:
38
+
```sql
39
+
-- name: ListAuthors :many
40
+
-- http: GET /authors
41
+
SELECT * FROM authors
42
+
ORDER BY name;
43
+
44
+
-- name: UpdateAuthorBio :exec
45
+
-- http: PATCH /authors/{id}/bio
46
+
UPDATE authors
47
+
SET bio = $1
48
+
WHERE id = $2;
49
+
```
50
+
51
+
## Post-process for server_type: http
52
+
53
+
>**Note:** If you’d rather not execute these steps, you might want to use [sqlc-http](https://github.com/walterwanderley/sqlc-http) instead of this plugin.
54
+
55
+
After execute `sqlc generate` you need to organize imports and fix go.mod.
56
+
57
+
1. Install the required tools:
58
+
59
+
```sh
60
+
go install golang.org/x/tools/cmd/goimports@latest
61
+
```
62
+
63
+
2. Organize imports:
64
+
65
+
```sh
66
+
goimports -w *.go **/*.go **/**/*.go
67
+
```
68
+
69
+
3. Fix go.mod:
70
+
71
+
```sh
72
+
go mod tidy
73
+
```
74
+
75
+
4. If you define more than one package, you’ll need to add them to the generated **registry.go** file.
76
+
77
+
78
+
## Post-process for server_type: grpc or connect
28
79
29
80
>**Note:** If you’d rather not execute these steps, you might want to use [sqlc-grpc](https://github.com/walterwanderley/sqlc-grpc) or [sqlc-connect](https://github.com/walterwanderley/sqlc-connect) instead of this plugin.
30
81
@@ -63,28 +114,6 @@ go mod tidy
63
114
64
115
5. If you define more than one package, you’ll need to add them to the generated **registry.go** file.
65
116
66
-
### Customizing HTTP endpoints
67
-
68
-
You can customize the HTTP endpoints generated by grpc-gateway (server_type: grpc) by adding comments to the queries.
69
-
70
-
```sql
71
-
-- http: Method Path
72
-
```
73
-
74
-
Here’s an example of a queries file that has custom HTTP endpoints:
75
-
```sql
76
-
-- name: ListAuthors :many
77
-
-- http: GET /authors
78
-
SELECT * FROM authors
79
-
ORDER BY name;
80
-
81
-
-- name: UpdateAuthorBio :exec
82
-
-- http: PATCH /authors/{id}/bio
83
-
UPDATE authors
84
-
SET bio = $1
85
-
WHERE id = $2;
86
-
```
87
-
88
117
## Plugin options
89
118
90
119
You can use all of the [Golang plugin's options](https://docs.sqlc.dev/en/latest/reference/config.html#go) as well as the options described below.
@@ -104,7 +133,7 @@ sql:
104
133
- plugin: go-server
105
134
out: "internal/db"
106
135
options:
107
-
server_type: "grpc" # The server type: grpcor connect.
136
+
server_type: "http"# The server type: grpc, connect or http.
108
137
module: "my-module"# The module name for the generated go.mod.
109
138
metric: false # If true, enable open telemetry metrics.
110
139
tracing: false # If true, enable open telemetry distributed tracing.
0 commit comments