Documentation ¶
Index ¶
- Variables
- func CollName(m Model) string
- func Ctx() context.Context
- func NewClient(opts ...*options.ClientOptions) (*mongo.Client, error)
- func NewConnection(config DBConfig) (*mongo.Client, *mongo.Database, error)
- func NewCtx(timeout time.Duration) context.Context
- func ResetDefaultConfig()
- func SetDefaultConfig(conf *Config, dbName string, opts ...*options.ClientOptions) (err error)
- func UpsertTrueOption() *options.UpdateOptions
- type BaseModel
- func (m *BaseModel) Create(model Model, opts ...*options.InsertOneOptions) error
- func (m *BaseModel) Delete(model Model) error
- func (m *BaseModel) Find(model Model, filter interface{}, results interface{}, ...)
- func (m *BaseModel) FindByID(id string, model Model) error
- func (m *BaseModel) Update(model Model, opts ...*options.UpdateOptions) error
- type Collection
- func (coll *Collection) Create(model Model, opts ...*options.InsertOneOptions) error
- func (coll *Collection) CreateWithCtx(ctx context.Context, model Model, opts ...*options.InsertOneOptions) error
- func (coll *Collection) Delete(model Model) error
- func (coll *Collection) DeleteWithCtx(ctx context.Context, model Model) error
- func (coll *Collection) FindByID(id interface{}, model Model) error
- func (coll *Collection) FindByIDWithCtx(ctx context.Context, id interface{}, model Model) error
- func (coll *Collection) First(filter interface{}, model Model, opts ...*options.FindOneOptions) error
- func (coll *Collection) FirstWithCtx(ctx context.Context, filter interface{}, model Model, ...) error
- func (coll *Collection) SimpleAggregate(results interface{}, stages ...interface{}) error
- func (coll *Collection) SimpleAggregateCursor(stages ...interface{}) (*mongo.Cursor, error)
- func (coll *Collection) SimpleAggregateFirst(result interface{}, stages ...interface{}) (bool, error)
- func (coll *Collection) SimpleFind(results interface{}, filter interface{}, opts ...*options.FindOptions) error
- func (coll *Collection) SimpleFindWithCtx(ctx context.Context, results interface{}, filter interface{}, ...) error
- func (coll *Collection) Update(model Model, opts ...*options.UpdateOptions) error
- func (coll *Collection) UpdateWithCtx(ctx context.Context, model Model, opts ...*options.UpdateOptions) error
- type CollectionGetter
- type CollectionNameGetter
- type Config
- type CreatedHook
- type CreatingHook
- type DBConfig
- type DateFields
- type DefaultFilter
- type DeletedHook
- type DeletingHook
- type IDField
- type Model
- type SavedHook
- type SavingHook
- type UpdatedHook
- type UpdatingHook
Constants ¶
This section is empty.
Variables ¶
var MongoDB *mongo.Database
var MongoDBClient *mongo.Client
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 .
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
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 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"` }
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 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 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