revmgo

package module
v0.0.0-...-5d1ef24 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2014 License: MIT Imports: 7 Imported by: 0

README

revmgo

mgo module for revel framework

Module Use

app.conf

Settings can be configured via the following directives in app.conf.

revmgo.dial

Please review the documentation at mgo.Session.Dial() for information on the syntax and valid settings.

revmgo.method

This can be one of 'clone', 'copy', 'new'. See mgo.Session.New() for more information.

app.init()

Add the following inside the app.init() function in app/init.go.

revel.OnAppStart(revmgo.AppInit)
controllers.init()

Similarly for your controllers init() function you must add the revmgo.ControllerInit() method. A minimum app/controllers/init.go file is represented below.

package controllers

import "github.com/jgraham909/revmgo"

func init() {
    revmgo.ControllerInit()
}
Embedding the controller

In any controller you want to have mongo connectivity you must include the MongoController.

Add the following import line in source files that will embed MongoController.

 "github.com/jgraham909/revmgo"

Embed the MongoController on your custom controller;

type Application struct {
	*revel.Controller
  revmgo.MongoController
	// Other fields
}

Your controller will now have a MongoSession variable of type *mgo.Session. Use this to query your mongo datastore.

See Also

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Session *mgo.Session // Global mgo Session
	Dial    string       // http://godoc.org/labix.org/v2/mgo#Dial
	Method  string       // clone, copy, new http://godoc.org/labix.org/v2/mgo#Session.New
)
View Source
var ObjectIdBinder = revel.Binder{

	Bind: revel.ValueBinder(func(val string, typ reflect.Type) reflect.Value {
		if len(val) == 0 {
			return reflect.Zero(typ)
		}
		if bson.IsObjectIdHex(val) {
			objId := bson.ObjectIdHex(val)
			return reflect.ValueOf(objId)
		} else {
			glog.Error("ObjectIdBinder.Bind - invalid ObjectId!")
			return reflect.Zero(typ)
		}
	}),

	Unbind: func(output map[string]string, name string, val interface{}) {
		var hexStr string
		hexStr = fmt.Sprintf("%s", val.(bson.ObjectId).Hex())

		if bson.IsObjectIdHex(hexStr) {
			output[name] = hexStr
		} else {
			glog.Error("ObjectIdBinder.Unbind - invalid ObjectId!")
			output[name] = ""
		}
	},
}

Custom TypeBinder for bson.ObjectId Makes additional Id parameters in actions obsolete

Functions

func AppInit

func AppInit()

func ControllerInit

func ControllerInit()

func MethodError

func MethodError(m string) error

Types

type MongoController

type MongoController struct {
	*revel.Controller
	MongoSession *mgo.Session // named MongoSession to avoid collision with revel.Session
}

func (*MongoController) Begin

func (c *MongoController) Begin() revel.Result

Connect to mgo if we haven't already and return a copy/new/clone of the session

func (*MongoController) End

func (c *MongoController) End() revel.Result

Close the controller session if we have an active one.

Jump to

Keyboard shortcuts

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