Skip to content
This repository was archived by the owner on Nov 3, 2025. It is now read-only.

Enigmatic-Smile/sqlc-grpc

 
 

Repository files navigation

sqlc-grpc

Enigmatic Smile Usage

  • ./install.sh from the root to get the tool installed into your GOPATH
  • Ensure GOPATH is on your PATH
  • go generate in the repo you want to use the tool

Alternatively you can create a run configuration to run this tool in a particular directory from within Goland. You can copy the sqlc-grpc.xml.example but the general steps are as follows:

  • Create a new go build configuration
  • Change run kind to directory and make sure it is this repos root
  • Change working directory to be the directory you want to run this tool in

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.

Requirements

go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
go install github.com/bufbuild/buf/cmd/buf@latest

Installation

go install github.com/walterwanderley/sqlc-grpc@latest

Example

  1. 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;
  1. Create a sqlc.yaml file
version: "2"
sql:
- schema: "./queries.sql"
  queries: "./queries.sql"
  engine: "postgresql"
  gen:
    go:
      out: "internal/author"
  1. Execute sqlc
sqlc generate
  1. Execute sqlc-grpc
sqlc-grpc -m "mymodule"
  1. Run the generated server
go run . -db [Database Connection URL] -dev
  1. Enjoy!

SQLite with LiteFS

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

Customizing HTTP endpoints

You can customize the HTTP endpoints generated by grpc-gateway by adding comments to the queries.

-- http: Method Path

Here’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;

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

About

Create a gRPC server from code generated by sqlc

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 99.7%
  • Shell 0.3%