cmgen

package module
v0.0.0-...-a3a3ab8 Latest Latest
Warning

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

Go to latest
Published: May 30, 2019 License: MIT Imports: 10 Imported by: 0

README

cmgen

golang mgo 代码自动生成器, 生成对 mongodb 的 CRUD 操作代码。forked from https://github.com/yakumioto/mgen

安装

go install github.com/chxfantasy/cmgen/cmgen

使用

mgo command

编写 配置文件, 然后使用命令生成. 可以参照 example

NAME:
   flag mgo - generate golang code

USAGE:
   flag mgo [command options] [arguments...]

OPTIONS:
   --config-file value, -c value  set the config file path
   --help, -h                     show help (default: false)

根据所传入的配置文件生成对应的 CRUD package.

example: cmgen mgo -c xxx.yaml

会在执行命令会在当前文件夹下生成一个 xxx.mg.go 的文件.

配置文件编写

简单的用法

base.yaml

packageName: base
models:
  - name: User
    collectionName: users
    fields:
      - name: UserName
        type: string
      - name: Email
        type: string
      - name: Password
        type: string

执行后生成的Go文件: base.mg.go

如果指定了 collectionName 就会生成这个对应的 CRUD 方法

  • NewUser() *User
  • (user *User) Insert() error
  • UpdateUserByID(id string, user *User) error
  • UpdateUserByIDAndEntityMap(id interface{}, updateMap map[string]interface{}) error
  • UpdateUser(selector interface{}, user *User) error
  • UpdateUserAll(selector interface{}, user *User) (*mgo.ChangeInfo, error)
  • GetUserByID(id string) (*User, error)
  • GetOneUserByQuery(query map[string]interface{}) (*User, error)
  • ListAllUserByQuery(query map[string]interface{}) ([]*User, error)
  • ExistUserByID(id string) (bool, error)
  • ExistUserByQuery(query interface{}) (bool, error)
  • DeleteUserByID(id string) error

说明

  1. 代码中会自动插入CreatedAt、UpdatedAt、deleted,所有的删除均为逻辑删除

  2. 自动生成的Model代码中,引入了以下包。如有需要,请自行更改。

    import (
        "errors"
        "github.com/globalsign/mgo"
        "github.com/globalsign/mgo/bson"
        "github.com/liamylian/jsontime"
        "time"
    )
    
  3. init函数中,需要自行初始化MongoSession

  4. tmpl文件,使用https://github.com/gobuffalo/packr 打包到二进制文件中

  5. 本地修改的tmpl文件,需要执行packr2,自动将文件内容打包到二进制包中。或者直接运行./rebuild.sh

进阶用法
packageName: advanced
models:
  - name: User
    collectionName: users
    fields:
      - name: UserName
        type: string
        unique: yes
        valid: required~first name is blank
      - name: Email
        type: string
        unique: yes
        valid: required,email
      - name: Password
        type: string
        valid: required

执行后生成的Go文件: advanced.mgo.go

unique 用来指定唯一

valid 用法实在太多了 使用的是 https://github.com/asaskevich/govalidator

感谢

The MongoDB driver for Go https://github.com/globalsign/mgo

Package of validators and sanitizers for strings, numerics, slices and structs https://github.com/asaskevich/govalidator

forked from https://github.com/yakumioto/mgen

Documentation

Overview

You can use the "packr clean" command to clean up this, and any other packr generated files.

Index

Constants

This section is empty.

Variables

View Source
var TemplateBoxes *packr.Box

Functions

func MgoAction

func MgoAction(context *cli.Context) error

func SnakeString

func SnakeString(str string) string

Types

type Field

type Field struct {
	Name   string `yaml:"name,omitempty"`
	Type   string `yaml:"type,omitempty"`
	Unique bool   `yaml:"unique,omitempty"`
	Valid  string `yaml:"valid,omitempty"`
}

type Model

type Model struct {
	Name           string   `yaml:"name,omitempty"`
	CollectionName string   `yaml:"collectionName,omitempty"`
	CRUD           bool     `yaml:"CRUD,omitempty"`
	Types          []string `yaml:"types,omitempty"`
	States         []string `yaml:"states,omitempty"`
	Fields         []Field  `yaml:"fields,omitempty"`
}

type ModelGenerator

type ModelGenerator struct {
	ConfigName  string  `yaml:"-"`
	FileName    string  `yaml:"-"`
	PackageName string  `yaml:"packageName,omitempty"`
	DbName      string  `yaml:"dbName,omitempty"`
	Models      []Model `yaml:"models,omitempty"`
	Raw         string  `yaml:"raw,omitempty"`
}

Directories

Path Synopsis
example
You can use the "packr2 clean" command to clean up this, and any other packr generated files.
You can use the "packr2 clean" command to clean up this, and any other packr generated files.

Jump to

Keyboard shortcuts

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