sqlkratos

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2025 License: MIT Imports: 12 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/kratos-cli/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 added in v0.0.5

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

func NewGenerator added in v0.0.5

func NewGenerator() *Generator

func (*Generator) Generate added in v0.0.5

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

func (*Generator) WriteDataPackageCode added in v0.0.5

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

func (*Generator) WriteMainCode added in v0.0.5

func (g *Generator) WriteMainCode(
	outputPath string,

	projectName string,
	serviceName string,

	servers []string,
) error

func (*Generator) WriteServerPackageCode added in v0.0.5

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

func (*Generator) WriteServicePackageCode added in v0.0.5

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

func (*Generator) WriteWireCode added in v0.0.5

func (g *Generator) WriteWireCode(
	outputPath string,

	projectName string,
	serviceName string,
) error

func (*Generator) WriteWireSetCode added in v0.0.5

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

type GeneratorOption added in v0.0.4

type GeneratorOption func(*GeneratorOptions)

func WithExcludedTables added in v0.0.4

func WithExcludedTables(tables []string) GeneratorOption

func WithIncludedTables added in v0.0.4

func WithIncludedTables(tables []string) GeneratorOption

func WithModuleName added in v0.0.4

func WithModuleName(name string) GeneratorOption

func WithModuleVersion added in v0.0.4

func WithModuleVersion(ver string) GeneratorOption

func WithOutputPath added in v0.0.4

func WithOutputPath(path string) GeneratorOption

func WithSource added in v0.0.4

func WithSource(dsn string) GeneratorOption

func WithSourceModuleName added in v0.0.4

func WithSourceModuleName(name string) GeneratorOption

type GeneratorOptions added in v0.0.4

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