mongobuf

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

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

Go to latest
Published: Jan 21, 2019 License: MIT Imports: 14 Imported by: 0

README

MongoBuffalo

Package for simple usage MongoDB database with buffalo framework

Current approach

Not support concurrent access => used Mutex for protect this.

Goal of this adapter

Help to develop simple low/mid loaded systems!

Usage

$ go get -u github.com/d7561985/mongo-buffalo
Init client:
    "github.com/d7561985/mongo-buffalo"
    "github.com/gobuffalo/pop"
if err := config.LoadConfigFile(); err != nil{
    return err
}
env := envy.Get("GO_ENV", "development")
client, err := mongobuf.NewMongo(config.ConnectionDetails[env])
if err != nil {
    return err
}
Validation

If u need validation(it's just optional, without this everything will work) for create and update instance of T (structure) Yout need to implement following interface with requirement of validate pkg of gobuffalo:

"github.com/gobuffalo/validate"
ValidateAble interface {
    Validate() *validate.Errors
}

This open a lot of flexible ways of usage. For example where is packege with prebuilded validators:

import(
    "github.com/gobuffalo/validate/validators"	
)

type A struct {
	ID         primitive.ObjectID `bson:"_id"`
	A          string
	AnnnaLirra int
}

func (a *A) Validate() *validate.Errors {
	return validate.Validate(
		&validators.StringIsPresent{Field: a.A, Name: "A"},
	)
}
Requirements to structure type
Update

For using update we should know ID of current object in MongoDB. One simple way follow it in out structure while create and update.

Because of it, u should have not closed var ( First letter is High ) of type primitive.ObjectID Example:

type A struct {
	AnyName  primitive.ObjectID `bson:"_id", json:"-"`
	...
}
All

For getting list of all struct instances in db where is requirement of slice interface:

	// All represent slice
	All interface {
		// return empty instance of slice type
		// PTR only!!!!!
		T() interface{}

		// add to back
		Add(interface{}) error
	}

receiver of Add method should be link (ptr). Example:

type As []A

func (As) T() interface{} {
    return &A{}
}

func (a *As) Add(in interface{}) error {
    link, ok := in.(*A)
    if !ok {
        return errors.New("bad cast")
    }
    *a = append(*a, *link)
    return nil
}

or as pointer in slice:

type As []*A

func (As) T() interface{} {
	return &A{}
}

func (a *As) Add(in interface{}) error {
	link, ok := in.(*A)
	if !ok {
		return errors.New("bad cast")
	}
	*a = append(*a, link)
	return nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type All

type All interface {
	// return empty instance of slice type
	// PTR only!!!!!
	T() interface{}

	// add to back
	Add(interface{}) error
}

All represent slice

type Model

type Model struct {
	pop.Model
}

func M

func M(in interface{}) *Model

func (*Model) GetObjectID

func (m *Model) GetObjectID() bson.M

GetObjectID find bson ObjectID in structure and return bson.M for searching model linked to this ID

func (*Model) UpdateObjectID

func (m *Model) UpdateObjectID()

UpdateObjectID generate new BSON ObjectID for first found in struct

func (*Model) Validate

func (m *Model) Validate() *validate.Errors

type Mongodb

type Mongodb struct {
	ConnectionDetails *pop.ConnectionDetails
	// contains filtered or unexported fields
}

Mongodb is helper for access to MongoDB. Current approach used here not support concurrent access => used Mutex for protect this. Goal of this adapter help to develop simple low/mid loaded systems.

func NewMongo

func NewMongo(deets *pop.ConnectionDetails) (*Mongodb, error)

NewMongo

func (*Mongodb) All

func (m *Mongodb) All(in All, filter bson.M) error

All pollute @in parameter of All interface with data witch was founded. @filter - simple map[string]interface{}, where string is in model var. name (key) and interface value of this key

func (*Mongodb) Create

func (m *Mongodb) Create(model interface{}) (*validate.Errors, error)

Save model inside db.

func (*Mongodb) Details

func (m *Mongodb) Details() *pop.ConnectionDetails

func (*Mongodb) Get

func (m *Mongodb) Get(in interface{}, filter bson.M) error

Get make search one document corresponding to filter rules. @filter - simple map[string]interface{}, where string is in model var. name (key) and interface value of this key

func (*Mongodb) GetCollection

func (m *Mongodb) GetCollection(mdl *Model) *mongo.Collection

GetCollection create collection object suitable for current instance of model.

func (*Mongodb) Name

func (m *Mongodb) Name() string

func (*Mongodb) Ping

func (m *Mongodb) Ping() error

Ping simple

func (*Mongodb) URL

func (m *Mongodb) URL() string

func (*Mongodb) Update

func (m *Mongodb) Update(model interface{}) (*validate.Errors, error)

Update update model with presented

type ValidateAble

type ValidateAble interface {
	Validate() *validate.Errors
}

Directories

Path Synopsis
Package config based on method loading github.com/gobuffalo/pop of configs.
Package config based on method loading github.com/gobuffalo/pop of configs.

Jump to

Keyboard shortcuts

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