mr

package module
v0.0.0-...-439d8c1 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2018 License: MIT Imports: 9 Imported by: 0

README

MongoRepo

GoDoc Build Status

Library to ease interaction with MongoDB.

This package is an opinionated abstraction for mgo.

To get started, connect to MongoDB (uses $MONGODB_URI from environment if set, or localhost):

repo := mr.MustAutoconnect("db-name")

Include the "Base" type in your data types:

type User struct {
        mr.Base `json:",inline" bson:",inline"`

        Name         string `bson:"name" json:"name"`
        Email        string `bson:"email" json:"email"`
        PasswordHash []byte `bson:"password_hash" json:"-"`
}

Doing so will add the following fields to your type:

  • ID: ObjectId of the document in the DB
  • CreatedAt: Timestamp of first insertion into DB
  • UpdatedAt: Timestamp of the last update operations

The "MongoCollection" type implements the basic CRUD operations defined in the "Colleciton" interface. IDs ,must be passed as strings, and objects are usually types that include the "Base" type (see above).

user := User{
        Name: "John",
}
err := repo.Insert(&user)

var users User
err := repo.FindAll(&users)

Manuel Hutter - GitHub @mhutter - Twitter @dratir

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

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

type MongoRepo struct {
	*mgo.Database
}

MongoRepo interacts with MongoDB

func (*MongoRepo) C

func (r *MongoRepo) C(name string) *mgo.Collection

C returns a Collection

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

func (r MongoRepo) CollectionNameFor(obj interface{}) string

CollectionNameFor determines the collection name for the given object. It naively converts the to lower case and appends an "s".

func (MongoRepo) Delete

func (r MongoRepo) Delete(object Model) error

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

func (r MongoRepo) Find(query bson.M, result interface{}) error

Find all documents based on query

func (MongoRepo) FindAll

func (r MongoRepo) FindAll(result interface{}) error

FindAll return all objects in `coll`

func (MongoRepo) FindID

func (r MongoRepo) FindID(id string, result interface{}) error

FindID returns one record by its ObjectID. Returns an ErrNoObjectID if "id" is not a valid ObjectID.

func (MongoRepo) FindOne

func (r MongoRepo) FindOne(query bson.M, result interface{}) error

FindOne returns the first item selected by "query"

func (MongoRepo) Insert

func (r MongoRepo) Insert(object Model) error

Insert generates an ID for `object`, updates date fields and inserts it into `coll`

func (MongoRepo) Update

func (r MongoRepo) Update(object Model) error

Update updates the document with the same ID as the given one

func (MongoRepo) UpdateID

func (r MongoRepo) UpdateID(id string, object Model) error

UpdateID updates the document with the given ID. It makes sure the ID is not changed and the "updated_at" is updated.

Returns an ErrNoObjectID if "id" is not a valid ObjectID.

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.

Jump to

Keyboard shortcuts

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