项目作者: yeeuu

项目描述 :
Simple ODM wraps mgo with interface.
高级语言: Go
项目地址: git://github.com/yeeuu/mongo.git
创建时间: 2018-02-28T03:32:04Z
项目社区:https://github.com/yeeuu/mongo

开源协议:MIT License

下载


mongo

GoDoc
Build Status
codecov
Go Report Card

Simple ODM wraps mgo with interface.

Design

  1. Wrap MongoDB operations with interface{} for TDD development friendly. E.X. gomock
  2. Strongly-typed queryset.

Feature

  1. interface wraps for mgo.
  2. Strongly-typed queryset support.

TODO

  • 0.1.0 release
  • Test coverage
  • Travis-ci support

Limits

Do not support complex mongo query for now.

More limits need to discovery :D

All API design may change in the future until reach 1.0

Usage

  1. type user struct {
  2. ID bson.ObjectId `bson:"_id,omitempty"`
  3. Name string `bson:"name"`
  4. LastIP string `bson:"last_ip"`
  5. LastTime time.Time `bson:"last_time"`
  6. }
  7. type userSelector struct {
  8. ID *bson.ObjectId `bson:"_id"`
  9. IDs *[]bson.ObjectId `bson:"_id"`
  10. Name *string `bson:"name"`
  11. }
  12. func (us *userSelector) Database() string {
  13. return "test"
  14. }
  15. func (us *userSelector) Collection() string {
  16. return "users"
  17. }
  18. func main() {
  19. sess, err := mgo.DialWithTimeout("127.0.0.1", 2*time.Second)
  20. if err != nil {
  21. panic(err)
  22. }
  23. store := mongo.NewStorage(sess)
  24. err = store.Query(&userSelector{}).Insert(user{Name: "hello"})
  25. if err != nil {
  26. panic(err)
  27. }