revmgo

package module
v0.0.0-...-96a9e35 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2015 License: MIT Imports: 5 Imported by: 0

README

revmgo Build Status

mgo module for revel framework

This is a mantained fork of https://github.com/jgraham909/revmgo.

Installation
    go get github.com/janekolszak/revmgo
Test
    revel test github.com/janekolszak/revmgo/testapp dev
Configuration

In app.conf:

Initialization
  • In app.init() in app/init.go add:
    revel.OnAppStart(revmgo.AppInit)
  • In controllers.init() in app/controllers/init.go add
    revmgo.ControllerInit()

So a minimal controller's init() would be:

    package controllers

    import "github.com/janekolszak/revmgo"

    func init() {
        revmgo.ControllerInit()
    }
Usage

Embed the MongoController on your custom controller;

    package controllers

    import (
        "github.com/janekolszak/revmgo"
        "github.com/revel/revel"
    )

    type App struct {
        *revel.Controller
        revmgo.MongoController
  		// ...
  	}

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

Use revmgo in revel.jobs

    package controllers

    import (
        "github.com/janekolszak/revmgo"
        "github.com/revel/revel"
    )

    type Job struct {}
    func(j Job){
        s, err := revmgo.GetSession()
        if err != nil {
            // error
        }
        defer s.Close()
        // ...
    }
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 {
			revel.ERROR.Print("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 {
			revel.ERROR.Print("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 GetSession

func GetSession() (*mgo.Session, error)

No InterceptMethod for Jobs so get the Session and defer Session.Close()

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.

Directories

Path Synopsis
testapp
app

Jump to

Keyboard shortcuts

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