gencode

package
v1.37.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package gencode 提供 Web 化低代码生成平台(对标 RuoYi 代码生成器): 在线查看数据库表/字段、编辑字段、同步表结构、一键生成 DDD 代码。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(app *iris.Application, prefix string, cfg Config) (iris.Party, error)

Register 注册低代码生成平台:

GET  <prefix>                 管理页面(表列表/字段编辑/生成)
GET  <prefix>/api/tables      表列表(+已生成标记)
GET  <prefix>/api/tables/{t}  表详情(字段)
POST <prefix>/api/tables/{t}/columns       新增字段
DELETE <prefix>/api/tables/{t}/columns/{c} 删除字段
POST <prefix>/api/tables/{t}/sync          同步表结构
GET  <prefix>/api/tables/{t}/preview       代码预览
POST <prefix>/api/tables/{t}/generate      生成代码(force=true 覆盖)

Types

type ColumnMeta

type ColumnMeta struct {
	Name       string `json:"name"`        // 列名
	DataType   string `json:"data_type"`   // 数据库类型(INTEGER/TEXT/VARCHAR...)
	Length     int    `json:"length"`      // 长度(0=无)
	Nullable   bool   `json:"nullable"`    // 是否可空
	Default    string `json:"default"`     // 默认值(空=无)
	Comment    string `json:"comment"`     // 注释
	PrimaryKey bool   `json:"primary_key"` // 是否主键
	AutoIncr   bool   `json:"auto_incr"`   // 是否自增
}

ColumnMeta 字段元数据。

type Config

type Config struct {
	// DB 数据库实例(元数据与生成记录使用;必填)。
	DB *gorm.DB
	// ModulePath 业务项目 module 路径(生成 import 用;必填)。
	ModulePath string
	// OutputDir 生成文件输出根目录(默认当前目录)。
	OutputDir string
	// Auth 管理接口认证中间件(推荐 webiris.Auth;nil 时仅限流)。
	Auth iris.Handler
	// RatePerSecond 管理接口限流(默认 5 QPS)。
	RatePerSecond float64
}

Config 组件配置。

type GenResult

type GenResult struct {
	Table     string          `json:"table"`
	Files     []GeneratedFile `json:"files"`
	RouteCode string          `json:"route_code"` // 路由注册代码段(复制进 main.go)
}

GenResult 一次生成的输出。

func Generate

func Generate(table TableMeta, modulePath string) (*GenResult, error)

Generate 从表元数据生成 DDD 代码(五件套 + 路由片段)。 modulePath 用于内部包 import 路径(如 github.com/example/app)。

type GeneratedFile

type GeneratedFile struct {
	Path    string `json:"path"`    // 相对项目路径(如 internal/model/test_mycat.go)
	Content string `json:"content"` // 文件内容
	Hash    string `json:"hash"`    // 内容哈希(覆盖比对用)
}

GeneratedFile 生成结果文件。

type Meta

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

Meta 元数据读取器(按驱动分支)。

func NewMeta

func NewMeta(db *gorm.DB) *Meta

NewMeta 创建元数据读取器。

func (*Meta) DB

func (m *Meta) DB() *gorm.DB

DB 返回底层数据库(生成记录表使用)。

func (*Meta) ListTables

func (m *Meta) ListTables(ctx context.Context) ([]TableMeta, error)

ListTables 读取全部业务表(排除系统表)。

func (*Meta) ReadTable

func (m *Meta) ReadTable(ctx context.Context, tableName string) (*TableMeta, error)

ReadTable 读取单表完整元数据(字段)。

type Service

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

Service 低代码生成服务。

func NewService

func NewService(db *gorm.DB, modulePath, outputDir string) (*Service, error)

NewService 创建生成服务。

func (*Service) AddColumn

func (s *Service) AddColumn(ctx context.Context, tableName string, column ColumnMeta) error

AddColumn 新增字段(ALTER TABLE ADD COLUMN)。

func (*Service) DropColumn

func (s *Service) DropColumn(ctx context.Context, tableName, columnName string) error

DropColumn 删除字段(驱动支持时)。

func (*Service) Generate

func (s *Service) Generate(ctx context.Context, tableName string, force bool) (*GenResult, []string, error)

Generate 生成代码并写入文件。 已生成过的表需要 force=true 才覆盖(返回待覆盖文件清单)。

func (*Service) ListTables

func (s *Service) ListTables(ctx context.Context) ([]TableInfo, error)

ListTables 表列表(含生成标记)。

func (*Service) Preview

func (s *Service) Preview(ctx context.Context, tableName string) (*GenResult, error)

Preview 生成代码预览(不落盘)。

func (*Service) ReadTable

func (s *Service) ReadTable(ctx context.Context, tableName string) (*TableMeta, error)

ReadTable 表详情(字段)。

func (*Service) SyncTable

func (s *Service) SyncTable(ctx context.Context, tableName string) (*TableMeta, error)

SyncTable 同步表结构:重读元数据并返回(对比记录中字段,标注新增/删除)。

type TableInfo

type TableInfo struct {
	TableMeta
	Generated   bool       `json:"generated"`
	GeneratedAt *time.Time `json:"generated_at,omitempty"`
}

TableInfo 表 + 已生成标记。

type TableMeta

type TableMeta struct {
	Name    string       `json:"name"`    // 表名
	Comment string       `json:"comment"` // 表注释
	Columns []ColumnMeta `json:"columns"` // 字段
}

TableMeta 表元数据。

Jump to

Keyboard shortcuts

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