Go-ORM is a simple ORM framework, auto-generate Model & CRUD methods based on model yml
- Auto create table
- Model & CRUD methods generator
- Transaction
- Pagination
- Connection Pool
- Write/Read Splitting
- Multi datasources
- Auto/Customized mapping of Model and datasource/table
- SQL log & Slow SQL log for profiling
sudo rm -fr /usr/local/go
Download & Install MacOS pkg from https://golang.org/dl/
export PATH=$PATH:/usr/local/go/binwget https://golang.org/dl/go1.15.1.linux-amd64.tar.gz
sudo tar -C /usr/local/ -xzvf go1.15.1.linux-amd64.tar.gz
sudo vi /etc/profile
export GOROOT=/usr/local/go
export GOPATH=/data/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT/bin
export PATH=$PATH:$GOPATH/binDefine Datasources in datasource.yml
datasources:
- name: defaultwrite: root:root@tcp(127.0.0.1:3306)/my_db_0?charset=utf8mb4&parseTime=Trueread: root:root@tcp(127.0.0.1:3306)/my_db_0?charset=utf8mb4&parseTime=True
- name: ds_2write: root:root@tcp(127.0.0.1:3306)/my_db_0?charset=utf8mb4&parseTime=Trueread: root:root@tcp(127.0.0.1:3306)/my_db_0?charset=utf8mb4&parseTime=Truesql_log: falseslow_sql_log: 500Define Models in model.yml
models:
- model: Username: stringcreated_at: time.Time
- model: Eventname: stringcreated_at: time.TimeGenerate Model go files
go run gen.goWhich will generate Model files
-- Generate model/User.go
typeUserModelstruct {
OdBstring`json:"-"`Lmtint`json:"-"`Ofsint`json:"-"`Datasourcestring`json:"-"`Tablestring`json:"-"`Trx*Tx`json:"-"`IDint64`json:"id"`Namestring`json:"name"`CreatedAt time.Time`json:"created_at"`
}
...func (m*UserModel) Find(idint64) (*UserModel, error) {
...
}
...varUser=UserModel{Datasource: "default", Table: "user"}You can use your User Model now:
package main
import (
"fmt""time"
. "github.com/hide2/go-orm/model"
)
funcmain() {
// Exec SQLfmt.Println("[Drop Table user]")
User.Exec("DROP TABLE IF EXISTS user")
// Create Tablefmt.Println("[Create Table user]")
User.CreateTable()
// Cu:=User.New()
fmt.Println("[New]", u)
u.Name="John"u.CreatedAt=time.Now()
u.Save()
fmt.Println("[Save]", u)
// Ru, e:=User.Find(123)
fmt.Println("[Find(123)]", u, e)
u, _=User.Find(1)
fmt.Println("[Find(1)]", u)
// Uu.Name="Calvin"u.Save()
fmt.Println("[Update]", u)
u, _=User.Find(1)
fmt.Println("[Find(1)]", u)
// Du.Delete()
u, _=User.Find(1)
fmt.Println("[After Delete Find(1)]", u)
// Exec SQLfmt.Println("[Drop Table user]")
User.Exec("DROP TABLE IF EXISTS user")
// Create Tablefmt.Println("[Create Table user]")
User.CreateTable()
// Createfori:=0; i<20; i++ {
props:=map[string]interface{}{"name": "Dog", "created_at": time.Now()}
u, _=User.Create(props)
fmt.Println("[Create]", u)
}
// WHEREconds:=map[string]interface{}{"name": "Dog"}
us, _:=User.Where(conds)
for_, v:=rangeus {
fmt.Println("[Where]", *v)
}
// UPDATEprops2:=map[string]interface{}{"name": "Cat"}
conds2:=map[string]interface{}{"name": "Dog"}
User.Update(props2, conds2)
us2, _:=User.Where(props2)
fmt.Println("[Update]", len(us2))
// COUNTc, _:=User.CountAll()
fmt.Println("[CountAll]", c)
c, _=User.Count(props2)
fmt.Println("[Count]", c)
// OrderBy&Limit&Paginateus3, _:=User.All()
fmt.Println("[All]", len(us3))
us4, _:=User.OrderBy("ID DESC").Offset(2).Limit(3).All()
for_, v:=rangeus4 {
fmt.Println("[OrderBy]", *v)
}
us5, _:=User.Offset(0).Limit(5).Where(props2)
for_, v:=rangeus5 {
fmt.Println("[Offset/Limit]", *v)
}
us6, _:=User.OrderBy("ID ASC, Name ASC").Page(1, 10).Where(props2)
for_, v:=rangeus6 {
fmt.Println("[Page]", *v)
}
// Tx-CommitUser.Begin()
fori:=0; i<10; i++ {
props:=map[string]interface{}{"name": fmt.Sprintf("%s%d", "Dog", i+1), "created_at": time.Now()}
User.Create(props)
}
User.Destroy(25)
User.Commit()
u, e=User.Find(30)
fmt.Println("[Find]", u, e)
// Tx-RollbackUser.Begin()
fori:=0; i<10; i++ {
props:=map[string]interface{}{"name": fmt.Sprintf("%s%d", "Dog", i+1), "created_at": time.Now()}
User.Create(props)
}
User.Rollback()
u, e=User.Find(40)
fmt.Println("[Find]", u, e)
}