mongofixtures

package module
v0.0.0-...-daf5d79 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2013 License: MIT Imports: 5 Imported by: 0

README

mongofixtures Build Status

A Go quick and dirty utility for cleaning collections and loading fixtures into them.

How to install ?

go get github.com/OwlyCode/mongofixtures

How to use ?

session, err := Begin("localhost", "database")
defer session.End()

if err != nil {
  panic(err)
}

// Initial cleaning.
session.Clean("collection")

// Now population time !
session.Push("collection", document{Id: bson.NewObjectId(), Title: "This is a demo"})

// Load some yaml.
session.ImportYamlFile("test.yml")

Note that Push and Clean can return both an error that you might want to check.

You can provide to the Begin function host parameter any string representing a mongo connection. For example : "mongodb://myuser:mypass@localhost" should work fine.

Notes on YAML

/!\ The short notation for arrays [] is not supported at the moment. /!\

You can generate ids by using __something__. Every time mongofixtures sees a string matching __(something)__ it generates an ObjectId and stores it. If you use __something__ elsewhere mongofixtures will set the same ObjectId. Take a look a test.yml to view an example.

What's on the todo ?

  • More tests !
  • Godoc the yml part
  • Improve yml support

Documentation

Overview

The package mongofixtures enables to load quickly and easily data into mongo db.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Session

type Session struct {
	MongoSession *mgo.Session
	DatabaseName string
	ObjectIds    map[string]bson.ObjectId
}

A session holds the mongo session (based on labix.org/v2/mgo).

func Begin

func Begin(host string, databaseName string) (Session, error)

Builds a new Session with the given host and database name.

func (*Session) Clean

func (l *Session) Clean(collectionName string) error

Removes the collection identified by collectionName.

func (*Session) End

func (l *Session) End()

Ends a session. Should be called with defer right after Begin :

session := Begin("localhost", "mydatabase")
defer session.End()

func (*Session) ImportYamlFile

func (l *Session) ImportYamlFile(path string)

func (*Session) ImportYamlString

func (l *Session) ImportYamlString(yml string)

func (*Session) Push

func (l *Session) Push(collectionName string, documents ...interface{}) error

Adds a bson-marshalled version of each documents passed as an argument. Usage example :

session.Push("collection", Document{Title:"hello world"}, Document{Title:"Hi there!"})

Jump to

Keyboard shortcuts

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