gomodel alternatives and similar packages
Based on the "ORM" category.
Alternatively, view gomodel alternatives based on common mentions on social networks and blogs.
-
upper.io/db
Data Access Layer (DAL) for PostgreSQL, CockroachDB, MySQL, SQLite and MongoDB with ORM-like features. -
xorm
xorm是一个简单而强大的Go语言ORM库,通过它可以使数据库操作非常简便。本库是基于原版xorm的定制增强版本,为xorm提供类似ibatis的配置文件及动态SQL支持,支持AcitveRecord操作 -
go-queryset
100% type-safe ORM for Go (Golang) with code generation and MySQL, PostgreSQL, Sqlite3, SQL Server support. GORM under the hood. -
golobby/orm
A lightweight yet powerful, fast, customizable, type-safe object-relational mapper for the Go programming language. -
lore
Light Object-Relational Environment (LORE) provides a simple and lightweight pseudo-ORM/pseudo-struct-mapping environment for Go
SaaSHub - Software Alternatives and Reviews
Do you think we are missing an alternative of gomodel or a related project?
README
gomodel
gomodel provide another method to interact with database.
Instead of reflection, use bitset represent fields of CRUD, sql/stmt cache and generate model code for you, high performance.
Install
$ go get github.com/cosiner/gomodel
$ cd /path/of/gomodel/cmd/gomodel
$ go install # it will install the gomodel binary file to your $GOPATH/bin
$ gomodel -cp # copy model.tmpl to default path $HOME/.config/go/model.tmpl
# or just put it to your model package, gomodel will search it first
$ # if need customed template, just copy model.tmpl to your models directory
The gomodel cmd tool and SQL convertion for structures.
There is a detailed example User-Follow.
User
var (
ErrDuplicateUserName = httperrs.Conflict.NewS("user name already exists")
ErrNoUser = httperrs.NotFound.NewS("user not found")
)
type User struct {
Id int64
Name string
Age int
Followings int
Followers int
}
func (u *User) Add() error {
u.Followings = 0
u.Followers = 0
id, err := DB.Insert(u, userFieldsExcpId, gomodel.RES_ID)
err = dberrs.DuplicateKeyError(err, UserNameCol, ErrDuplicateUserName)
if err == nil {
u.Id = id
}
return err
}
func DeleteUserById(id int64) error {
c, err := DB.ArgsDelete(userInstance, USER_ID, id)
return dberrs.NoAffects(c, err, ErrNoUser)
}
func (u *User) Update() error {
c, err := DB.Update(u, USER_NAME|USER_AGE, USER_ID)
return dberrs.NoAffects(c, err, ErrNoUser)
}
func (u *User) ById() error {
err := DB.One(u, userFieldsExcpId, USER_ID)
return dberrs.NoRows(err, ErrNoUser)
}
func UsersByAge(age, start, count int) ([]User, error) {
users := userStore{
Fields: userFieldsAll,
}
err := DB.ArgsLimit(&users, userInstance, userFieldsAll, USER_AGE, age, start, count)
return users.Values, dberrs.NoRows(err, ErrNoUser)
}
func AllUsersByAge(age int) ([]User, error) {
users := userStore{
Fields: userFieldsAll,
}
err := DB.ArgsAll(&users, userInstance, userFieldsAll, USER_AGE, age)
return users.Values, dberrs.NoRows(err, ErrNoUser)
}
Follow
var (
ErrFollowed = httperrs.Conflict.NewS("already followed")
ErrNonFollow = httperrs.NotFound.NewS("non follow")
)
type Follow struct {
UserId int64 `table:"user_follow"`
FollowUserId int64
}
//gomodel insertUserFollowSQL = [
// INSERT INTO Follow(UserId, FollowUserId)
// SELECT ?, ? FROM DUAL
// WHERE EXISTS(SELECT Id FROM User WHERE Id=?)
//]
func (f *Follow) Add() error {
return f.txDo(DB, func(tx gomodel.Tx, f *Follow) error {
c, err := tx.UpdateById(insertUserFollowSQL, gomodel.FieldVals(f, followFieldsAll, f.FollowUserId)...)
err = dberrs.NoAffects(c, err, ErrNoUser)
err = dberrs.DuplicateKeyError(err, dberrs.PRIMARY_KEY, ErrFollowed)
return f.updateFollowInfo(tx, err, 1)
})
}
func (f *Follow) Delete() error {
return f.txDo(DB, func(tx gomodel.Tx, f *Follow) error {
c, err := tx.Delete(f, followFieldsAll)
err = dberrs.NoAffects(c, err, ErrNonFollow)
return f.updateFollowInfo(tx, err, -1)
})
}
func (f *Follow) updateFollowInfo(tx gomodel.Tx, err error, c int) error {
if err == nil {
_, err = tx.ArgsIncrBy(userInstance, USER_FOLLOWINGS, USER_ID, c, f.UserId)
if err == nil {
_, err = tx.ArgsIncrBy(userInstance, USER_FOLLOWERS, USER_ID, c, f.FollowUserId)
}
}
return err
}
LICENSE
MIT.
*Note that all licence references and agreements mentioned in the gomodel README section above
are relevant to that project's source code only.