mogo

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2021 License: MIT Imports: 13 Imported by: 0

README

MoGo

Mongo DB wrapper for golang

Quick Start

Install
go get github.com/risusanto/mogo
Usage

To get started, import mogo package, and setup for default config:


import "github.com/risusanto/mogo"

func init() {
   // Setup mgm default config
   var mongoConfig  corey.DBConfig
   mongoConfig.MongoURI = "mongodb://root:12345@localhost:27017"
   mongoConfig.DBName = "mogo_db"
   _,_,err := corey.NewConnection(mongoConfig)
   if err != nil{
     panic(err)
   }
}

Define model:

type Podcast struct {
    corey.BaseModel		  	  `bson:",inline"`
    Title  string             `json:"title" bson:"title,omitempty"`
    Author string             `json:"author" bson:"author,omitempty"`
}

Insert data:


podcast := &Podcast{}

podcast.Tittle = "Learn GoLang"
podcast.Author = "Ari"

podcast.Create(podcast)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MongoDB *mongo.Database
View Source
var MongoDBClient *mongo.Client

Functions

func CollName

func CollName(m Model) string

CollName check if you provided collection name in your model, return it's name, otherwise guess model collection's name.

func Ctx

func Ctx() context.Context

Ctx function create new context with default timeout and return it.

func NewClient

func NewClient(opts ...*options.ClientOptions) (*mongo.Client, error)

NewClient return new mongodb client.

func NewConnection

func NewConnection(config DBConfig) (*mongo.Client, *mongo.Database, error)

func NewCtx

func NewCtx(timeout time.Duration) context.Context

NewCtx function create and return new context with your specified timeout.

func ResetDefaultConfig

func ResetDefaultConfig()

ResetDefaultConfig reset all of the default config

func SetDefaultConfig

func SetDefaultConfig(conf *Config, dbName string, opts ...*options.ClientOptions) (err error)

SetDefaultConfig initial default client and Database .

func UpsertTrueOption

func UpsertTrueOption() *options.UpdateOptions

UpsertTrueOption returns new instance of the UpdateOptions with upsert=true property.

Types

type BaseModel

type BaseModel struct {
	IDField    `bson:",inline"`
	DateFields `bson:",inline"`
}

func (*BaseModel) Create

func (m *BaseModel) Create(model Model, opts ...*options.InsertOneOptions) error

func (*BaseModel) Delete

func (m *BaseModel) Delete(model Model) error

func (*BaseModel) Find

func (m *BaseModel) Find(model Model, filter interface{}, results interface{}, opts ...*options.FindOptions)

func (*BaseModel) FindByID

func (m *BaseModel) FindByID(id string, model Model) error

func (*BaseModel) Update

func (m *BaseModel) Update(model Model, opts ...*options.UpdateOptions) error

type Collection

type Collection struct {
	*mongo.Collection
}

Collection performs operations on models and given Mongodb collection

func Coll

func Coll(m Model, opts ...*options.CollectionOptions) *Collection

Coll return model's collection.

func CollectionByName

func CollectionByName(name string, opts ...*options.CollectionOptions) *Collection

CollectionByName return new collection from default config

func NewCollection

func NewCollection(db *mongo.Database, name string, opts ...*options.CollectionOptions) *Collection

NewCollection return new collection with passed database

func (*Collection) Create

func (coll *Collection) Create(model Model, opts ...*options.InsertOneOptions) error

Create method insert new model into database.

func (*Collection) CreateWithCtx

func (coll *Collection) CreateWithCtx(ctx context.Context, model Model, opts ...*options.InsertOneOptions) error

CreateWithCtx method insert new model into database.

func (*Collection) Delete

func (coll *Collection) Delete(model Model) error

Delete method delete model (doc) from collection. If you want to doing something on deleting some model use hooks, don't need to override this method.

func (*Collection) DeleteWithCtx

func (coll *Collection) DeleteWithCtx(ctx context.Context, model Model) error

DeleteWithCtx method delete model (doc) from collection. If you want to doing something on deleting some model use hooks, don't need to override this method.

func (*Collection) FindByID

func (coll *Collection) FindByID(id interface{}, model Model) error

FindByID method find a doc and decode it to model, otherwise return error. id field can be any value that if passed to `PrepareID` method, it return valid id(e.g string,bson.ObjectId).

func (*Collection) FindByIDWithCtx

func (coll *Collection) FindByIDWithCtx(ctx context.Context, id interface{}, model Model) error

FindByIDWithCtx method find a doc and decode it to model, otherwise return error. id field can be any value that if passed to `PrepareID` method, it return valid id(e.g string,bson.ObjectId).

func (*Collection) First

func (coll *Collection) First(filter interface{}, model Model, opts ...*options.FindOneOptions) error

First method search and return first document of search result.

func (*Collection) FirstWithCtx

func (coll *Collection) FirstWithCtx(ctx context.Context, filter interface{}, model Model, opts ...*options.FindOneOptions) error

FirstWithCtx method search and return first document of search result.

