Socket
Book a DemoInstallSign in
Socket

github.com/youngchan1988/ormgenc

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/youngchan1988/ormgenc

v0.0.4
Source
Go
Version published
Created
Source

通过反转数据库表信息自动生成ORM代码

1. 安装

go get github.com/youngchan1988/ormgenc

2. 使用

Clone : git@github.com:youngchan1988/ormgenc.git

执行 :

$ cd ormgenc
$ go install
$ ormgenc --help
Usage of ormgenc:
  -config string
        set config file path (default "./config.yaml")
  -db string
        set target database which like 'postgresql', 'mysql', 'sqlite' and so on (default "postgresql")
  -debug
        sets log level to debug
  -help
        ormgenc usage
  -orm string
        set target orm which like 'gorm' and 'xorm' (default "gorm")
  -out string
        set output path (default ".")
  -version
        show current version

3. 配置

配置文件中包含连接数据库的信息:

db_host: "127.0.0.1"
db_name: "apptoygodb"
db_user: "newcore"
db_password: "xinheyun2014@0711"
db_port: 5433

4. Entity <-> Model

生成的ORM代码中包含两个方法:EntityToModelModelToEntity ,方便在业务Entity和数据库Model 相互转换。

比如:

//Define User struct
type UserEntity struct {
	ID int `json:"id" ormgen:"Id"`
	Name     string `json:"name" ormgen:"Name"`
	MobilePhone string `json:"mobilePhone" ormgen:"MobilePhone"`
	Email string `json:"email" ormgen:"Email"`
	State int `json:"state" ormgen:"State"`
	Info *UserInfoEntity `json:"info" ormgen:"Info"`
	CreatedDate time.Time `json:"createdDate" ormgen:"CreatedDate"`
	UpdatedDate time.Time `json:"updatedDate" ormgen:"UpdatedDate"`
	DeletedDate time.Time `json:"deleteDate" ormgen:"DeletedDate"`
}

//UserInfoEntity 
type UserInfoEntity struct {
	Avatar string `json:"avatar"`
}

type UserDBModel struct {

	Id int `gorm:"column:id;primaryKey"`

	Name string `gorm:"column:name"`

	Password string `gorm:"column:password"`

	MobilePhone string `gorm:"column:mobile_phone"`

	Email string `gorm:"column:email"`

	State int `gorm:"column:state"`

	Info *pgtype.JSONB `gorm:"column:info"`

	CreatedDate time.Time `gorm:"column:created_date;autoCreateTime"`

	UpdatedDate time.Time `gorm:"column:updated_date;autoUpdateTime"`

	DeletedDate gorm.DeletedAt `gorm:"column:deleted_date;index"`
}

在Entity中使用ormgen struct tag 来标识这个字段在数据库Model中对应的名称,针对Postgresql数据支持jsonb数据类型的转换

5.Todo

orm

  • gorm

  • xorm

database

  • postgresql

  • mysql

FAQs

Package last updated on 07 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.