Create a gRPC (and HTTP/JSON) Server from the generated code by the awesome sqlc project. If you’re searching for a SQLC plugin, use sqlc-gen-go-server.
go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
go install github.com/bufbuild/buf/cmd/buf@latestgo install github.com/walterwanderley/sqlc-grpc@latest- Create a queries.sql file:
--queries.sql
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name text NOT NULL,
bio text,
created_at TIMESTAMP
);
-- name: GetAuthor :one
-- http: GET /authors/{id}
SELECT * FROM authors
WHERE id = $1 LIMIT 1;
-- name: ListAuthors :many
-- http: GET /authors
SELECT * FROM authors
ORDER BY name;
-- name: CreateAuthor :one
-- http: POST /authors
INSERT INTO authors (
name, bio, created_at
) VALUES (
$1, $2, $3
)
RETURNING *;
-- name: DeleteAuthor :exec
-- http: DELETE /authors/{id}
DELETE FROM authors
WHERE id = $1;
-- name: UpdateAuthorBio :exec
-- http: PATCH /authors/{id}/bio
UPDATE authors
SET bio = $1
WHERE id = $2;- Create a sqlc.yaml file
version: "2"
sql:
- schema: "./queries.sql"
queries: "./queries.sql"
engine: "postgresql"
gen:
go:
out: "internal/author"- Execute sqlc
sqlc generate- Execute sqlc-grpc
sqlc-grpc -m "mymodule"- Run the generated server
go run . -db [Database Connection URL] -dev- Enjoy!
- Swagger UI http://localhost:5000/swagger
Use the -litefs command line parameter to replicate SQLite with LiteFS as a library. Example: https://github.com/walterwanderley/sqlc-grpc/tree/main/_examples/authors/sqlite
You can customize the HTTP endpoints generated by grpc-gateway by adding comments to the queries.
-- http: Method Path :RolesHere’s an example of a queries file that has a custom HTTP endpoint:
-- name: ListAuthors :many
-- http: GET /authors
SELECT * FROM authors
ORDER BY name;
-- name: UpdateAuthorBio :exec
-- http: PATCH /authors/{id}/bio
UPDATE authors
SET bio = $1
WHERE id = $2;
-- name: CreateAuthor :one
-- http: POST /authors :admin,editor
INSERT INTO authors (
name, bio, created_at
) VALUES (
$1, $2, $3
)
RETURNING *;-
It's safe to edit any generated code that doesn't have the
DO NOT EDITindication at the very first line. -
After modify a SQL file, execute these commands below:
sqlc generate
go generate- After modify a *.proto file, execute
buf generate.
ignore_services:
- HealthService
- DebugService
remove_services:
- DeprecatedUserService
add_services:
- name: AuthorService
method: CreateAuthor
path: /v1/authors
req_fields:
- name: name
type: string
- name: bio
type: string
res_fields:
- name: id
type: int64
- name: created_at
type: string
remove_fields:
- "*.bio" # Remove 'bio' field from all messages
- "Author.email" # Remove 'email' field only from Author message
add_fields:
- msg: CreateAuthorRequest
fields:
- name: age
type: int32
- name: email
type: string
roles_filter:
admin:
add_fields:
- msg: Author
fields:
- name: is_verified
type: bool
- name: internal_notes
type: string
remove_fields:
- "*.debug_info"
viewer:
remove_fields:
- "*.email"
- "*.phone"
packages:
authors:
ignore_services:
- LegacyAuthorService
add_fields:
- msg: Author
fields:
- name: country
type: string
roles_filter:
viewer:
remove_fields:
- "*.bio"
- "Author.email"
editor:
add_fields:
- msg: Author
fields:
- name: updated_by
type: string