ormtool

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2023 License: MIT Imports: 7 Imported by: 2

README

Convert database table to Golang struct

1. Get

go get -u github.com/enjoy322/ormtool@main

2. Configuration

Note: The database table column should be underlined in lowercase

GenerateMySQL(
    Config{
        //[user]:[password]@tcp([host]:[port])/[database]?parseTime=true
        ConnStr: "root:qwe123@tcp(127.0.0.1:3306)/test?parseTime=true",
        // database name
        Database: "test",
        // relative path
        SavePath: "./model/model.go",
        // Generate one file or files
        IsGenInOneFile: true,
        // Generate simple database field information like: "int unsigned not null"
        // value 1:not generate; 2:simple info
        GenDBInfoType: 1,
        // json tag
        IsGenJsonTag: true,
        // json tag type. The necessary conditions:IsGenJsonTag:true.
        // 1.UserName 2.userName 3.user_name 4.user-name
        JsonTagType: 3,
        // sql of creating table in the database
        IsGenCreateSQL: true,
        // simple crud function
        IsGenFunction:true,
        // custom type relationships will be preferred. 
        // the key is the database type, The value is the golang type
        CustomType: map[string]string{
        "int":          "int",
        "int unsigned": "uint32",
        "tinyint(1)":   "bool",
        "json":         "json.RawMessage",
        },
	})


// result example
// User	用户表
type User struct {
    Id         int    `json:"id"`
    CreateTime int    `json:"create_time"` // 创建时间
    UserName   string `json:"user_name"`   // 用户名
}

func (*User) TableName() string {
    return "user"
}

var UserCol = struct {
    Id         string
    CreateTime string
    UserName   string
}{
    Id:         "id",
    CreateTime: "create_time",
    UserName:   "user_name",
}

// function

type UserModelInterface interface {
    Create(data *User) error
    Get(id int) (User, error)
    Find(condition interface{}, page, limit int) ([]User, error)
    Delete(id int) error
    DeleteUnScope(id int) error
}

type userModelService struct {
    db *gorm.DB
}

func NewUserModelService(db *gorm.DB) UserModelInterface {
    return userModelService{db: db}
}

func (s userModelService) Create(data *User) error {
    err := s.db.Create(data).Error
    if err != nil {
        return err
    }
    return nil
}

Reference

https://github.com/gohouse/converter

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DealFilePath added in v1.4.1

func DealFilePath(s string, db string) (packageName, fileDir, fileName string)

DealFilePath back save path and package name

func GenerateMySQL

func GenerateMySQL(c Config)

func JsonTag added in v1.4.1

func JsonTag(jsonType int, origin string) string

JsonTag deal tag by jsonType

func UpperCamel added in v1.4.1

func UpperCamel(s string) string

func Write added in v1.4.1

func Write(f FileInfo, data []StructInfo, oneFile bool)

Write struct information to .go file

Types

type Config added in v1.4.1

type Config struct {
	// connect mysql, [user]:[password]@tcp([host]:[port])/[database]?parseTime=true
	ConnStr string
	// database name
	Database string
	// file, relative path
	SavePath string
	// json tag
	IsGenJsonTag bool
	// Generate one file or files by table
	IsGenInOneFile bool
	// Generate simple database field information like: "int unsigned not null"
	// value 1:not generate; 2:simple info
	GenDBInfoType int
	// json tag type. The necessary conditions:IsGenJsonTag:true.
	// 1.UserName 2.userName 3.user_name 4.user-name
	JsonTagType int
	// sql of creating table in the database
	IsGenCreateSQL bool
	// simple crud function
	IsGenFunction bool
	// cache simple model info to redis, Ps. IsGenCreateSQL = true
	IsGenFunctionWithCache bool
	// custom type relationships will be preferred.
	// the key is the database type, The value is the golang type
	CustomType map[string]string
}

Config config information

type FileInfo added in v1.4.1

type FileInfo struct {
	PackageName string
	FileDir     string
	FileName    string
}

type StructInfo added in v1.4.1

type StructInfo struct {
	ImportInfo    []string
	Name          string
	FileName      string //model file name
	TableName     string
	Note          string // description
	CreateSQL     string // create table sql
	StructContent string
	Function      string // simple crud function
}

Jump to

Keyboard shortcuts

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