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)
- func Transaction(f TransactionFunc) error
- func TransactionWithClient(ctx context.Context, client *mongo.Client, f TransactionFunc) error
- func TransactionWithCtx(ctx context.Context, f TransactionFunc) 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) 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 DateFields
- type DefaultModel
- type DeletedHook
- type DeletingHook
- type IDField
- type Model
- type SavedHook
- type SavingHook
- type TransactionFunc
- type UpdatedHook
- type UpdatingHook
Constants ¶
This section is empty.
Variables ¶
var Version = "2.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 .
func Transaction ¶
func Transaction(f TransactionFunc) error
Transaction run a transaction with default client..
func TransactionWithClient ¶
TransactionWithClient run transaction with the given client.
func TransactionWithCtx ¶
func TransactionWithCtx(ctx context.Context, f TransactionFunc) error
TransactionWithCtx run transaction with the given context and default client.
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, 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 doing 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) 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 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) 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 TransactionFunc ¶
type TransactionFunc func(session mongo.Session, sc mongo.SessionContext) error
TransactionFunc is handler to manage transaction.
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