Documentation
¶
Index ¶
- Variables
- func CollName(m Model) string
- func Ctx() context.Context
- func NewClient(opts ...*options.ClientOptions) (*mongo.Client, error)
- func NewCtx(timeout time.Duration) context.Context
- func ResetDefaultConfig()
- func SetDefaultConfig(conf *Config, dbName string, opts ...*options.ClientOptions) (err error)
- type Collection
- func (coll *Collection) Create(model Model) error
- func (coll *Collection) Delete(model Model) error
- func (coll *Collection) FindByID(id interface{}, model Model) error
- func (coll *Collection) First(filter interface{}, model Model, opts ...*options.FindOneOptions) error
- func (coll *Collection) Save(model Model) error
- func (coll *Collection) SimpleAggregate(results interface{}, stages ...interface{}) error
- func (coll *Collection) SimpleAggregateCursor(stages ...interface{}) (*mongo.Cursor, error)
- func (coll *Collection) SimpleFind(results interface{}, filter interface{}, opts ...*options.FindOptions) error
- func (coll *Collection) Update(model Model) error
- type CollectionGetter
- type CollectionNameGetter
- type Config
- type CreatedHook
- type CreatingHook
- type DateFields
- type DefaultModel
- type DeletedHook
- type DeletingHook
- type IDField
- type Model
- type SavedHook
- type SavingHook
- type UpdatedHook
- type UpdatingHook
Constants ¶
This section is empty.
Variables ¶
var Version = "1.0.0"
Version variable
Functions ¶
func CollName ¶
CollName check if you provided collection name in your model, return it's name, otherwise guess model collection's name.
func NewClient ¶
func NewClient(opts ...*options.ClientOptions) (*mongo.Client, error)
NewClient return new mongodb client.
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 .
Types ¶
type Collection ¶
type Collection struct {
*mongo.Collection
}
Collection performs operations on models and given Mongodb 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) error
Create 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) 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) 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) Save ¶
func (coll *Collection) Save(model Model) error
Save method save model(insert,update).
func (*Collection) SimpleAggregate ¶
func (coll *Collection) SimpleAggregate(results interface{}, stages ...interface{}) error
SimpleAggregate doing simple aggregation and decode aggregate result to the results. stages value can be Operator|bson.M
func (*Collection) SimpleAggregateCursor ¶
func (coll *Collection) SimpleAggregateCursor(stages ...interface{}) (*mongo.Cursor, error)
SimpleAggregateCursor doing simple aggregation and return cursor.
func (*Collection) SimpleFind ¶
func (coll *Collection) SimpleFind(results interface{}, filter interface{}, opts ...*options.FindOptions) error
SimpleFind find and decode result to results.
func (*Collection) Update ¶
func (coll *Collection) Update(model Model) 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.
type CollectionGetter ¶
type CollectionGetter interface {
// Collection method return collection
Collection() *Collection
}
CollectionGetter interface contain method to return model's custom collection.
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 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 DateFields ¶
type DateFields struct {
CreatedAt time.Time `json:"created_at" bson:"created_at"`
UpdatedAt time.Time `json:"updated_at" bson:"updated_at"`
}
DateFields struct contain `created_at` and `updated_at` fields that autofill on insert/update model.
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 DefaultModel ¶
type DefaultModel struct {
IDField `bson:",inline"`
DateFields `bson:",inline"`
}
DefaultModel struct contain model's default fields.
func (*DefaultModel) Creating ¶
func (model *DefaultModel) Creating() error
Creating function call to it's inner fields defined hooks
func (*DefaultModel) Saving ¶
func (model *DefaultModel) Saving() error
Saving function call to it's inner fields defined hooks
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 ¶
IDField struct contain model's ID field.
type Model ¶
type Model interface {
// PrepareID convert id value if need, and then
// return it.(e.g convert string to objectId)
PrepareID(id interface{}) (interface{}, error)
IsNew() bool
GetID() interface{}
SetID(id interface{})
}
Model interface is base method that must implement by each model, If you're using `DefaultModel` struct in your model, don't need to implement any of those method.
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