Project is moved to kapow-tech
SDM is a toolset for Golang projects to manage sensitive data (PII) by separating it from public chain data using Protobuf annotations. It automatically generates Go models, SQL schemas, and Repository functions to handle the data flow.
- Proto Annotations: Define
primary_key,pii,hashed, etc., directly in your.protofiles. - Auto-Generated Go Models: Creates GORM-compatible structs for PII tables, Chain tables, and combined Views.
- Auto-Generated SQL: Generates
CREATE TABLEandCREATE VIEWstatements for PostgreSQL. - Auto-Generated Repositories: Generates type-safe
SaveandFetchmethods that handle:- Splitting data into PII and Chain tables.
- Hashing fields marked as
hashed. - Reconstructing objects from the DB View.
- Integrated Toolchain: The
sdmCLI manages dependencies, setup, and generation, acting as a wrapper around standard tools likebufandprotoc.
-
Install the Tool:
go install github.com/jinuthankachan/sdm/cmd/sdm@latest
-
Configuration:
Generate a configuration file to manage your project settings.
sdm config
This creates
sdm.cfg.yamlwhere you can customize output directories and input proto files. -
Setup the Environment: Run
sdm setupto install required dependencies (protoc-gen-go,buf,protoc-gen-sdm), initialize thebufmodule, and download SDM proto definitions.sdm setup
An example can be found at SDM examples repo
Create a .proto file (e.g., proto/invoice/invoice.proto) and import annotations/annotations.proto. Annotate your fields:
syntax = "proto3";
package invoice;
import "annotations/annotations.proto";
option go_package = "github.com/jinuthankachan/sdm/proto/invoice";
message Invoice {
string id = 1 [(sdm.primary_key) = true, (sdm.chain_identifier_key) = true];
int64 invoice_number = 2 [(sdm.pii) = true];
string seller_gst = 3 [(sdm.pii) = true, (sdm.hashed) = true];
// ...
}Run the sdm tool to generate the artifacts.
sdm generate --proto proto/invoice/invoice.proto --out gen_outOr, if you have configured sdm.cfg.yaml with your protos:
sdm generateThis will compile the protos using the sdm directory (setup by sdm setup) as an import path and generate:
invoice.pb.go: Standard Protobuf Go code.invoice_sdm_model.go: SDM Structs (...Pii,...Chain,...View).invoice_sdm_schema.sql: SQL DDL for PII, Chain tables and Views.invoice_sdm_repo.go: GORM Repository implementation.
import (
"context"
"gorm.io/gorm"
"github.com/jinuthankachan/sdm/proto/invoice"
// Ensure the annotations package is available if needed, usually implicitly handled by generated code imports
)
func main() {
db, _ := gorm.Open(...)
repo := invoice.NewInvoiceRepo(db)
// Save (Splits and Hashes automatically)
err := repo.Save(ctx, &invoice.Invoice{
Id: "inv_123",
InvoiceNumber: 1001,
SellerGst: "GST001",
})
// Fetch (reconstructs from View)
view, err := repo.Fetch(ctx, "inv_123")
}sdm setup: Installs dependencies (protoc-gen-go,buf,protoc-gen-sdm), initializesbuf, and exports SDM protos to a localsdm/directory.sdm config: Generates a defaultsdm.cfg.yamlfile.sdm generate: Compiles and generates code.--proto: Input proto file (optional if defined in config).--out: Output directory (optional if defined in config).--cfg: Path to config file (defaultsdm.cfg.yaml).
If you prefer using buf directly without the sdm wrapper:
- Install the plugin:
go install github.com/jinuthankachan/sdm/cmd/protoc-gen-sdm@latest
- Configure
buf.gen.yaml:version: v1 plugins: - plugin: go out: . opt: paths=source_relative - plugin: sdm out: . opt: paths=source_relative
- Generate:
buf generate
pii_<name>s: Storespiifields andprimary_key.chain_<name>s: key-value store for non-pii andhashedfields (EAV pattern).<name>s(View): Joins the PII table with the latest values from the Chain table.