tbox

package module
v1.1.5 Latest Latest
Warning

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

Go to latest
Published: May 11, 2025 License: MIT Imports: 8 Imported by: 0

README

tbox

Model generation tool, which mainly generates mysql table 
as golang type, avoids the inconvenience caused by manual
operation, and facilitates project maintenance and expansion.

usage

go version >= 1.16
$ go install github.com/daheige/tbox/cmd/tbox@latest

$ tbox -h
Usage of tbox:
    -d string
        pkg dir path,eg:-d=./model (default "./model")
    -dsn string
        mysql dsn,eg:-dsn="root:root1234@tcp(127.0.0.1:3306)/test?charset=utf8mb4"
    -j    whether add json tag eg:-j=true
    -m    whether add TableName func eg:-m=true
    -p string
        pkg name,eg:-p=model (default "model")
    -t string
        table,eg:-t=user;order
    -tag string
        tag key,eg:-tag=db (default "db")
    -u    whether uc first only,eg:-u=true (default false)
    -v    whether output cmd,eg:-v=true
    -n    whether all field no null eg:-n=true

take tbox_demo as an example:
$ cd ~
$ mkdir tbox_demo
$ cd tbox_demo
$ tbox -dsn="root:root1234@tcp(127.0.0.1:3306)/test?charset=utf8mb4"
2021/12/25 19:36:18 generating code success

the style of code generation

// Package model of db entity
// Code generated by daheige/tbox. DO NOT EDIT!!!

package model

import (
	"time"
)

// NewsTable for news
const NewsTable = "news"

// NewsEntity for news table entity struct.
type NewsEntity struct {
	ID        int64      `json:"id" db:"id"`
	CreatedAt *time.Time `json:"created_at" db:"created_at"`
	UpdatedAt *time.Time `json:"updated_at" db:"updated_at"`
	DeletedAt *time.Time `json:"deleted_at" db:"deleted_at"`
	Title     *string    `json:"title" db:"title"`
	Slug      *string    `json:"slug" db:"slug"`
	Content   *string    `json:"content" db:"content"`
	Status    *string    `json:"status" db:"status"`
}

// TableName for news
func (NewsEntity) TableName() string {
	return NewsTable
}

for gorm:

// Package model of db entity
// Code generated by daheige/tbox. DO NOT EDIT!!!

package model

// UserTable for user
const UserTable = "user"

// UserEntity for user table entity struct.
type UserEntity struct {
	ID       int64  `json:"id" gorm:"column:id;primaryKey"`
	Username string `json:"username" gorm:"column:username"`
	Age      int64  `json:"age" gorm:"column:age"`
}

// TableName for user
func (UserEntity) TableName() string {
	return UserTable
}

for xorm:

// Package model of db entity
// Code generated by daheige/tbox. DO NOT EDIT!!!

package model

// UserTable for user
const UserTable = "user"

// UserEntity for user table entity struct.
type UserEntity struct {
	ID       int64  `json:"id" xorm:"id pk autoincr"`
	Username string `json:"username" xorm:"username"`
	Age      int64  `json:"age" xorm:"age"`
}

// TableName for user
func (UserEntity) TableName() string {
	return UserTable
}

xorm engine gen tab struct

tbox -dsn="root:root123456@tcp(127.0.0.1:3306)/test?checkConnLiveness=false&loc=Local&parseTime=true&readTimeout=5s&timeout=10s&writeTimeout=5s&maxAllowedPacket=0&charset=utf8mb4" -t=user -v=true -n=true -m=true -j=true -tag=xorm

code style:

// Package model of db entity
// Code generated by daheige/tbox. DO NOT EDIT!!!

package model

// UserTable for user
const UserTable = "user"

// UserEntity for user table entity struct.
type UserEntity struct {
	ID	int64	`json:"id" xorm:"id pk autoincr"`
	User	string	`json:"user" xorm:"user"`
	Name	string	`json:"name" xorm:"name"`
	UserID	int64	`json:"user_id" xorm:"user_id"`
}

// TableName for user
func (UserEntity) TableName() string {
	return UserTable
}

gorm engine gen tab struct

tbox -dsn="root:root123456@tcp(127.0.0.1:3306)/test?checkConnLiveness=false&loc=Local&parseTime=true&readTimeout=5s&timeout=10s&writeTimeout=5s&maxAllowedPacket=0&charset=utf8mb4" -t=user -v=true -n=true -m=true -j=true -tag=gorm

code style:

// Package model of db entity
// Code generated by daheige/tbox. DO NOT EDIT!!!

package model

// UserTable for user
const UserTable = "user"

// UserEntity for user table entity struct.
type UserEntity struct {
	ID	int64	`json:"id" gorm:"column:id;primaryKey"`
	User	string	`json:"user" gorm:"column:user"`
	Name	string	`json:"name" gorm:"column:name"`
	UserID	int64	`json:"user_id" gorm:"column:user_id"`
}

// TableName for user
func (UserEntity) TableName() string {
	return UserTable
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(dsn string, opts ...Option) *engine

New create an entry for engine

func RegisterType

func RegisterType(mysqlType string, goType string)

RegisterType register mysql to to golang type

Types

type Option

type Option func(t *engine)

Option option for engine

func WithEnableJsonTag

func WithEnableJsonTag() Option

WithEnableJsonTag add json tab.

func WithEnableTableNameFunc

func WithEnableTableNameFunc() Option

WithEnableTableNameFunc generate TableName method.

func WithNoNullField added in v1.1.2

func WithNoNullField() Option

WithNoNullField no null field when code gen.

func WithOutputCmd

func WithOutputCmd() Option

WithOutputCmd output struct to cmd

func WithPkgName

func WithPkgName(name string) Option

WithPkgName pkg name

func WithPkgPath

func WithPkgPath(pkgPath string) Option

WithPkgPath gen code to file dir path

func WithTagKey

func WithTagKey(tag string) Option

WithTagKey set field tag key

func WithUcFirstOnly

func WithUcFirstOnly() Option

WithUcFirstOnly first word upper.

Directories

Path Synopsis
cmd
tbox command

Jump to

Keyboard shortcuts

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