mongo

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2018 License: MIT Imports: 4 Imported by: 0

README

mongo

GoDoc Build Status codecov Go Report Card

Simple ODM wraps mgo with interface.

TODO

  • 0.1.0 release
  • Test cgo getoverage
  • 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

type user struct {
	ID       bson.ObjectId `bson:"_id,omitempty"`
	Name     string        `bson:"name"`
	LastIP   string        `bson:"last_ip"`
	LastTime time.Time     `bson:"last_time"`
}

type userSelector struct {
	ID   *bson.ObjectId   `bson:"_id"`
	IDs  *[]bson.ObjectId `bson:"_id"`
	Name *string          `bson:"name"`
}

func (us *userSelector) Database() string {
	return "test"
}

func (us *userSelector) Collection() string {
	return "users"
}

func main() {
	sess, err := mgo.DialWithTimeout("127.0.0.1", 2*time.Second)
	if err != nil {
		panic(err)
	}
	store := mongo.NewStorage(sess)
	err = store.Query(&userSelector{}).Insert(user{Name: "hello"})
	if err != nil {
		panic(err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection

type Collection interface {
	Database() string
	Collection() string
}

Collection to show database and collection.

type QuerySet

type QuerySet interface {
	Insert(docs ...interface{}) error
	Update(update interface{}) error
	UpdateAll(update interface{}) (info *mgo.ChangeInfo, err error)
	Upsert(update interface{}) (info *mgo.ChangeInfo, err error)
	Remove() error
	RemoveAll() (info *mgo.ChangeInfo, err error)
	All(result interface{}) error
	One(result interface{}) error
	Count() (int, error)
	Limit(n int) QuerySet
	Skip(n int) QuerySet
	Prefetch(p float64) QuerySet
	Sort(fields ...string) QuerySet
}

QuerySet likes mgo.Query.

type Storage

type Storage interface {
	Query(Collection) QuerySet
	Raw(Collection, bson.M) QuerySet
}

Storage supply a interface for make Queryset.

func NewStorage

func NewStorage(sess *mgo.Session) Storage

NewStorage create a new mongodb storage.

Directories

Path Synopsis
Package mock_mongo is a generated GoMock package.
Package mock_mongo is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL