The MongoDB SDM Generator for Go
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
connection
generator
mojo-sample
parser
template
.travis.yml
LICENSE
README.md
mojo.go

README.md

Build Status

Mojo inspects your structs and generates helper functions that allow you to perform all your CRUD without writing all the boilerplate code.


Requirements


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
**-d [path]**
The directory to search for and parse mojo-enabled structs. By default, the parser will search all subdirectories under this directory as well. Default is **current working directory**
**-n**
Disable the searching of subdirectories for the path specified by **-d**. Default is **false**
**-D**
Generate debug logging (log.Println) for mojo-enabed structs. Default is **false**
**-p** [package path]
The path to the package _in your project_ that implements the github.com/evilnode/mojo/connection interface. Default is **current working directory**
**-S** [structname]
The name of the struct that implements the github.com/evilnode/mojo/connection interface. Obviously this struct is defined in the package at **-p**. Default is **MojoConn**
**-s** [suffix]
The desired suffix for mojo-enabled files. Default is **-mojo**. A file for a struct named _User_ would be named _user-mojo.go_
**-t** [path]
The _full path_ to the template you want to use to generate your mojo files. By default, mojo uses the _default.mojotemplate_ file that is included with the mojo project. If you define this flag, _you must provide the full file path_

Roadmap

This project will be converted to go generate