Mojo inspects your structs and generates helper functions that allow you to perform all your CRUD without writing all the boilerplate code.
Requirements
- A working installation of MongoDB
- mgo
Installation
Use go install: go install github.com/evilnode/mojo
Overview
Mojo will inspect your structs by looking at both your comments and tags. Your struct MUST have a property Id of type bson.ObjectId (labix.org/v2/mgo/bson), which functions as the primary identifier for the document.
An example:
package user
import (
"gopkg.in/mgo.v2/bson"
)
// mojo #users
type User struct {
Id bson.ObjectId `bson:"_id"`
CustomerId int `bson:"customerid", mojo-index:"customerid"`
Email string `bson:"email", mojo-unique:"email"`
FirstName string `bson:"fname"`
LastName string `bson:"lname"`
}In the above code, the comment // mojo #users tells the mojo parser that it should examine the following struct and assign it to the collection users within the mongodb database. Anything tagged as mojo-index will be initialized as an index when you bootstrap your application (see below). Anything tagged as mojo-unique will be indexed as a unique key upon bootstrap. Additionally, fetch methods for each unique key will be generated. The following functions are generated from our sample struct:
func FetchUserWithId(id bson.ObjectId) (*User, error) {
...
}
func FetchUserWithEmail(email string) (*User, error) {
...
}
func (this *User) Save() (bool, error) {
...
}
func (this *User) Delete() (bool, error) {
...
}Usage
mojo <options>
Options
Roadmap
This project will be converted to go generate