storable

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2015 License: MIT Imports: 7 Imported by: 0

README

storable - MongoDB ODM for Go

Build Status Coverage Status GoDoc

License

MIT, see LICENSE

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NonNewDocumentErr  = errors.New("Cannot insert a non new document.")
	NewDocumentErr     = errors.New("Cannot updated a new document.")
	EmptyQueryInRawErr = errors.New("Empty queries are not allowed on raw ops.")
	EmptyIdErr         = errors.New("A document without id is not allowed.")
)
View Source
var (
	IdField = NewField("_id", "bson.ObjectId")
)
View Source
var (
	ResultSetClosed = errors.New("Cannot close a closed resultset.")
)

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 . "github.com/tyba/storable/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

GetSort return the current limit preferences of the query.

func (*BaseQuery) GetSkip

func (q *BaseQuery) GetSkip() int

GetSort 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) 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 FieldSort

type FieldSort struct {
	F Field
	D Dir
}

type HookError added in v0.2.0

type HookError struct {
	Hook  string // Name of the hook method that returned the cause.
	Field string // Path from the root to the field that caused the error, dot-separated.
	Cause error  // The wrapped error.
}

A HookError wraps an error returned from a call to a hook method.

func (HookError) Error added in v0.2.0

func (e HookError) Error() string

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)
	GetSort() Sort
	GetLimit() int
	GetSkip() int
}

type ResultSet

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

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.

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{}) (bool, error)

One return a document from the ResultSet and close it, the following calls to One returns ResultSetClosed error.

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) String added in v0.2.0

func (s Sort) String() string

String 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) 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) 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