cfmgo

package
v0.0.0-...-5601fd7 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2015 License: Apache-2.0, GPL-2.0 Imports: 5 Imported by: 0

README

cfmgo

A MongoDB integration package for Cloud Foundry

Overview

cfmgo is a package to assist you in connecting Go applications running on Cloud Foundry to MongoDB.

Usage

go get github.com/pivotal-pez/cfmgo

appEnv, _ := cfenv,Current() //relies on github.com/cloudfoundry-community/go-cfenv
serviceName := os.Getenv("DB_NAME")
serviceURIName := os.Getenv("DB_URI")
serviceURI := cfmgo.GetServiceBinding(serviceName, serviceURIName, appEnv)
collection := cfmgo.Connect(cfmgo.NewCollectionDialer, serviceURI, "my-collection")
cfmgo/params

params is a package that extracts query parameters from a request to be used in cfmgo.Collection.Find() operations.

Example
func ListInventoryItemsHandler(collection cfmgo.Collection) http.HandlerFunc {
	return func(w http.ResponseWriter, req *http.Request) {
		collection.Wake()

		params := params.Extract(req.URL.Query())

		items := make([]RedactedInventoryItem, 0)

		if count, err := collection.Find(params, &items); err == nil {
			Formatter().JSON(w, http.StatusOK, wrapper.Collection(&items, count))
		} else {
			Formatter().JSON(w, http.StatusNotFound, wrapper.Error(err.Error()))
		}
	}
}
cfmgo/wrapper

wrapper is a simple helper to wrap API response data and errors in a consistent structure.

//ResponseWrapper provides a standard structure for API responses.
type ResponseWrapper struct {
	//Status indicates the result of a request as "success" or "error"
	Status string `json:"status"`
	//Data holds the payload of the response
	Data interface{} `json:"data,omitempty"`
	//Message contains the nature of an error
	Message string `json:"message,omitempty"`
	//Count contains the number of records in the result set
	Count int `json:"count,omitempty"`
}
Examples

wrapper.Error(err) yields:

{
"status": "error",
"message": "error message text"
}

wrapper.One(&someRecord) yields:

{
"status": "success",
"data": {
	"id": 1,
	"name": "fluffy"
	}
}

wrapper.Collection(&someResults, count) yields:

{
"status": "success",
"data": [
	{
	"id": 1,
	"name": "fluffy"
	},
	{
	"id": 2,
	"name": "thiggy"
	}
	],
"count": 2
}

Note: count represents the total number of matching records from the query, not the number of records returned in the result set. That number is governed by limit.

Documentation

Overview

A MongoDB integration package for Cloud Foundry.

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrInvalidID -- error for invalid id
	ErrInvalidID = errors.New("value is not a properly formatted hex string")
)

Functions

func GetServiceBinding

func GetServiceBinding(serviceName string, serviceURIName string, appEnv *cfenv.App) (serviceURI string)

GetServiceBinding parses a *cfenv.App object and returns a URI for the specified service. (Refer to http://github.com/cloudfountry-community/go-cfenv for more details.)

Types

type Collection

type Collection interface {
	Wake()
	Close()
	Find(params Params, result interface{}) (count int, err error)
	FindOne(id string, result interface{}) (err error)
	UpsertID(selector interface{}, update interface{}) (info *mgo.ChangeInfo, err error)
	FindAndModify(selector interface{}, update interface{}, target interface{}) (info *mgo.ChangeInfo, err error)
	Count() (int, error)
}

Collection - an interface representing a trimmed down collection object

func Connect

func Connect(dialer CollectionDialer, URI string, collectionName string) (collection Collection)

Connect to the specified database and return a Collection object for the specified collection.

func NewCollectionDialer

func NewCollectionDialer(url string, dbname string, collectionname string) (collection Collection, err error)

NewCollectionDialer -- dials a new mongo connection

type CollectionDialer

type CollectionDialer func(url string, dbname string, collectionname string) (collection Collection, err error)

CollectionDialer - a funciton type to dial for collections

type CollectionRepo

type CollectionRepo struct {
	Col *mgo.Collection
	// contains filtered or unexported fields
}

CollectionRepo - mgo collection adaptor

func (*CollectionRepo) Close

func (s *CollectionRepo) Close()

Close -- closes the connection

func (*CollectionRepo) Count

func (s *CollectionRepo) Count() (int, error)

Count -- counts the collection records

func (*CollectionRepo) Find

func (s *CollectionRepo) Find(params Params, result interface{}) (count int, err error)

Find -- finds all records matching given selector

func (*CollectionRepo) FindAndModify

func (s *CollectionRepo) FindAndModify(selector interface{}, update interface{}, result interface{}) (info *mgo.ChangeInfo, err error)

FindAndModify -- execute a normal upsert

func (*CollectionRepo) FindOne

func (s *CollectionRepo) FindOne(id string, result interface{}) (err error)

FindOne -- finds record with given ID

func (*CollectionRepo) UpsertID

func (s *CollectionRepo) UpsertID(id interface{}, update interface{}) (info *mgo.ChangeInfo, err error)

UpsertID -- upserts the given object to the given id

func (*CollectionRepo) Wake

func (s *CollectionRepo) Wake()

Wake - will ping and reconnect if need be

type Params

type Params interface {
	Selector() bson.M
	Scope() bson.M
	Limit() int
	Offset() int
}

Params interface exposes mongodb-specific query parameters: Selector, Scope, Limit, and Offset

Directories

Path Synopsis
Params will extract query parameters from the query string of a request into a RequestParams object.
Params will extract query parameters from the query string of a request into a RequestParams object.
Wrap is a simple helper to wrap API response data and errors in a consistent structure.
Wrap is a simple helper to wrap API response data and errors in a consistent structure.

Jump to

Keyboard shortcuts

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