mgocrud

command module
v0.0.0-...-3758aa5 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2018 License: MIT Imports: 1 Imported by: 0

README

MgoCRUD

generate mongo CRUD methods from a golang struct

Demo

What is It ?

mgocrud is a cli tool which can generate basic CRUD functions from a golang struct

Example

Following are contents of main.go

package main

type User struct {
	ID   bson.ObjectId `bson:"_id,omitempty"`
	Name string        `bson:"name"`
}

Now on running mgocrud generate main.go, the file will be appended with CRUD functions for the User struct.

package main

type User struct {
	ID   bson.ObjectId `bson:"_id,omitempty"`
	Name string        `bson:"name"`
}

func (u *User) Create(mgosession *mgo.Session, database, collection string) error {
	err := mgosession.DB(database).C(collection).Insert(u)
	if err != nil {
		return err
	}
	return err
}
func (u *User) Read(mgosession *mgo.Session, database, collection string, selector bson.M) (*[]User, error) {
	var results []User
	err := mgosession.DB(database).C(collection).Find(selector).All(&results)
	if err != nil {
		return nil, err
	}
	return &results, nil
}
func (u *User) Update(mgosession *mgo.Session, database, collection string, selector, change bson.M) error {
	err := mgosession.DB(database).C(collection).Update(selector, bson.M{"$set": change})
	if err != nil {
		return err
	}
	return nil
}
func (u *User) Delete(mgosession *mgo.Session, database, collection string, selector bson.M) (*mgo.ChangeInfo, error) {
	info, err := mgosession.DB(database).C(collection).RemoveAll(selector)
	if err != nil {
		return nil, err
	}
	return info, nil
}

mgocrud expects github.com/globalsign/mgo & github.com/globalsign/bson to be used as the driver for connecting to mongo.

Okay, How can I try It ?

go get github.com/scriptonist/mgocrud

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

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