gomongo

package module
v0.0.0-...-18c4c95 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2018 License: MIT Imports: 5 Imported by: 0

README

MONGODB WRAPPER FOR GOLANG | mongodb, golang

Basic functionalities for data management like insert, remove, find, findbyid, update, upsert, etc

All functions are available in sync as well as async flavours like insert, insertAsync

Just Append Async after the function name like insert, insertAsync

# clone the repo
git clone https://gitlab.com/amulyakashyap09/gomongo.git
import "github.com/amulyakashyap09/gomongo"
Instantiate the connection
    config := Config{"mongodb", MongoDBHosts, AuthDatabase, AuthUserName, AuthPassword}
    db, err := Init(MONGODB)
    
Connect to database
    sess, err := db.Connect(&config)
Insert

# Insert sample
#   user := new(User)
#   user.FirstName = "AmulyaXIVXIV"
#   user.LastName = "KashyapXIVXIV"
#   user.Age = 26
#   user.Phone = "9559974779"
#   user.Salary = 654654564
#   user.DateTime = time.Now()

#   insertStr := new(InsertStruct)
#   insertStr.Data = user

#   sess.Collection = "users"

#   err = sess.Insert(insertStr)

#   if err!=nil {
#       fmt.Println("error in inserting : ", err)    
#   }else{
#       fmt.Println("record inserted successfully : ")    
#   }
Update

# Update sample
#   updateStruct := new(UpdateStruct)
#   conn.Collection = "users"
#   updateStruct.Id = "5b28da94a34bd180f5ab0f5a"
#   updateStruct.Data = bson.M{"$set": bson.M{"firstname": "AmulyaXXX", "lastname": "Kashyap", "age": 26, "phone": "9559974779", "salary": "7854693210", "datetime": time.Now()}}
#   err = conn.Update(updateStruct)

#   if err!=nil {
#       fmt.Println("error in update : ", err)    
#   }else{
#       fmt.Println("record updated successfully : ")    
#   }
Find

# Find sample
#   findStr := new(FindStruct)
#   findStr.Fields = bson.M{"firstname": 1}
#   findStr.Options = make(map[string]int)
#   findStr.Options["skip"] = 0
#   findStr.Options["limit"] = 10
#   findStr.Query = bson.M{"_id": bson.ObjectIdHex("5b28da94a34bd180f5ab0f5a")}
#   findData, err := sess.Find(findStr)

#   if err!=nil {
#       fmt.Println("error in find : ", err)    
#   }else{
#       fmt.Println("record found successfully : ", findData)    
#   }
Remove

# Remove sample
#   removeStr := new(RemoveStruct)
#   removeStr.Query = bson.M{"_id": bson.ObjectIdHex("5b28da94a34bd180f5ab0f5a")}
#   err = sess.Remove(removeStr)

#   if err!=nil {
#       fmt.Println("error in find : ", err)    
#   }else{
#       fmt.Println("record found successfully : ", findData)    
#   }

Project Details

Author
    Amulya Kasyap

Maintainer

    Amulya Ratan
        email : amulyakashyap09@gmail.com
        contact : +91-9559974779
Version
    1.0.0
License
    This project is licensed under the MIT License

Documentation

Index

Constants

View Source
const (
	MYSQL   = "mysql"
	SQLITE  = "sqlite"
	MONGODB = "mongodb"
)

Our Database types types

Variables

View Source
var (
	MongoErrorNotFound = errors.New("not found") //especiall for mongo not found error | that's why "n" is in small letters | dont change it
	ErrorNotFound      = errors.New("Data not found")
	ErrorInvalidDBType = errors.New("Invalid database type")
	ErrorInvalidDriver = errors.New("Unsupported storage driver!")
)

Functions

func Close

func Close(conn *Connection) error

Types

type BulkInsertStruct

type BulkInsertStruct struct {
	Config *Config
	Data   []interface{}
}

type Callback

type Callback struct {
	Data  interface{}
	Error error
}

type Config

type Config struct {
	Uri            string
	DbType         string
	Hosts          string //connection url i.e, localhost:27017
	Database       string //database name
	ReplicaSetName string //database name
	Username       string //username
	Password       string //password
	AuthDatabase   string //auth db
	Direct         bool
}

type Connection

type Connection struct {
	Database    string                     //database name
	DialInfo    *mgo.DialInfo              // connection info
	Session     *mgo.Session               //session info
	Collections map[string]*mgo.Collection //all collections
	Collection  string                     //collection name
}

func ConnectMongo

func ConnectMongo(config *Config) (*Connection, error)

ConnectMongo ...

func (*Connection) BulkInsert

