sqlc-grpc

command module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2021 License: MIT Imports: 21 Imported by: 0

README

sqlc-grpc

Create a gRPC (and HTTP/JSON) Server from the generated code by the awesome sqlc project.

Requirements

  • Go 1.16 or superior
  • protoc
  • sqlc, protoc-gen-go, protoc-gen-go-grpc and grpc-gateway
go install github.com/kyleconroy/sqlc/cmd/sqlc@latest
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@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
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;

  1. Create a sqlc.yaml file
version: "1"
packages:
  - path: "internal/author"
    queries: "./queries.sql"
    schema: "./queries.sql"
    engine: "postgresql"

  1. Execute sqlc
sqlc generate
  1. Execute sqlc-grpc
sqlc-grpc -m "my/module/path"
  1. Run the generated server
go run . -db [Database Connection URL] -dev -grpcui
  1. Enjoy!

Similar Projects

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL