storable

package module
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2016 License: MIT Imports: 6 Imported by: 8

README

storable - MongoDB ODM for Go

Build Status codecov.io GoDoc GitHub release

Installation

The recommended way to install storable is:

go get -u gopkg.in/src-d/storable.v1/...

storable includes a binary tool used by go generate, please be sure that $GOPATH/bin is on your $PATH

License

MIT, see LICENSE

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrResultSetClosed is throwed when you are working over a closed ResultSet
	ErrResultSetClosed = errors.New("closed resultset")
	//ErrNotFound document not found
	ErrNotFound = errors.New("document not found")
	// ErrStop if is used on a callback of a ResultSet.ForEach function the loop
	// is stopped
	ErrStop = errors.New("document not found")
)
View Source
var (
	// ErrNonNewDocument non-new documents cannot be inserted
	ErrNonNewDocument = errors.New("Cannot insert a non new document.")
	// ErrNewDocument a new documents cannot be updated
	ErrNewDocument = errors.New("Cannot updated a new document.")
	// ErrEmptyQueryInRaw an empty query cannot be used on any *Raw method
	ErrEmptyQueryInRaw = errors.New("Empty queries are not allowed on raw ops.")
	// ErrEmptyID a document without Id cannot be used with Save method
	ErrEmptyID = errors.New("A document without id is not allowed.")
)
View Source
var (
	IdField = NewField("_id", "bson.ObjectId")
)

Functions

This section is empty.

Types

type BaseQuery

type BaseQuery struct {
	// contains filtered or unexported fields
}

func NewBaseQuery

func NewBaseQuery() *BaseQuery

func (*BaseQuery) AddCriteria

func (q *BaseQuery) AddCriteria(expr bson.M)

AddCriteria adds a new mathing expression to the query, all the expressions are merged on a $and expression.

Use operators package instead of build expresion by hand:

import . "gopkg.in/src-d/storable.v1/operators"

func (q *YourQuery) FindNonZeroRecords() {
    // All the Fields are defined on the Schema generated variable
    size := NewField("size", "int")
    q.AddCriteria(Gt(size, 0))
}

func (*BaseQuery) GetCriteria

func (q *BaseQuery) GetCriteria() bson.M

GetCriteria returns a valid bson.M used internally by Store.

func (*BaseQuery) GetLimit

func (q *BaseQuery) GetLimit() int

GetLimit return the current limit preferences of the query.

func (*BaseQuery) GetSelect

func (q *BaseQuery) GetSelect() Select

GetSelect return the current select preferences of the query.

func (*BaseQuery) GetSkip

func (q *BaseQuery) GetSkip() int

GetSkip return the current skip preferences of the query.

func (*BaseQuery) GetSort

func (q *BaseQuery) GetSort() Sort

GetSort return the current sorting preferences of the query.

func (*BaseQuery) Limit

func (q *BaseQuery) Limit(l int)

Limit sets the limit of the query.

func (*BaseQuery) Select

func (q *BaseQuery) Select(projection Select)

Select specifies the fields to return using projection operators. http://docs.mongodb.org/manual/reference/operator/projection/

func (*BaseQuery) Skip

func (q *BaseQuery) Skip(s int)

Skip sets the skip of the query.

func (*BaseQuery) Sort

func (q *BaseQuery) Sort(s Sort)

Sort sets the sorting cristeria of the query.

func (*BaseQuery) String

func (q *BaseQuery) String() string

Strings return a json representation of the criteria. Sorry but this is not fully compatible with the MongoDb CLI.

type Dir

type Dir int
const (
	Asc  Dir = 1
	Desc Dir = -1
)

type Document

type Document struct {
	Id bson.ObjectId `bson:"_id" json:"_id"`
	// contains filtered or unexported fields
}

func (*Document) GetId

func (d *Document) GetId() bson.ObjectId

GetId returns the document id.

func (*Document) IsNew

func (d *Document) IsNew() bool

IsNew returns if this document is new or not.

func (*Document) SetId

func (d *Document) SetId(id bson.ObjectId)

SetId sets the document id.

func (*Document) SetIsNew

func (d *Document) SetIsNew(isNew bool)

SetIsNew configures is this document is new in the store or not, dont mess with this if you dont want have duplicate records on your database.

type DocumentBase

type DocumentBase interface {
	GetId() bson.ObjectId
	SetId(bson.ObjectId)
	IsNew() bool
	SetIsNew(isNew bool)
}