func (conn *Connection) BulkInsert(bulkInsertStruct *BulkInsertStruct) (*mgo.BulkResult, error)

BulkInsert : Function inserts the data in bulk to the collection Input Parameters :

	*BulkInsertStruct (Struct) :
 		Data([] interfaces{}]) : the object which has to be inserted

Output Parameters

record(*mgo.BulkResult) : return count of matched and modified results
error : if it was error then return error else nil

func (*Connection) Find

func (conn *Connection) Find(findStruct *FindStruct) ([]interface{}, error)

Find : Function finds the record into the collection according to the query/criteria Input Parameters

*FindStruct (Struct) :
	Query(bson Object) : Criteria as per the update should execute
	Options(map[string]int) : optional things like, limit, skip, etc

Output Parameters

records([]interface{]}) : Return the result mapped as interface
error(error) : if it was error then return error else nil

func (*Connection) FindAll

func (conn *Connection) FindAll(findAllStruct *FindAllStruct) ([]interface{}, error)

FindAll : Function finds all the records into the collection Input Parameters

*FindAllStruct (Struct) :

Output Parameters

records([]interface{}) : all the records present in database
error : if it was error then return error else nil

func (*Connection) FindAllAsync

func (conn *Connection) FindAllAsync(findAllStruct *FindAllStruct, callback chan *Callback)

FindAllAsync : Function finds all the records into the collection Input Parameters

*FindAllStruct (Struct) :
	callback (channel) : which returns data to goroutine

Output Parameters

callback(data, error) : returns data, error to channel

func (*Connection) FindAsync

func (conn *Connection) FindAsync(findStruct *FindStruct, callback chan *Callback)

FindAsync : Function finds the record into the collection according to the query/criteria Input Parameters

*FindStruct (Struct) :
	Query(bson Object) : Criteria as per the update should execute
	Options(map[string]int) : optional things like, limit, skip, etc
	callback (channel) : which returns data to goroutine

Output Parameters

callback(data, error) : returns data, error to channel

func (*Connection) FindByID

func (conn *Connection) FindByID(findByIDStruct *FindByIDStruct) (interface{}, error)

FindByID : Function FindByID finds and returns record by Hexadecimal ID Input Parameters :

*FindByIDStruct (Struct) :
	Id(string) : The record id whose details have to be updated

Output :

record(interface{}) : Returns the mongo Object
error : Return error object if found

func (*Connection) FindByIDAsync

func (conn *Connection) FindByIDAsync(findByIDStruct *FindByIDStruct, callback chan *Callback)

FindByIDAsync : Function FindByIDAsync finds and returns record by Hexadecimal ID Input Parameters :

*FindByIDStruct (Struct) :
	Id(string) : The record id whose details have to be updated
	callback (channel) : which returns data to goroutine

Output :

callback(data, error) : sends data, error to channel

func (*Connection) Insert

func (conn *Connection) Insert(insertStruct *InsertStruct) error

Insert : Function inserts the data object into the collection Input Parameters :

	*InsertStruct (Struct) :
 		Data([] interfaces{}]) : the object which has to be inserted

Output Parameters

error : if it was error then return error else nil

func (*Connection) InsertAsync

func (conn *Connection) InsertAsync(insertStruct *InsertStruct, callback chan *Callback)

InsertAsync : Function inserts the data object into the collection Input Parameters :

	*InsertStruct (Struct) :
 		Data([] interfaces{}]) : the object which has to be inserted
		callback (channel) : which returns data to goroutine

Output Parameters

callback(data, error) : sends data, error back to channel

func (*Connection) Remove

func (conn *Connection) Remove(removeStruct *RemoveStruct) error

Remove : Function removes the record from the collection as per criteria/query Input Parameters

*RemoveStruct (Struct) :
	Query(bson Object) : Criteria as per the update should execute

Output Parameters

records(boolean) : returns true / false depending on output of operation
error : if it was error then return error else nil

func (*Connection) RemoveAll

func (conn *Connection) RemoveAll(removeAllStruct *RemoveAllStruct) (*mgo.ChangeInfo, error)

RemoveAll : Function removes all the record from the collection Input Parameters

*RemoveAllStruct (Struct) :

Output Parameters

records(*mgo.ChangeInfo) : returns matched modified removed count
error : if it was error then return error else nil

func (*Connection) RemoveAllAsync

func (conn *Connection) RemoveAllAsync(removeAllStruct *RemoveAllStruct, callback chan *Callback)

RemoveAllAsync : Function removes all the record from the collection asynchronously Input Parameters

*RemoveAllStruct (Struct) :
callback (channel) : which returns data to goroutine

Output Parameters

