sqlkratos

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2026 License: MIT Imports: 13 Imported by: 0

README

SQL → Kratos Microservice

This tool imports the SQL database schemas and generates Kratos microservice code.

HOW TO INSTALL

go install github.com/tx7do/go-wind-toolkit/sql-kratos/cmd/sql2kratos@latest

HOW TO USE

sql2kratos imports the SQL database schemas and generates Kratos microservice code.

Usage:
  sql2kratos [flags]

Flags:
  -n, --dsn string          Data source name (connection information), for example:
                            "mysql://user:pass@tcp(localhost:3306)/dbname"
                            "postgres://user:pass@host:port/dbname"
  -e, --excludes strings    comma-separated list of tables to exclude
  -l, --gen-data            enable generate data package code (default true)
  -k, --gen-main            enable generate main package code (default true)
  -z, --gen-orm             enable generate ORM code (default true)
  -q, --gen-proto           enable generate protobuf schema files (default true)
  -w, --gen-srv             enable generate server package code (default true)
  -a, --gen-svc             enable generate service package code (default true)
  -h, --help                help for sql2kratos
  -i, --includes strings    comma-separated list of tables to inspect (all if empty)
  -m, --module string       Target module name for the generated code, e.g., 'admin' (default "admin")
  -r, --orm string          ORM type to use (ent, gorm) (default "ent")
  -o, --output string       output path for protobuf schema files (default "./api/protos/")
  -p, --project string      Project name for the generated code, e.g., 'kratos-admin' (default "kratos-admin")
  -x, --repo                use repository pattern (default true)
  -g, --servers strings     comma-separated list of servers to generate, e.g., "grpc,rest" (default [grpc])
  -c, --service string      Service name for the generated code, e.g., 'user' (default "user")
  -s, --src-module string   Source module name, for REST service generate, e.g., "admin" (default "user")
  -v, --version string      Version of the module, e.g., 'v1' (default "v1")

EXAMPLES

generate code for gRPC service:

sql2kratos \
  -p "kratos-admin" \
  -n "postgres://postgres:pass@localhost:5432/test?sslmode=disable" \
  -r "ent" \
  -o "." \
  -m "user" \
  -c "user" \
  -g "grpc"

generate code for REST service:

sql2kratos \
  -p "kratos-admin" \
  -n "postgres://postgres:pass@localhost:5432/test?sslmode=disable" \
  -r "ent" \
  -o "." \
  -s "user" \
  -m "admin" \
  -c "admin" \
  -g "rest" \
  -x=false \
  -z=false \
  -l=false

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(ctx context.Context, opts GeneratorOptions) error

Types

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

func NewGenerator

func NewGenerator() *Generator

func (*Generator) Generate

func (g *Generator) Generate(ctx context.Context, opts GeneratorOptions) error

func (*Generator) WriteDataPackageCode

func (g *Generator) WriteDataPackageCode(
	outputPath string,
	orm string,
	projectName string,
	serviceName string,
	name string,
	moduleName, moduleVersion string,
	protoFields []generators.DataField,
) error

func (*Generator) WriteMainCode

func (g *Generator) WriteMainCode(
	outputPath string,

	projectName string,
	serviceName string,

	servers []string,
) error

func (*Generator) WriteServerPackageCode

func (g *Generator) WriteServerPackageCode(
	outputPath string,
	projectName string,
	serviceType string,
	serviceName string,
	services map[string]string,
) error

func (*Generator) WriteServicePackageCode

func (g *Generator) WriteServicePackageCode(
	outputPath string,
	projectName string,
	serviceName string,
	name string,
	targetModuleName, sourceModuleName, moduleVersion string,
	useRepo, isGrpcService bool,
) error

func (*Generator) WriteWireCode

func (g *Generator) WriteWireCode(
	outputPath string,

	projectName string,
	serviceName string,
) error

func (*Generator) WriteWireSetCode

func (g *Generator) WriteWireSetCode(
	outputPath string,
	projectModule string,
	serviceName string,
	packageName string,
	postfix string,
	services []string,
) error

type GeneratorOption

type GeneratorOption func(*GeneratorOptions)

func WithExcludedTables

func WithExcludedTables(tables []string) GeneratorOption

func WithIncludedTables

func WithIncludedTables(tables []string) GeneratorOption

func WithModuleName

func WithModuleName(name string) GeneratorOption

func WithModuleVersion

func WithModuleVersion(ver string) GeneratorOption

func WithOutputPath

func WithOutputPath(path string) GeneratorOption

func WithSource

func WithSource(dsn string) GeneratorOption

func WithSourceModuleName

func WithSourceModuleName(name string) GeneratorOption

type GeneratorOptions

type GeneratorOptions struct {
	Driver string
	Source string // Data Source name (DSN), e.g., "mysql://user:pass@tcp(localhost:3306)/dbname"

	IncludedTables []string
	ExcludedTables []string

	OutputPath string

	SourceModuleName string // for REST service, the Source module name
	ModuleName       string
	ModuleVersion    string

	OrmType string // ORM type, e.g., "gorm", "sqlx", "ent"

	ProjectName string
	ServiceName string

	Servers []string

	UseRepo bool

	GenerateProto   bool
	GenerateServer  bool
	GenerateService bool
	GenerateORM     bool
	GenerateData    bool
	GenerateMain    bool
}

Directories

Path Synopsis
cmd
sql2kratos command

Jump to

Keyboard shortcuts

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