mgomodel

package module
v0.0.0-...-4226ca4 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2012 License: MIT Imports: 7 Imported by: 0

README

mgomodel is a GO package that lets you interact with mongodb in a familiar model based way. It builds on top of the awsome mgo mongo driver http://labix.org/mgo.

mgomodel exposes the familiar save, delete, update, find, load methods one would expect with a normal ORM type setup. There is also a configurable mechanism for building in custom data validation.

Check out mgomodel_test.go for some examples. Docs are here: http://go.pkgdoc.org/github.com/mdennebaum/mgomodel

Documentation

Overview

mgomodel is a GO package that lets you interact with mongodb in a familiar model based way. It builds on top of the awsome mgo mongo driver http://labix.org/mgo.

mgomodel exposes the familiar save, delete, update, find, load methods one would expect with a normal ORM type setup. There is also a configurable mechanism for building in custom data validation.

Comming soon:

a utility for managing associated mongo indexes.

Example:

import (
	"labix.org/v2/mgo/bson"
	"github.com/mdennebaum/mgomodel"
	"errors"
)

type AddressInfo struct{
	Address1 string
	Address2 string
	City string
	State string
	Zip string
}

type Person struct{
	Id bson.ObjectId "_id,omitempty"
	Name string
	Address *AddressInfo
	User string
}

//satisfy the modler interface
func (this *Person) Collection() string {
	return "people"
}

//satisfy the modler interface
func (this *Person) ID() bson.ObjectId {
	return this.Id
}

//satisfy the validatedmodler interface
func (this *Person) RequiredFields() []string {
	return []string{"Name", "User"}
}

func NewPerson(name,user) *Person{
	return &Person {
		Name: name,
		User: user,
		Address: &AddressInfo{}
	}
}

func main(){
	//init the mongo connection
	mgomodel.Mongo().Connect("localhost")

	//set the default database
	mgomodel.Mongo().SetDB("test")

	//create an empty user instance to hold our user document
	user := User{}

	//find the user with the username
	mgomodel.Mongo().Collection("users").find(bson.M{"Username":"mattdennebaum"}).One(&user)

	//create a new person object
	person := NewPerson("Matt Dennebaum",user.ID().String())

	//validate the person
	if mgomodel.Valid(person) {
		//save the person
		err := mgomodel.Save(person)
	}
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(inst Modeler) error

Delete will remove the current document from the collection

func Json

func Json(inst Modeler) (string, error)

Json outputs a model instance as a json packet

func Load

func Load(inst Modeler) error

Load a document. You must first create an empty Model and set the Id

func Mongo

func Mongo() *mongoDB

Mongo is an accessor for the mongo singleton

func Save

func Save(inst Modeler) error

Save will save this instance. If there is an Id field thats not set we assume this is a new model and we create a new objectid and insert the new document. If there is an Id then we update the document at that Id. If its an insert we will first set all the default values.

func Valid

func Valid(inst ValidatedModeler) error

Valid validates a model based on the RequiredFields and Validators methods

Types

type DefaultedModeler

type DefaultedModeler interface {
	DefaultValues() map[string]interface{}
}

DefaultedModeler defines an interface for models that support default value funtionality

type IndexedModeler

type IndexedModeler interface {
	Indexes() []*mgo.Index
}

IndexedModeler defines an interface for models that support indexing funtionality. indexes are not added automatically. You must use the index manager utility.

type Modeler

type Modeler interface {
	ID() bson.ObjectId
	Collection() string
}

Modeler is the basic interface type for model instances

type ValidatedModeler

type ValidatedModeler interface {
	Validators() []func(interface{}) error
	RequiredFields() []string
}

ValidatedModeler defines an interface for models that support validation funtionality

Jump to

Keyboard shortcuts

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