GithubHelp home page GithubHelp logo

jamesarthurholland / sqlc-grpc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from walterwanderley/sqlc-grpc

0.0 0.0 0.0 880 KB

Create a gRPC server from code generated by sqlc

License: MIT License

Shell 0.79% Go 96.74% PLpgSQL 1.70% Dockerfile 0.77%

sqlc-grpc's Introduction

sqlc-grpc

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

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
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-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

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

sqlc-grpc's People

Contributors

walterwanderley avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.