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.