crud

package
v0.0.0-...-b484ebe Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2025 License: Apache-2.0 Imports: 17 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Cmd = &cobra.Command{
	Use:   "crud",
	Short: "生成模块的crud功能",
	Long: `
字段设定格式: 字段注释(:字典备注:组件名称)
如:
状态 -  仅解析名称, 组件根据类型或字段后缀推导
状态:-1=禁用,0=待审核,1=正常 - 解析名称并设置下拉
状态:-1=禁用,0=待审核,1=正常:el-checkbox
`,
	Run: func(cmd *cobra.Command, args []string) {
		InitDB()
		if !IsDebug() {
			pine.Logger().Warn("非Debug模式,不支持 crud 命令")
			return
		}
		table, _ := cmd.Flags().GetString("table")
		force, _ := cmd.Flags().GetBool("force")
		onlyInfo, _ := cmd.Flags().GetBool("info")
		frontendPath, _ := cmd.Flags().GetString("path")
		if len(table) == 0 {
			_ = cmd.Help()
			return
		}
		metas, err := Orm().DBMetas()
		if err != nil {
			pine.Logger().Error(err.Error())
			return
		}
		var tableMata *schemas.Table
		for _, meta := range metas {
			if meta.Name == getTableName(table) {
				tableMata = meta
				break
			}
		}
		if tableMata == nil {
			pine.Logger().Error(fmt.Sprintf("无法获取数据表[%s]元信息", getTableName(table)))
			return
		}
		for _, v := range tableMata.Columns() {
			cols[v.Name] = v
		}
		controllerName, controllerPath := getControllerName(table)
		tablePath := tableDir + table + goExt
		if !force {
			for _, s := range []string{frontendPath + "/" + feModuleDir + table, controllerPath, tablePath} {
				if _, err := os.Stat(s); !os.IsNotExist(err) {
					pine.Logger().Info("已有存在: " + s)
					return
				}
			}
		}

		helper.PanicErr(genTableFileAndFrontendFile(onlyInfo, table, tableDir+table+goExt, frontendPath))
		helper.PanicErr(genControllerFile(onlyInfo, controllerName, table, controllerPath))

		byts, err := os.ReadFile(routerFile)
		helper.PanicErr(err)
		controllerNamespace := `"github.com/xiusin/pinecms/src/application/controllers/backend"`
		pineNamespace := `"github.com/xiusin/pine"`
		holder := "// holder"
		if !bytes.Contains(byts, []byte(controllerNamespace)) {
			byts = bytes.Replace(byts, []byte(pineNamespace), []byte(pineNamespace+"\r\n\t"+controllerNamespace), 1)
		}
		byts = bytes.Replace(byts, []byte(holder), []byte(holder+"\r\n\t"+`backendRouter.Handle(new(backend.`+controllerName+`), "/`+util.SnakeString(table)+`")`), 1)
		os.WriteFile(routerFile, byts, os.ModePerm)
		pine.Logger().Info("创建模块文件成功, 已注册路由信息至: " + routerFile)
		pine.Logger().Info("[❎] TODO: ast自动生成文件,参照tests/ast_test.go")
	},
}

Functions

func FormatEnum

func FormatEnum(field string, opts []map[string]any, item map[string]any)

func FormatSet

func FormatSet(field string, opts []map[string]any, item map[string]any)

func JSONMarshal

func JSONMarshal(t any) ([]byte, error)

JSONMarshal 不转义字符串编码

Types

type SQLColumn

type SQLColumn struct {
	Name, Type    string
	IsPrimaryKey  bool
	IsUnique      bool
	Length        string
	EnumValues    []string
	AutoIncrement bool
	NotNull       bool
	Default       string
}

SQLColumn 描述结构体

type SQLTable

type SQLTable struct {
	Name string
	Cols []SQLColumn
}

SQLTable 表名结构体

Jump to

Keyboard shortcuts

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