func (*Collection) SimpleAggregate

func (coll *Collection) SimpleAggregate(results interface{}, stages ...interface{}) error

SimpleAggregate does simple aggregation and decode aggregate result to the results. stages value can be Operator|bson.M Note: you can not use this method in a transaction because it does not get context. So you should use the regular aggregation method in transactions.

func (*Collection) SimpleAggregateCursor

func (coll *Collection) SimpleAggregateCursor(stages ...interface{}) (*mongo.Cursor, error)

SimpleAggregateCursor doing simple aggregation and return cursor. Note: you can not use this method in a transaction because it does not get context. So you should use the regular aggregation method in transactions.

func (*Collection) SimpleAggregateFirst

func (coll *Collection) SimpleAggregateFirst(result interface{}, stages ...interface{}) (bool, error)

SimpleAggregateFirst does simple aggregation and decode first aggregate result to the provided result param. stages value can be Operator|bson.M Note: you can not use this method in a transaction because it does not get context. So you should use the regular aggregation method in transactions.

func (*Collection) SimpleFind

func (coll *Collection) SimpleFind(results interface{}, filter interface{}, opts ...*options.FindOptions) error

SimpleFind find and decode result to results.

func (*Collection) SimpleFindWithCtx

func (coll *Collection) SimpleFindWithCtx(ctx context.Context, results interface{}, filter interface{}, opts ...*options.FindOptions) error

SimpleFindWithCtx find and decode result to results.

func (*Collection) Update

func (coll *Collection) Update(model Model, opts ...*options.UpdateOptions) error

Update function update save changed model into database. On call to this method also mgm call to model's updating,updated, saving,saved hooks.

func (*Collection) UpdateWithCtx

func (coll *Collection) UpdateWithCtx(ctx context.Context, model Model, opts ...*options.UpdateOptions) error

UpdateWithCtx function update save changed model into database. On call to this method also mgm call to model's updating,updated, saving,saved hooks.

type CollectionGetter

type CollectionGetter interface {
	// Collection method return collection
	Collection() *Collection
}

CollectionGetter interface contain method to return collection of model.

type CollectionNameGetter

type CollectionNameGetter interface {
	// CollectionName method return model collection's name.
	CollectionName() string
}

CollectionNameGetter interface contain method to return collection name of model.

type Config

type Config struct {
	// Set to 10 second (10*time.Second) for example.
	CtxTimeout time.Duration
}

Config struct contain extra config.

func DefaultConfigs

func DefaultConfigs() (*Config, *mongo.Client, *mongo.Database, error)

DefaultConfigs return you'r default mongodb configs.

type CreatedHook

type CreatedHook interface {
	Created() error
}

CreatedHook call after model has been created

type CreatingHook

type CreatingHook interface {
	Creating() error
}

CreatingHook call before saving new model into database

type DBConfig

type DBConfig struct {
	MongoURI string
	DBName   string
}

type DateFields

type DateFields struct {
	CreatedAt time.Time `json:"created_at" bson:"created_at"`
	UpdatedAt time.Time `json:"updated_at" bson:"updated_at"`
}

func (*DateFields) Creating

func (f *DateFields) Creating() error

Creating hook used here to set `created_at` field value on inserting new model into database.

func (*DateFields) Saving

func (f *DateFields) Saving() error

Saving hook used here to set `updated_at` field value on create/update model.

type DefaultFilter

type DefaultFilter struct {
	Skip    int    `json:"skip"`
	Take    int    `json:"take"`
	Filters bson.M `json:"filters"`
}

type DeletedHook

type DeletedHook interface {
	Deleted(result *mongo.DeleteResult) error
}

DeletedHook call after model has been deleted)

type DeletingHook

type DeletingHook interface {
	Deleting() error
}

DeletingHook call before deleting model

type IDField

type IDField struct {
	ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
}

func (*IDField) GetID

func (f *IDField) GetID() interface{}

GetID method return model's id

func (*IDField) PrepareID

func (f *IDField) PrepareID(id interface{}) (interface{}, error)

func (*IDField) SetID

func (f *IDField) SetID(id interface{})

type Model

type Model interface {
	Create(model Model, opts ...*options.InsertOneOptions) error
	Delete(model Model) error
	Find(model Model, filter interface{}, results interface{}, opts ...*options.FindOptions)
	FindByID(id string, model Model) error
	GetID() interface{}
	PrepareID(id interface{}) (interface{}, error)
	SetID(id interface{})
	Update(model Model, opts ...*options.UpdateOptions) error
}

Model interface contain method must be implemented by each model

type SavedHook

type SavedHook interface {
	Saved() error
}

SavedHook call after model has been saved in database.

type SavingHook

type SavingHook interface {
	Saving() error
}

SavingHook call before save model(new or existed model) into database.

type UpdatedHook

type UpdatedHook interface {
	Updated(result *mongo.UpdateResult) error
}

UpdatedHook call after model updated

type UpdatingHook

type UpdatingHook interface {
	Updating() error
}

UpdatingHook call when before updating model

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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