protoc-gen-structify

command module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 1 Imported by: 0

README

protoc-gen-structify

protoc-gen-structify is a plugin for protoc-gen-go that facilitates the generation of database wrappers from protobuf structures.

How It Works

The plugin extends protobuf definitions with additional options, enabling detailed configuration of the database representation of the protobuf structures. It effectively manages various database functionalities, such as the definition of primary keys, indexing, uniqueness constraints, and relationships between different entities (e.g., one-to-many, many-to-one).

Installation

go get -u github.com/cjp2600/protoc-gen-structify

Usage

See Makefile for example usage. (build-example)

.PHONY: build-example
build-example: build ## Build example: make build-example f=example/blog.proto
ifndef f
f = example/case_one/db/blog.proto
endif
	@$(PROTOC) -I/usr/local/include -I.  \
	-I$(GOPATH)/src   \
	--plugin=protoc-gen-structify=$(GOBIN)/structify \
	--structify_out=. --structify_opt=paths=source_relative,include_connection=true \
	$(f)

Supported Databases

  • Postgres
  • MySQL
  • SQLite
  • CockroachDB
  • MongoDB
  • Cassandra
  • Redis
  • DynamoDB

Example

See the example directory for a complete example. (db/blog.db.go is generated by protoc-gen-structify wrapper from blog.proto)

Protobuf
syntax = "proto3";

package blog;

option go_package = "github.com/cjp2600/protoc-gen-structify/example/case_one/db";

import "github.com/cjp2600/protoc-gen-structify/structify.proto";

message Blog {
    option (structify) = {
        table_name: "blogs"
        primary_key: "id"
        indexes: ["title"]
        unique_indexes: ["title"]
        relationships: [
            {
                name: "author"
                type: "one_to_many"
                foreign_key: "author_id"
                foreign_table: "authors"
            }
        ]
    };
    int64 id = 1;
    string title = 2;
    string content = 3;
    int64 author_id = 4;
}

## Postgres

Currently, `protoc-gen-structify` specializes in generating wrappers for PostgreSQL databases. It exploits libraries such as `"github.com/Masterminds/squirrel"` and `"github.com/lib/pq"`, as well as the `database/sql/driver`, omitting the need for reflection in its operations.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
example
case_two/db
Code generated by protoc-gen-structify.
Code generated by protoc-gen-structify.
case_one Module

Jump to

Keyboard shortcuts

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