Documentation
¶
Index ¶
- Variables
- func BuildIndex(p ParsedIndex) *mgo.Index
- func GetBsonName(field reflect.StructField) string
- func GetChangedFields(struct1 interface{}, struct2 interface{}, useBson bool) ([]string, error)
- func MakeDoc(d interface{}) interface{}
- func NewDoc(d interface{}) interface{}
- func Populate(doc Document, query interface{}, ref string)
- func Remove(doc Document) error
- func RemoveAll(docs []Document) map[bson.ObjectId]error
- func RemoveAllBySelector(selectors map[Model]interface{}) map[string]*ChangeInfoWithError
- func RemoveBySelector(model Model, selector interface{}) error
- func Save(doc Document) error
- func TrimAllSpaces(src string) string
- func ValidateInclusionIn(value string, options []string) bool
- func ValidateMongoIDRef(id bson.ObjectId, collection *Collection) bool
- func ValidateRequired(val interface{}) bool
- func ValueOf(d interface{}) reflect.Value
- type AfterDeleteHook
- type AfterFindHook
- type AfterSaveHook
- type BeforeDeleteHook
- type BeforeSaveHook
- type ChangeInfoWithError
- type Collection
- func (c *Collection) C() *mgo.Collection
- func (c *Collection) Find(query interface{}) *Query
- func (c *Collection) FindID(id interface{}) *Query
- func (c *Collection) PreSave(doc Document) error
- func (c *Collection) Remove(doc Document) error
- func (c *Collection) RemoveAll(docs []Document) map[bson.ObjectId]error
- func (c *Collection) RemoveAllBySelector(selectors map[Model]interface{}) map[string]*ChangeInfoWithError
- func (c *Collection) RemoveBySelector(selector interface{}) error
- func (c *Collection) Save(doc Document) error
- type Config
- type Connection
- type Context
- type DiffTracker
- func (d *DiffTracker) Clear()
- func (d *DiffTracker) Compare(useBson bool) (bool, []string, error)
- func (d *DiffTracker) GetModified(useBson bool) (bool, []string)
- func (d *DiffTracker) GetOriginalValue(field string) (interface{}, error)
- func (d *DiffTracker) Modified(field string) bool
- func (d *DiffTracker) NewSession(useBsonTags bool) (*DiffTrackingSession, error)
- func (d *DiffTracker) Reset()
- func (d *DiffTracker) SetOriginal(orig interface{})
- type DiffTrackingSession
- type Document
- type DocumentModel
- func (d *DocumentModel) AsDocument() Document
- func (d *DocumentModel) AsModel() Model
- func (d *DocumentModel) AsNew()
- func (d *DocumentModel) BsonID() *bson.M
- func (d *DocumentModel) Find(query interface{}) *Query
- func (d *DocumentModel) FindByID(id interface{}, result interface{}) error
- func (d *DocumentModel) FindID(id interface{}) *Query
- func (d *DocumentModel) FindOne(query interface{}, result interface{}) error
- func (d *DocumentModel) GetAllIndex() []*mgo.Index
- func (d *DocumentModel) GetAllParsedIndex() map[string][]ParsedIndex
- func (d *DocumentModel) GetCInfo() *mgo.ChangeInfo
- func (d *DocumentModel) GetColl() *Collection
- func (d *DocumentModel) GetCollName() string
- func (d *DocumentModel) GetConn() *Connection
- func (d *DocumentModel) GetCreated() time.Time
- func (d *DocumentModel) GetID() bson.ObjectId
- func (d *DocumentModel) GetIndex(name string) []*mgo.Index
- func (d *DocumentModel) GetMe() (iname string, me interface{})
- func (d *DocumentModel) GetModified() time.Time
- func (d *DocumentModel) GetParsedIndex(name string) []ParsedIndex
- func (d *DocumentModel) GetRefIndex(name string) RefIndex
- func (d *DocumentModel) IsNew() bool
- func (d *DocumentModel) Populate(f string) *Query
- func (d *DocumentModel) Ref(name string) RefIndex
- func (d *DocumentModel) Remove() error
- func (d *DocumentModel) Save() error
- func (d *DocumentModel) SetCInfo(ci *mgo.ChangeInfo)
- func (d *DocumentModel) SetCollName(name string)
- func (d *DocumentModel) SetConn(c *Connection)
- func (d *DocumentModel) SetCreated(t time.Time)
- func (d *DocumentModel) SetID(id bson.ObjectId)
- func (d *DocumentModel) SetIsNew(isNew bool)
- func (d *DocumentModel) SetMe(iname string, me interface{})
- func (d *DocumentModel) SetModified(t time.Time)
- type Iter
- type Model
- type ModelInternals
- type ModelReg
- func (r ModelReg) Exists(i interface{}) (string, *ModelInternals, bool)
- func (r ModelReg) ExistsByName(n string) (string, *ModelInternals, bool)
- func (r ModelReg) Field(i int, d interface{}) reflect.Value
- func (r ModelReg) Index(n string) int
- func (r ModelReg) New(n string) interface{}
- func (r ModelReg) Refs(n string) map[string]RefIndex
- func (r ModelReg) Register(i ...interface{})
- func (r ModelReg) SearchRef(i interface{}, n string) (*ModelInternals, *RefIndex)
- func (r ModelReg) TypeOf(n string) reflect.Type
- type NewTracker
- type Paginate
- type ParsedIndex
- type PopulateInfo
- type Query
- func (q *Query) All(result interface{}) error
- func (q *Query) C() *mgo.Collection
- func (q *Query) Find(query interface{}) *Query
- func (q *Query) Iter() *Iter
- func (q *Query) Limit(n int) *Query
- func (q *Query) One(result interface{}) error
- func (q *Query) Paginate(n int) *Query
- func (q *Query) Q() *mgo.Query
- func (q *Query) Skip(n int) *Query
- type RefField
- type RefFieldSlice
- type RefIndex
- type Registry
- type Stringer
- type TimeCreatedTracker
- type TimeModifiedTracker
- type Trackable
- type ValidateHook
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
var ModelRegistry = make(ModelReg, 0)
ModelRegistry is the centralized registry of all models used for the app
Functions ¶
func BuildIndex ¶
func BuildIndex(p ParsedIndex) *mgo.Index
BuildIndex build an mgo Index using the values of a ParsedIndex struct
func GetChangedFields ¶
GetChangedFields ...
func MakeDoc ¶
func MakeDoc(d interface{}) interface{}
MakeDoc (no returning new) adds the DocumentModel to an existant interface ((TODO))
func RemoveAllBySelector ¶
func RemoveAllBySelector(selectors map[Model]interface{}) map[string]*ChangeInfoWithError
RemoveAllBySelector is convenience method for Collection.RemoveAllBySelector
func RemoveBySelector ¶
RemoveBySelector is convenience method for Collection.RemoveBySelector The model argument here is used to reference its Collection to call the mgo driver on it.
func TrimAllSpaces ¶
TrimAllSpaces removes all spaces from the passed string and returns the trimmed string
func ValidateInclusionIn ¶
ValidateInclusionIn ...
func ValidateMongoIDRef ¶
func ValidateMongoIDRef(id bson.ObjectId, collection *Collection) bool
ValidateMongoIDRef ...
Types ¶
type BeforeDeleteHook ¶
type BeforeDeleteHook interface {
BeforeDelete() error
}
BeforeDeleteHook ...
type ChangeInfoWithError ¶
type ChangeInfoWithError struct { Info *mgo.ChangeInfo Err error }
ChangeInfoWithError is a return value for most of mgo methods
type Collection ¶
type Collection struct { Name string Database string Context *Context Connection *Connection }
Collection ...
func (*Collection) Find ¶
func (c *Collection) Find(query interface{}) *Query
Find is a wrapper to the mgo Find. This is the entry point to the Query object. Populate makes a call to this method with a special meaning
func (*Collection) FindID ¶
func (c *Collection) FindID(id interface{}) *Query
FindID is a wrapper to the mgo FindId
func (*Collection) Remove ¶
func (c *Collection) Remove(doc Document) error
Remove removes the passed document from database, executing before and after delete hooks
func (*Collection) RemoveAll ¶
func (c *Collection) RemoveAll(docs []Document) map[bson.ObjectId]error
RemoveAll removes all documents passed in slice executing, for each one, the before and after delete hooks. Document in slice can belong to different collections.
func (*Collection) RemoveAllBySelector ¶
func (c *Collection) RemoveAllBySelector(selectors map[Model]interface{}) map[string]*ChangeInfoWithError
RemoveAllBySelector is a wrapper aorund mgo.RemoveAll method.
This is faster then Collection.RemoveAll method but it doesn't run before / after delete hooks.
The selectors argument is a map[Model]interface{}, where the key is the model (collection) on which we are going to apply the selector and the interface is the mgo selector.
func (*Collection) RemoveBySelector ¶
func (c *Collection) RemoveBySelector(selector interface{}) error
RemoveBySelector is a wrapper aorund mgo.Remove method. This is faster the Collection.Remove method but it doesn't run before / after delete hooks
type Connection ¶
Connection ...
var DBConn *Connection
DBConn is the connection initialized after Connect is called. All underlying operations are made using this connection
func Connect ¶
func Connect(config *Config) (*Connection, error)
Connect creates a new connection and run Connect()
func (*Connection) Collection ¶
func (m *Connection) Collection(name string) *Collection
Collection ...
func (*Connection) CollectionFromDatabase ¶
func (m *Connection) CollectionFromDatabase(name string, database string) *Collection
CollectionFromDatabase ...
func (*Connection) Connect ¶
func (m *Connection) Connect() (err error)
Connect to the database using the provided config
type DiffTracker ¶
type DiffTracker struct {
// contains filtered or unexported fields
}
DiffTracker ...
func (*DiffTracker) Compare ¶
func (d *DiffTracker) Compare(useBson bool) (bool, []string, error)
Compare ...
func (*DiffTracker) GetModified ¶
func (d *DiffTracker) GetModified(useBson bool) (bool, []string)
GetModified ...
func (*DiffTracker) GetOriginalValue ¶
func (d *DiffTracker) GetOriginalValue(field string) (interface{}, error)
GetOriginalValue ...
func (*DiffTracker) Modified ¶
func (d *DiffTracker) Modified(field string) bool
Modified test on DiffTracker struct...
func (*DiffTracker) NewSession ¶
func (d *DiffTracker) NewSession(useBsonTags bool) (*DiffTrackingSession, error)
NewSession ...
func (*DiffTracker) SetOriginal ¶
func (d *DiffTracker) SetOriginal(orig interface{})
SetOriginal ...
type DiffTrackingSession ¶
DiffTrackingSession ...
func (*DiffTrackingSession) Modified ¶
func (s *DiffTrackingSession) Modified(field string) bool
Modified ...
type Document ¶
type Document interface { Model GetID() bson.ObjectId SetID(bson.ObjectId) BsonID() *bson.M AsNew() AsDocument() Document AsModel() Model SetCInfo(*mgo.ChangeInfo) GetCInfo() *mgo.ChangeInfo }
Document ...
type DocumentModel ¶
type DocumentModel struct { ID bson.ObjectId `bson:"_id,omitempty" json:"_id"` Created time.Time `bson:"_created" json:"_created"` Modified time.Time `bson:"_modified" json:"_modified"` // contains filtered or unexported fields }
DocumentModel ...
func (*DocumentModel) AsDocument ¶
func (d *DocumentModel) AsDocument() Document
AsDocument tranforms the DocumentModel to Document interface
func (*DocumentModel) AsModel ¶
func (d *DocumentModel) AsModel() Model
AsModel tranforms the DocumentModel to Document interface
func (*DocumentModel) AsNew ¶
func (d *DocumentModel) AsNew()
AsNew assigns a new ID to the current document so it can be considered as a new document.
func (*DocumentModel) BsonID ¶
func (d *DocumentModel) BsonID() *bson.M
BsonID returns the document id using bson.M interface style This method can be directly used with Find, but not with FindID which expects directly id interface{} (i.e. d.ID/d.GetID())
func (*DocumentModel) Find ¶
func (d *DocumentModel) Find(query interface{}) *Query
Find is the wrapper method to mgo Find
func (*DocumentModel) FindByID ¶
func (d *DocumentModel) FindByID(id interface{}, result interface{}) error
FindByID is a shortcut for FindID().One()
func (*DocumentModel) FindID ¶
func (d *DocumentModel) FindID(id interface{}) *Query
FindID is a wrapper to the mgo FindId
func (*DocumentModel) FindOne ¶
func (d *DocumentModel) FindOne(query interface{}, result interface{}) error
FindOne is a shortcut for Find().One()
func (*DocumentModel) GetAllIndex ¶
func (d *DocumentModel) GetAllIndex() []*mgo.Index
GetAllIndex returns the mgo.Index struct required to mgo.EnsureIndex method using the ParsedIndex information stored in the index map of the Model. TODO: discard bad formatted indexes
func (*DocumentModel) GetAllParsedIndex ¶
func (d *DocumentModel) GetAllParsedIndex() map[string][]ParsedIndex
GetAllParsedIndex returns all stored parsed indexes
func (*DocumentModel) GetCInfo ¶
func (d *DocumentModel) GetCInfo() *mgo.ChangeInfo
GetCInfo gets the document cinfo field (see mgo.upsert)
func (*DocumentModel) GetColl ¶
func (d *DocumentModel) GetColl() *Collection
GetColl implementation for Model interface
func (*DocumentModel) GetCollName ¶
func (d *DocumentModel) GetCollName() string
GetCollName implementation for the Model interface
func (*DocumentModel) GetCreated ¶
func (d *DocumentModel) GetCreated() time.Time
GetCreated gets the created date
func (*DocumentModel) GetID ¶
func (d *DocumentModel) GetID() bson.ObjectId
GetID satisfies the document interface
func (*DocumentModel) GetIndex ¶
func (d *DocumentModel) GetIndex(name string) []*mgo.Index
GetIndex returns the mgo.Index struct required to mgo.EnsureIndex method using the ParsedIndex information stored for passed field name. TODO: discard bad formatted indexes
func (*DocumentModel) GetMe ¶
func (d *DocumentModel) GetMe() (iname string, me interface{})
GetMe is used to save the iname, me fields of the document model. This is useful after returning from one of a find method where mgo driver return a freshly create zero filled struct.DocumentModel Note: mgo should consider bson "-" tag also on unmarshal, actually overwrites it
func (*DocumentModel) GetModified ¶
func (d *DocumentModel) GetModified() time.Time
GetModified gets the modified date
func (*DocumentModel) GetParsedIndex ¶
func (d *DocumentModel) GetParsedIndex(name string) []ParsedIndex
GetParsedIndex returns the index stored with the passed field name
func (*DocumentModel) GetRefIndex ¶
func (d *DocumentModel) GetRefIndex(name string) RefIndex
GetRefIndex returns the RefIndex struct for the given field
func (*DocumentModel) Populate ¶
func (d *DocumentModel) Populate(f string) *Query
Populate builds a Query to populate the referenced field (see scratch) The returned Query object refers to the target field object not the original one.
func (*DocumentModel) Ref ¶
func (d *DocumentModel) Ref(name string) RefIndex
Ref same as GetRefIndex
func (*DocumentModel) Remove ¶
func (d *DocumentModel) Remove() error
Remove removes document from database, running before and after delete hooks
func (*DocumentModel) SetCInfo ¶
func (d *DocumentModel) SetCInfo(ci *mgo.ChangeInfo)
SetCInfo sets the document cinfo field
func (*DocumentModel) SetCollName ¶
func (d *DocumentModel) SetCollName(name string)
SetCollName implementation for Model interface (why may you need to change collection name at runtime ?)
func (*DocumentModel) SetCreated ¶
func (d *DocumentModel) SetCreated(t time.Time)
SetCreated sets the created date
func (*DocumentModel) SetID ¶
func (d *DocumentModel) SetID(id bson.ObjectId)
SetID sets the ID for the document
func (*DocumentModel) SetIsNew ¶
func (d *DocumentModel) SetIsNew(isNew bool)
SetIsNew satisfies the new tracker interface
func (*DocumentModel) SetMe ¶
func (d *DocumentModel) SetMe(iname string, me interface{})
SetMe is used to set the iname and me fields
func (*DocumentModel) SetModified ¶
func (d *DocumentModel) SetModified(t time.Time)
SetModified sets the modified date
type Iter ¶
Iter is the mgo.Iter wrapper
type Model ¶
type Model interface { GetCollName() string SetCollName(name string) GetColl() *Collection GetParsedIndex(name string) []ParsedIndex GetAllParsedIndex() map[string][]ParsedIndex GetIndex(name string) []*mgo.Index GetAllIndex() []*mgo.Index GetRefIndex(name string) RefIndex GetConn() *Connection SetConn(*Connection) GetMe() (iname string, me interface{}) SetMe(iname string, me interface{}) Find(interface{}) *Query FindID(interface{}) *Query Save() error Remove() error }
Model ...
type ModelInternals ¶
type ModelInternals struct { // Idx is the index of the field containing the DM Idx int // The Type Type reflect.Type // Model internal data Collection string Indexes map[string][]ParsedIndex Refs map[string]RefIndex }
ModelInternals contains some internal information about the model
type ModelReg ¶
type ModelReg map[string]*ModelInternals
ModelReg ...
func (ModelReg) Exists ¶
func (r ModelReg) Exists(i interface{}) (string, *ModelInternals, bool)
Exists ...
func (ModelReg) ExistsByName ¶
func (r ModelReg) ExistsByName(n string) (string, *ModelInternals, bool)
ExistsByName ...
func (ModelReg) Index ¶
Index returns the index of the DocumentModel field in the struct or -1 if the struct name passed is not found
func (ModelReg) Refs ¶
Refs returns the Refs of the DocumentModel field in the struct or nil if the struct name passed is not found
type Paginate ¶
type Paginate struct { Page int `json:"page"` // Current page Pages int `json:"pages"` // Total pages N int `json:"items"` // Items per pages T int `json:"total"` // Total records in query OnPage int `json:"onPage"` // Records in current page }
Paginate ...
type ParsedIndex ¶
type ParsedIndex struct { Fields []string Options []string // contains filtered or unexported fields }
ParsedIndex contains a parsed index
func IndexScan ¶
func IndexScan(src string) []ParsedIndex
IndexScan ... TODO:
add optional parameter to pass the name of the field to be used in case of empty {} or if filled name is empty
type PopulateInfo ¶
PopulateInfo ...
type Query ¶
type Query struct { MgoC *mgo.Collection MgoQ *mgo.Query Pagination *Paginate // Populate if true says this query is a populate type query Populate bool // Query is the query used to build the mgo.Query. In case of populate query its type is bson.M // with $and operator to merge with the target id(s) of the target Model Query interface{} }
Query is the mgo.Query wrapper
func (*Query) C ¶
func (q *Query) C() *mgo.Collection
C direct access to mgo driver Collection layer
func (*Query) Find ¶
Find makes a query filter and returns a Query object. If q is a populate type of Query object append the filter to the $and array.
type RefField ¶
RefField is a reference field to another model. The receiver will return the real object.
type RefFieldSlice ¶
type RefFieldSlice []*RefField
RefFieldSlice is a slice of RefField. The receiver will return an Iterator to this field.
type RefIndex ¶
type RefIndex struct { // The docuemnt model name Model string // The referenced object name Ref string // The field index in the parsed struct Idx int // The kind of the field (slice or other) Kind reflect.Kind // The type of the field Type reflect.Type // Whenever the reference object exists in the Registry Exists bool }
RefIndex contains the object stored as reference in database
type Registry ¶
type Registry interface { Register(...interface{}) Exists(interface{}) (string, *ModelInternals, bool) ExistByName(string) (string, *ModelInternals, bool) Index(string) int TypeOf(string) reflect.Type New(string) interface{} Field(string, interface{}) interface{} }
Registry ...
type TimeCreatedTracker ¶
TimeCreatedTracker ...
type TimeModifiedTracker ¶
TimeModifiedTracker ...
type ValidationError ¶
type ValidationError struct {
Errors []error
}
ValidationError ...
func (*ValidationError) Error ¶
func (v *ValidationError) Error() string