Documentation ¶
Overview ¶
Package mr eases interactions with MongoDB.
This package aims to abstract away some of of the "uninteresting" parts of mgo. It also provides some interfaces that help when testing.
For usage examples see the readme: https://github.com/mhutter/mr/blob/master/README.md
Index ¶
- type Base
- type ErrInvalidURL
- type ErrNoObjectID
- type Model
- type MongoRepo
- func (r *MongoRepo) C(name string) *mgo.Collection
- func (r MongoRepo) CollectionFor(obj interface{}) *mgo.Collection
- func (r MongoRepo) CollectionNameFor(obj interface{}) string
- func (r MongoRepo) Delete(object Model) error
- func (r MongoRepo) Find(query bson.M, result interface{}) error
- func (r MongoRepo) FindAll(result interface{}) error
- func (r MongoRepo) FindID(id string, result interface{}) error
- func (r MongoRepo) FindOne(query bson.M, result interface{}) error
- func (r MongoRepo) Insert(object Model) error
- func (r MongoRepo) Update(object Model) error
- func (r MongoRepo) UpdateID(id string, object Model) error
- type Repository
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Base ¶
type Base struct { ID bson.ObjectId `bson:"_id" json:"id"` CreatedAt time.Time `bson:"created_at" json:"created_at,omitempty"` UpdatedAt time.Time `bson:"updated_at" json:"updated_at,omitempty"` }
Base contains common attributes that are common to all model structs that are to be stored in the database. It also implements the Model interface, which is used when interacting with the repository.
See package documentation for usage.
type ErrInvalidURL ¶
type ErrInvalidURL string
ErrInvalidURL is returned when the given connection URL could not be parsed
func (ErrInvalidURL) Error ¶
func (e ErrInvalidURL) Error() string
type ErrNoObjectID ¶
type ErrNoObjectID string
ErrNoObjectID is returned when you try to use a method that expects an object id with an ID that is not a valid ObjectID. See `bson.IsObjectIdHex`.
func (ErrNoObjectID) Error ¶
func (e ErrNoObjectID) Error() string
type Model ¶
type Model interface {
// contains filtered or unexported methods
}
Model must be implemented by types that should be inserted into the Repository
type MongoRepo ¶
MongoRepo interacts with MongoDB
func (MongoRepo) CollectionFor ¶
func (r MongoRepo) CollectionFor(obj interface{}) *mgo.Collection
CollectionFor returns the appropriate collection for the given object, based on the return value of "CollectionNameFor".
func (MongoRepo) CollectionNameFor ¶
CollectionNameFor determines the collection name for the given object. It naively converts the to lower case and appends an "s".
func (MongoRepo) Delete ¶
Delete removes the document with the same ID as the given one.
Note that there is no "DeleteID" method as we need the object to determine the collection name anyway...
func (MongoRepo) FindID ¶
FindID returns one record by its ObjectID. Returns an ErrNoObjectID if "id" is not a valid ObjectID.
func (MongoRepo) Insert ¶
Insert generates an ID for `object`, updates date fields and inserts it into `coll`
type Repository ¶
type Repository interface { Insert(object Model) error FindAll(result interface{}) error Find(query bson.M, result interface{}) error FindOne(query bson.M, result interface{}) error FindID(id string, result interface{}) error Update(object Model) error UpdateID(id string, object Model) error Delete(object Model) error }
Repository interacts with the actual database
func Autoconnect ¶
func Autoconnect(fallbackDBName string) (Repository, error)
Autoconnect tries to determine its DB URI from looking at the MONGODB_URI environment variable. If it is not set or empty it falls back to connecting to localhost, using "fallbackDBName" as the Database to use.
func Connect ¶
func Connect(url string) (Repository, error)
Connect dials MongoDB and returns the configured Repository
func MustAutoconnect ¶
func MustAutoconnect(fallbackDBName string) Repository
MustAutoconnect works similar to Autoconnect, but aborts the programm if connection fails.