Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cosiner/Gomodel
[UNMATAINED] A lightweight, fast, orm-like library helps interactive with database
https://github.com/cosiner/Gomodel
Last synced: 20 days ago
JSON representation
[UNMATAINED] A lightweight, fast, orm-like library helps interactive with database
- Host: GitHub
- URL: https://github.com/cosiner/Gomodel
- Owner: cosiner
- License: mit
- Archived: true
- Created: 2015-04-16T13:48:44.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-08-20T05:55:26.000Z (about 7 years ago)
- Last Synced: 2024-08-03T15:06:03.314Z (3 months ago)
- Language: Go
- Homepage:
- Size: 188 KB
- Stars: 60
- Watchers: 6
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gomodel [![wercker status](https://app.wercker.com/status/9c6ef0eec7d6d217bd831bbdc3a3ace2/s "wercker status")](https://app.wercker.com/project/bykey/9c6ef0eec7d6d217bd831bbdc3a3ace2) [![GoDoc](https://godoc.org/github.com/cosiner/gomodel?status.png)](http://godoc.org/github.com/cosiner/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
```sh
$ 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](https://github.com/cosiner/gomodel/tree/master/cmd/gomodel).
There is a detailed example [User-Follow](https://github.com/cosiner/gomodel/tree/master/example/userfollow).
#### User
```Go
var (
ErrDuplicateUserName = httperrs.Conflict.NewS("user name already exists")
ErrNoUser = httperrs.NotFound.NewS("user not found")
)type User struct {
Id int64
Name string
Age intFollowings int
Followers int
}func (u *User) Add() error {
u.Followings = 0
u.Followers = 0id, 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
```Go
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.