type Field

type Field struct {
	// contains filtered or unexported fields
}

func NewField

func NewField(bson, typ string) Field

NewField return a new Field instance.

func (Field) String

func (f Field) String() string

String returns a string with a valid representation to be used on mgo.

func (Field) Type

func (f Field) Type() string

Type returns the type of this Field.

type FieldSelect

type FieldSelect struct {
	F Field
	D Filter
}

type FieldSort

type FieldSort struct {
	F Field
	D Dir
}

type Filter

type Filter int
const (
	Include Filter = 1
	Exclude Filter = 0
)

type Map

type Map struct {
	// contains filtered or unexported fields
}

func NewMap

func NewMap(bson, typ string) Map

NewMap return a new Map instance.

func (Map) Key

func (f Map) Key(key string) Field

Key returns a Field for the specific map key.

func (Map) Type

func (f Map) Type() string

Type returns the type used by this Map.

type Query

type Query interface {
	GetCriteria() bson.M
	Sort(s Sort)
	Limit(l int)
	Skip(s int)
	Select(p Select)
	GetSort() Sort
	GetLimit() int
	GetSkip() int
	GetSelect() Select
}

type ResultSet

type ResultSet struct {
	IsClosed bool
	// contains filtered or unexported fields
}

ResultSet contains the result of an executed query command.

func (*ResultSet) All

func (r *ResultSet) All(result interface{}) error

All returns all the documents in the ResultSet and close it. Dont use it with large results.

func (*ResultSet) Close

func (r *ResultSet) Close() error

Close close the ResultSet closing the internal iter.

func (*ResultSet) Count

func (r *ResultSet) Count() (int, error)

Count returns the total number of documents in the ResultSet. Count DON'T close the ResultSet after be called.

func (*ResultSet) Next

func (r *ResultSet) Next(doc interface{}) (bool, error)

Next return a document from the ResultSet, can be called multiple times.

func (*ResultSet) One

func (r *ResultSet) One(doc interface{}) error

One return a document from the ResultSet and close it, the following calls to One returns ErrResultSetClosed error. If a document is not returned the error ErrNotFound is retuned.

type Select

type Select []FieldSelect

func (Select) IsEmpty

func (s Select) IsEmpty() bool

IsEmpty returns if this select is empty or not

func (Select) ToMap

func (s Select) ToMap() bson.M

type Sort

type Sort []FieldSort

func (Sort) IsEmpty

func (s Sort) IsEmpty() bool

IsEmpty returns if this sort map is empty or not

func (Sort) ToList

func (s Sort) ToList() []string

ToList returns a representation of Sort compatible with the format of mgo.

type Store

type Store struct {
	// contains filtered or unexported fields
}

func NewStore

func NewStore(db *mgo.Database, collection string) *Store

NewStore returns a new Store instance

func (*Store) Count

func (s *Store) Count(q Query) (int, error)

Count executes the given query in the collection and returns the count

func (*Store) Delete

func (s *Store) Delete(doc DocumentBase) error

Delete remove the document from the collection

func (*Store) Find

func (s *Store) Find(q Query) (*ResultSet, error)

Find executes the given query in the collection

func (*Store) Insert

func (s *Store) Insert(doc DocumentBase) error

Insert insert the given document in the collection, returns error if no-new document is given. The document id is setted if is empty.

func (*Store) MustCount

func (s *Store) MustCount(q Query) int

MustCount like Count but panics on error

func (*Store) MustFind

func (s *Store) MustFind(q Query) *ResultSet

MustFind like Find but panics on error

func (*Store) RawDelete

func (s *Store) RawDelete(query Query, multi bool) error

RawDelete performes a direct remove in the collection. If a query without criteria is given EmptyQueryInRawErr is returned

func (*Store) RawUpdate

func (s *Store) RawUpdate(query Query, update interface{}, multi bool) error

RawUpdate performes a direct update in the collection, update is wrapped on a $set operator. If a query without criteria is given EmptyQueryInRawErr is returned

func (*Store) Save

func (s *Store) Save(doc DocumentBase) (updated bool, err error)

Save insert or update the given document in the collection, a document with id should be provided. Upsert is used (http://godoc.org/gopkg.in/mgo.v2#Collection.Upsert)

func (*Store) Update

func (s *Store) Update(doc DocumentBase) error

Update update the given document in the collection, returns error if a new document is given.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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