Documentation
¶
Index ¶
- Constants
- 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)
- func SetTestDefaultConfig(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
- func UpsertTrueOption() *options.UpdateOptions
- type Collection
- func (coll *Collection) Create(model Model, opts ...*options.InsertOneOptions) error
- func (coll *Collection) CreateMany(models []Model, opts ...*options.InsertManyOptions) error
- func (coll *Collection) CreateManyWithCtx(ctx context.Context, models []Model, opts ...*options.InsertManyOptions) error
- func (coll *Collection) CreateWithCtx(ctx context.Context, model Model, opts ...*options.InsertOneOptions) error
- func (coll *Collection) DelMany(c *Collection, models []Model, filter interface{}, ...) error
- func (coll *Collection) DelManyWithCtx(ctx context.Context, c *Collection, models []Model, filter interface{}, ...) 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) SimpleAggregateCursorWithCtx(ctx context.Context, stages ...interface{}) (*mongo.Cursor, error)
- func (coll *Collection) SimpleAggregateFirst(result interface{}, stages ...interface{}) (bool, error)
- func (coll *Collection) SimpleAggregateFirstWithCtx(ctx context.Context, result interface{}, stages ...interface{}) (bool, error)
- func (coll *Collection) SimpleAggregateWithCtx(ctx context.Context, results interface{}, stages ...interface{}) error
- func (coll *Collection) SimpleCount(filter interface{}, opts ...*options.CountOptions) (int64, error)
- func (coll *Collection) SimpleCountWithCtx(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, 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) SimpleSum(stages ...interface{}) (float64, error)
- func (coll *Collection) Update(model Model, opts ...*options.UpdateOptions) error
- func (coll *Collection) UpdateWithCtx(ctx context.Context, model Model, opts ...*options.UpdateOptions) error
- func (coll *Collection) UpsertDocWithCtx(ctx context.Context, filter, doc interface{}, opts ...*options.UpdateOptions) error
- func (coll *Collection) UpsertWithCtx(ctx context.Context, filter interface{}, model Model, ...) error
- type CollectionGetter
- type CollectionNameGetter
- type Config
- type CreatedHook
- type CreatedHookWithCtx
- type CreatingHook
- type CreatingHookWithCtx
- type Date
- type DateFields
- type DefaultModel
- type DefaultModelDate
- type DefaultModelDateState
- type DefaultModelState
- type DeletedHook
- type DeletedHookWithCtx
- type DeletingHook
- type DeletingHookWithCtx
- type ID
- type IDField
- type Model
- type ModelDate
- type ModelDateState
- type ModelState
- type SavedHook
- type SavedHookWithCtx
- type SavingHook
- type SavingHookWithCtx
- type State
- type StateField
- type StateFields
- type TransactionFunc
- type UpdatedHook
- type UpdatedHookWithCtx
- type UpdatingHook
- type UpdatingHookWithCtx
- type UpdatingStatesHook
Constants ¶
const Version = "4.0.2"
Version of the package
Variables ¶
This section is empty.
Functions ¶
func CollName ¶
CollName returns a model's collection name. The `CollectionNameGetter` will be used if the model implements this interface. Otherwise, the collection name is inferred based on the model's type using reflection.
func NewClient ¶
func NewClient(opts ...*options.ClientOptions) (*mongo.Client, error)
NewClient returns a new mongodb client.
func ResetDefaultConfig ¶
func ResetDefaultConfig()
ResetDefaultConfig resets the configuration values, client and database.
func SetDefaultConfig ¶
func SetDefaultConfig(conf *Config)
SetDefaultConfig initializes the client and database using the specified configuration values, or default.
func SetTestDefaultConfig ¶ added in v4.0.4
func SetTestDefaultConfig(conf *Config, dbName string, opts ...*options.ClientOptions) (err error)
func Transaction ¶
func Transaction(f TransactionFunc) error
Transaction creates a transaction with the default client.
func TransactionWithClient ¶
TransactionWithClient creates a transaction with the given client.
func TransactionWithCtx ¶
func TransactionWithCtx(ctx context.Context, f TransactionFunc) error
TransactionWithCtx creates a transaction with the given context and the default client.
func UpsertTrueOption ¶
func UpsertTrueOption() *options.UpdateOptions
UpsertTrueOption returns new instance of UpdateOptions with the upsert property set to true.
Types ¶
type Collection ¶
type Collection struct {
*mongo.Collection
}
Collection performs operations on models and the given Mongodb collection
func Coll ¶
func Coll(m Model, opts ...*options.CollectionOptions) *Collection
Coll returns the collection associated with a model.
func CollectionByName ¶
func CollectionByName(name string, opts ...*options.CollectionOptions) *Collection
CollectionByName returns a new collection using the current configuration values.
func NewCollection ¶
func NewCollection(db *mongo.Database, name string, opts ...*options.CollectionOptions) *Collection
NewCollection returns a new collection with the supplied database.
func (*Collection) Create ¶
func (coll *Collection) Create(model Model, opts ...*options.InsertOneOptions) error
Create method inserts a new model into the database.
func (*Collection) CreateMany ¶
func (coll *Collection) CreateMany(models []Model, opts ...*options.InsertManyOptions) error
CreateMany method inserts a new model into the database.
func (*Collection) CreateManyWithCtx ¶
func (coll *Collection) CreateManyWithCtx(ctx context.Context, models []Model, opts ...*options.InsertManyOptions) error
CreateManyWithCtx method inserts a new model into the database.
func (*Collection) CreateWithCtx ¶
func (coll *Collection) CreateWithCtx(ctx context.Context, model Model, opts ...*options.InsertOneOptions) error
CreateWithCtx method inserts a new model into the database.
func (*Collection) DelMany ¶ added in v4.0.3
func (coll *Collection) DelMany(c *Collection, models []Model, filter interface{}, opts ...*options.DeleteOptions) error
DeleteMany method deletemanys a model (doc) from a collection. To perform additional operations when deleting a model you should use hooks rather than overriding this method.
func (*Collection) DelManyWithCtx ¶ added in v4.0.3
func (coll *Collection) DelManyWithCtx(ctx context.Context, c *Collection, models []Model, filter interface{}, opts ...*options.DeleteOptions) error
DeleteManyWithCtx method deletemanys a model (doc) from a collection using the specified context. To perform additional operations when deleting a model you should use hooks rather than overriding this method.
func (*Collection) Delete ¶
func (coll *Collection) Delete(model Model) error
Delete method deletes a model (doc) from a collection. To perform additional operations when deleting a model you should use hooks rather than overriding this method.
func (*Collection) DeleteWithCtx ¶
func (coll *Collection) DeleteWithCtx(ctx context.Context, model Model) error
DeleteWithCtx method deletes a model (doc) from a collection using the specified context. To perform additional operations when deleting a model you should use hooks rather than overriding this method.
func (*Collection) FindByID ¶
func (coll *Collection) FindByID(id interface{}, model Model) error
FindByID method finds a doc and decodes it to a model, otherwise returns an error. The id field can be any value that if passed to the `PrepareID` method, it returns a valid ID (e.g string, bson.ObjectId).
func (*Collection) FindByIDWithCtx ¶
func (coll *Collection) FindByIDWithCtx(ctx context.Context, id interface{}, model Model) error
FindByIDWithCtx method finds a doc and decodes it to a model, otherwise returns an error. The id field can be any value that if passed to the `PrepareID` method, it returns a valid ID (e.g string, bson.ObjectId).
func (*Collection) First ¶
func (coll *Collection) First(filter interface{}, model Model, opts ...*options.FindOneOptions) error
First method searches and returns the first document in the search results.
func (*Collection) FirstWithCtx ¶
func (coll *Collection) FirstWithCtx(ctx context.Context, filter interface{}, model Model, opts ...*options.FindOneOptions) error
FirstWithCtx method searches and returns the first document in the search results.
func (*Collection) SimpleAggregate ¶
func (coll *Collection) SimpleAggregate(results interface{}, stages ...interface{}) error
SimpleAggregate is just same as SimpleAggregateWithCtx, but doesn't get context param.
func (*Collection) SimpleAggregateCursor ¶
func (coll *Collection) SimpleAggregateCursor(stages ...interface{}) (*mongo.Cursor, error)
SimpleAggregateCursor is just same as SimpleAggregateCursorWithCtx, but doesn't get context.
func (*Collection) SimpleAggregateCursorWithCtx ¶
func (coll *Collection) SimpleAggregateCursorWithCtx(ctx context.Context, stages ...interface{}) (*mongo.Cursor, error)
SimpleAggregateCursorWithCtx performs a simple aggregation and returns a cursor over the resulting documents. Note: you can not use this method in a transaction because it does not accept a context. To participate in transactions, please use the regular aggregation method.
func (*Collection) SimpleAggregateFirst ¶
func (coll *Collection) SimpleAggregateFirst(result interface{}, stages ...interface{}) (bool, error)
SimpleAggregateFirst is just same as SimpleAggregateFirstWithCtx, but doesn't get context param.
func (*Collection) SimpleAggregateFirstWithCtx ¶
func (coll *Collection) SimpleAggregateFirstWithCtx(ctx context.Context, result interface{}, stages ...interface{}) (bool, error)
SimpleAggregateFirstWithCtx performs a simple aggregation, decodes the first aggregate result and returns it using the provided result parameter. The value of `stages` can be Operator|bson.M Note: you can not use this method in a transaction because it does not accept a context. To participate in transactions, please use the regular aggregation method.
func (*Collection) SimpleAggregateWithCtx ¶
func (coll *Collection) SimpleAggregateWithCtx(ctx context.Context, results interface{}, stages ...interface{}) error
SimpleAggregateWithCtx performs a simple aggregation, decodes the aggregate result and returns the list using the provided result parameter. The value of `stages` can be Operator|bson.M Note: you can not use this method in a transaction because it does not accept a context. To participate in transactions, please use the regular aggregation method.
func (*Collection) SimpleCount ¶
func (coll *Collection) SimpleCount(filter interface{}, opts ...*options.CountOptions) (int64, error)
SimpleCount count documents.
func (*Collection) SimpleCountWithCtx ¶
func (coll *Collection) SimpleCountWithCtx(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)
SimpleCountWithCtx count documents using the specified context.
func (*Collection) SimpleFind ¶
func (coll *Collection) SimpleFind(results interface{}, filter interface{}, opts ...*options.FindOptions) error
SimpleFind finds, decodes and returns the results.
func (*Collection) SimpleFindWithCtx ¶
func (coll *Collection) SimpleFindWithCtx(ctx context.Context, results interface{}, filter interface{}, opts ...*options.FindOptions) error
SimpleFindWithCtx finds, decodes and returns the results using the specified context.
func (*Collection) SimpleSum ¶
func (coll *Collection) SimpleSum(stages ...interface{}) (float64, error)
SimpleSum Note: use the "total" field as $sum acumulator matchStage := bson.D{{"$match", bson.D{{"st", "win"}}}} groupStage := bson.D{{"$group", bson.D{{"_id", ""}, {"total", bson.D{{"$sum", "$tp"}}}}}}
func (*Collection) Update ¶
func (coll *Collection) Update(model Model, opts ...*options.UpdateOptions) error
Update function persists the changes made to a model to the database. Calling this method also invokes the model's mgm updating, updated, saving, and saved hooks.
func (*Collection) UpdateWithCtx ¶
func (coll *Collection) UpdateWithCtx(ctx context.Context, model Model, opts ...*options.UpdateOptions) error
UpdateWithCtx function persists the changes made to a model to the database using the specified context. Calling this method also invokes the model's mgm updating, updated, saving, and saved hooks.
func (*Collection) UpsertDocWithCtx ¶ added in v4.0.10
func (coll *Collection) UpsertDocWithCtx(ctx context.Context, filter, doc interface{}, opts ...*options.UpdateOptions) error
func (*Collection) UpsertWithCtx ¶ added in v4.0.9
func (coll *Collection) UpsertWithCtx(ctx context.Context, filter interface{}, model Model, opts ...*options.UpdateOptions) error
type CollectionGetter ¶
type CollectionGetter interface { // Collection method return collection Collection() *Collection }
CollectionGetter interface contains a method to return a model's custom collection.
type CollectionNameGetter ¶
type CollectionNameGetter interface { // CollectionName method return model collection's name. CollectionName() string }
CollectionNameGetter interface contains a method to return the collection name of a model.
type CreatedHook ¶
type CreatedHook interface {
Created() error
}
CreatedHook is called after a model has been created Deprecated: Please use CreatedHookWithCtx
type CreatedHookWithCtx ¶
CreatedHookWithCtx is called after a model has been created
type CreatingHook ¶
type CreatingHook interface {
Creating() error
}
CreatingHook is called before saving a new model to the database Deprecated: please use CreatingHookWithCtx
type CreatingHookWithCtx ¶
CreatingHookWithCtx is called before saving a new model to the database
type DateFields ¶
type DateFields struct { CreatedAt time.Time `json:"c_at" bson:"c_at"` UpdatedAt time.Time `json:"u_at" bson:"u_at"` }
DateFields struct contains the `c_at` and `u_at` fields that autofill when inserting or updating a model.
func (*DateFields) Creating ¶
func (f *DateFields) Creating() error
Creating hook is used here to set the `c_at` field value when inserting a new model into the database. TODO: get context as param the next version(4).
func (*DateFields) Saving ¶
func (f *DateFields) Saving() error
Saving hook is used here to set the `u_at` field value when creating or updating a model. TODO: get context as param the next version(4).
type DefaultModel ¶
type DefaultModel struct {
IDField `bson:",inline"`
}
DefaultModel struct contains a model's default fields.
type DefaultModelDate ¶
type DefaultModelDate struct { DefaultModel `bson:",inline"` DateFields `bson:",inline"` }
DefaultModelDate struct contains a model's default fields.
type DefaultModelDateState ¶
type DefaultModelDateState struct { DefaultModel `bson:",inline"` DateFields `bson:",inline"` StateFields `bson:",inline"` }
DefaultModelDateState struct contains a model's default fields.
type DefaultModelState ¶
type DefaultModelState struct { DefaultModel `bson:",inline"` StateFields `bson:",inline"` }
DefaultModelState struct contains a model's default fields.
type DeletedHook ¶
type DeletedHook interface {
Deleted(result *mongo.DeleteResult) error
}
DeletedHook is called after a model is deleted Deprecated: Please use DeletedHookWithCtx
type DeletedHookWithCtx ¶
type DeletedHookWithCtx interface {
Deleted(ctx context.Context, result *mongo.DeleteResult) error
}
DeletedHookWithCtx is called after a model is deleted
type DeletingHook ¶
type DeletingHook interface {
Deleting() error
}
DeletingHook is called before a model is deleted Deprecated: Please use DeletingHookWithCtx
type DeletingHookWithCtx ¶
DeletingHookWithCtx is called before a model is deleted
type IDField ¶
IDField struct contains a model's ID field.
func (*IDField) PrepareID ¶
PrepareID method prepares the ID value to be used for filtering e.g convert hex-string ID value to bson.ObjectId
type Model ¶
type Model interface { ID }
Model interface contains base methods that must be implemented by each model. If you're using the `DefaultModel` struct in your model, you don't need to implement any of these methods.
type ModelDateState ¶ added in v4.0.6
type ModelState ¶ added in v4.0.6
type SavedHook ¶
type SavedHook interface {
Saved() error
}
SavedHook is called after a model is saved to the database. Deprecated: Please use SavedHookWithCtx
type SavedHookWithCtx ¶
SavedHookWithCtx is called after a model is saved to the database.
type SavingHook ¶
type SavingHook interface {
Saving() error
}
SavingHook is called before a model (new or existing) is saved to the database. Deprecated: Please use SavingHookWithCtx
type SavingHookWithCtx ¶
SavingHookWithCtx is called before a model (new or existing) is saved to the database.
type StateField ¶
type StateField struct { State string `bson:"st" json:"st"` Updated time.Time `bson:"ts" json:"ts"` }
State with time
type StateFields ¶
type StateFields struct { State string `bson:"st" json:"st"` States []StateField `bson:"ss" json:"ss"` }
StateField list
func (*StateFields) GetState ¶ added in v4.0.8
func (f *StateFields) GetState() string
func (*StateFields) LastState ¶ added in v4.0.11
func (f *StateFields) LastState() StateField
func (*StateFields) SetState ¶ added in v4.0.5
func (f *StateFields) SetState(state string)
func (*StateFields) UpdatingStates ¶
func (f *StateFields) UpdatingStates() error
UpdatingStates hook is used here to set the `states` field value when change state into model.
type TransactionFunc ¶
type TransactionFunc func(session mongo.Session, sc mongo.SessionContext) error
TransactionFunc is a handler to manage a transaction.
type UpdatedHook ¶
type UpdatedHook interface { // Deprecated: Updated(result *mongo.UpdateResult) error }
UpdatedHook is called after a model is updated Deprecated: Please use UpdatedHookWithCtx
type UpdatedHookWithCtx ¶
type UpdatedHookWithCtx interface {
Updated(ctx context.Context, result *mongo.UpdateResult) error
}
UpdatedHookWithCtx is called after a model is updated
type UpdatingHook ¶
type UpdatingHook interface {
Updating() error
}
UpdatingHook is called before updating a model Deprecated: Please use UpdatingHookWithCtx
type UpdatingHookWithCtx ¶
UpdatingHookWithCtx is called before updating a model
type UpdatingStatesHook ¶
type UpdatingStatesHook interface {
UpdatingStates() error
}