sqlc-grpc
Create a gRPC (and HTTP/JSON) Server from the generated code by the awesome sqlc project.
Requirements
go install github.com/kyleconroy/sqlc/cmd/sqlc@latest
go install github.com/bufbuild/buf/cmd/buf@latest
Installation
go install github.com/walterwanderley/sqlc-grpc@latest
Example
- 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
SELECT * FROM authors
WHERE id = $1 LIMIT 1;
-- name: ListAuthors :many
SELECT * FROM authors
ORDER BY name;
-- name: CreateAuthor :one
INSERT INTO authors (
name, bio, created_at
) VALUES (
$1, $2, $3
)
RETURNING *;
-- name: DeleteAuthor :exec
DELETE FROM authors
WHERE id = $1;
- Create a sqlc.yaml file
version: "1"
packages:
- path: "internal/author"
queries: "./queries.sql"
schema: "./queries.sql"
engine: "postgresql"
- Execute sqlc
sqlc generate
- Execute sqlc-grpc
sqlc-grpc -m "my/module/path"
- Run the generated server
go run . -db [Database Connection URL] -dev -grpcui
- Enjoy!
Editing the generated code
-
It's safe to edit any generated code that doesn't have the DO NOT EDIT indication 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.
Similar Projects