callback(data, error) : returns data, error to channel

func (*Connection) RemoveAsync

func (conn *Connection) RemoveAsync(removeStruct *RemoveStruct, callback chan *Callback)

removeAsync : Function removes the record from the collection as per criteria/query Input Parameters

*RemoveStruct (Struct) :
	Query(bson Object) : Criteria as per the update should execute
	callback (channel) : which returns data to goroutine

Output Parameters

callback(data, error) : returns data, error to channel

func (*Connection) Update

func (conn *Connection) Update(updateStruct *UpdateStruct) error

func (*Connection) UpdateAll

func (conn *Connection) UpdateAll(updateAllStruct *UpdateAllStruct) (*mgo.ChangeInfo, error)

func (*Connection) UpdateAllAsync

func (conn *Connection) UpdateAllAsync(updateAllStruct *UpdateAllStruct, callback chan *Callback)

func (*Connection) UpdateAsync

func (conn *Connection) UpdateAsync(updateStruct *UpdateStruct, callback chan *Callback)

UpdateAsync : Function Updates the record into the collection Input Parameters

	*UpdateStruct (Struct) :
 		Data([] interfaces{}]) : the object which has to be inserted
		Id(string) : The record id whose details have to be updated
		callback (channel) : which returns data to goroutine

Output Parameters

callback(data, error) : sends if error back to channel

func (*Connection) Upsert

func (conn *Connection) Upsert(upsertStruct *UpsertStruct) (*mgo.ChangeInfo, error)

func (*Connection) UpsertAll

func (conn *Connection) UpsertAll(upsertAllStruct *UpsertAllStruct) (*mgo.ChangeInfo, error)

func (*Connection) UpsertAllAsync

func (conn *Connection) UpsertAllAsync(upsertAllStruct *UpsertAllStruct, callback chan *Callback)

func (*Connection) UpsertAsync

func (conn *Connection) UpsertAsync(upsertStruct *UpsertStruct, callback chan *Callback)

type DB

type DB interface {
	Connect(*Config) (*Connection, error)
}

func Init

func Init(driver string) (DB, error)

Database factory Return a DB for general purpose

type FindAllStruct

type FindAllStruct struct {
	Fields bson.M
}

type FindByIDStruct

type FindByIDStruct struct {
	Id     string
	Fields bson.M
}

type FindStruct

type FindStruct struct {
	Query   bson.M
	Options map[string]int
	Fields  bson.M
}

type InsertStruct

type InsertStruct struct {
	Data interface{}
}

type MongoDB

type MongoDB struct{}

func (MongoDB) Connect

func (n MongoDB) Connect(config *Config) (*Connection, error)

Connect ...

type Operations

type Operations interface {
	BulkInsertStruct(*BulkInsertStruct)
	Insert(*InsertStruct) error
	InsertAsync(*InsertStruct, *Callback)
	Update(*UpdateStruct) error
	UpdateAsync(*UpdateStruct, *Callback)
	Upsert(*UpsertStruct) (*mgo.ChangeInfo, error)
	UpsertAsync(*UpsertStruct, *Callback)
	UpdateAll(*UpdateAllStruct) (*mgo.ChangeInfo, error)
	UpdateAllAsync(*UpdateAllStruct, *Callback)
	UpsertAll(*UpsertAllStruct) (*mgo.ChangeInfo, error)
	UpsertAllAsync(*UpsertAllStruct, *Callback)
	FindByID(*FindByIDStruct) (interface{}, error)
	FindByIDAsync(*FindByIDStruct, *Callback)
	Find(*FindStruct) ([]interface{}, error)
	FindAsync(*FindStruct, *Callback)
	FindAll(*FindAllStruct) ([]interface{}, error)
	FindAllAsync(*FindAllStruct, *Callback)
	Remove(*RemoveStruct) error
	RemoveAsync(*RemoveStruct, *Callback)
	RemoveAll(*RemoveAllStruct) (*mgo.ChangeInfo, error)
	RemoveAllAsync(*RemoveAllStruct, *Callback)
}

type RemoveAllStruct

type RemoveAllStruct struct {
}

type RemoveStruct

type RemoveStruct struct {
	Query bson.M
}

type UpdateAllStruct

type UpdateAllStruct struct {
	Query bson.M
	Data  interface{}
}

type UpdateStruct

type UpdateStruct struct {
	Id   string
	Data interface{}
}

type UpsertAllStruct

type UpsertAllStruct struct {
	Query bson.M
	Data  interface{}
}

type UpsertStruct

type UpsertStruct struct {
	Id   string
	Data interface{}
}

Jump to

Keyboard shortcuts

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