walterwanderley / sqlc-connect

Generate connect-go server from SQL
MIT License
22 stars 2 forks source link

sqlc-connect

Generate connect-go server from SQL. 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-connect@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: "2"
sql:
- schema: "./queries.sql"
  queries: "./queries.sql"
  engine: "postgresql"
  gen:
    go:
      out: "internal/author"
  1. Execute sqlc
sqlc generate
  1. Execute sqlc-connect
sqlc-connect -m "authors"
  1. Run the generated server
go run . -db [Database Connection URL] -dev
  1. Enjoy!

http://localhost:5000/swagger

or

go install github.com/fullstorydev/grpcui/cmd/grpcui@latest
grpcui -plaintext localhost:5000

Editing the generated code

sqlc generate
go generate

Similar Projects