Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/walterwanderley/sqlc-grpc

Create a gRPC server from code generated by sqlc
https://github.com/walterwanderley/sqlc-grpc

grpc sqlc sqlite

Last synced: about 1 month ago
JSON representation

Create a gRPC server from code generated by sqlc

Awesome Lists containing this project

README

        

## sqlc-grpc

Create a **gRPC** (and **HTTP/JSON**) **Server** from the generated code by the awesome [sqlc](https://sqlc.dev/) project. If you’re searching for a SQLC plugin, use [sqlc-gen-go-server](https://github.com/walterwanderley/sqlc-gen-go-server/).

### Requirements

- Go 1.21 or superior
- [sqlc](https://sqlc.dev/)
- [buf](https://buf.build/)

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

### Installation

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

### Example

1. Create a queries.sql file:

```sql
--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;
```

2. Create a sqlc.yaml file

```yaml
version: "2"
sql:
- schema: "./queries.sql"
queries: "./queries.sql"
engine: "postgresql"
gen:
go:
out: "internal/author"
```

3. Execute sqlc

```sh
sqlc generate
```

4. Execute sqlc-grpc

```sh
sqlc-grpc -m "mymodule"
```

5. Run the generated server

```sh
go run . -db [Database Connection URL] -dev
```

6. Enjoy!

- Swagger UI [http://localhost:5000/swagger](http://localhost:5000/swagger)

### SQLite with LiteFS

Use the **-litefs** command line parameter to replicate SQLite with [LiteFS](https://github.com/superfly/litefs) as a library.
Example: [https://github.com/walterwanderley/sqlc-grpc/tree/main/_examples/authors/sqlite](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.

```sql
-- http: Method Path
```

Here’s an example of a queries file that has a custom HTTP endpoint:
```sql
-- 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:

```sh
sqlc generate
go generate
```

- After modify a *.proto file, execute `buf generate`.

### Similar Projects

- [xo-grpc](https://github.com/walterwanderley/xo-grpc)