Plugin
go mongox
supports plugin programming, providing a flexible way to insert custom logic before and after database operations, thereby enhancing the scalability and maintainability of applications.
Enabling Built-in Plugins (Hooks)
The go mongox
library comes with three useful built-in hook
plugins (implemented based on plugins):
field
hook: Automates the update of the defaultfield
fieldmodel
hook: Sets up hook functions for models (structs), which are called before and after collection operations inMongoDB
.validator
hook: Uses struct tags (tag
) to validate field values.
The go mongox
library does not activate these hooks by default. If you want to activate them, you can refer to the following code:
mongox.InitPlugin(&mongox.PluginConfig{
EnableDefaultFieldHook: true,
EnableModelHook: true,
EnableValidationHook: true,
// Override the default validator, effective when EnableValidationHook is true
Validate: nil,
})
Plugin Registration and Removal
go mongox
provides the RegisterPlugin
and UnregisterPlugin
methods to register and remove plugins.
// Register Plugin
mongox.RegisterPlugin("after find", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
if user, ok := opCtx.Doc.(*User); ok {
fmt.Println(user)
}
if users, ok := opCtx.Doc.([]*User); ok {
fmt.Println(users)
}
return nil
}, operation.OpTypeAfterFind)
// Remove Plugin
mongox.RemovePlugin("after find", operation.OpTypeAfterFind)
The function signature of mongox.RegisterPlugin
is as follows:
func RegisterPlugin(name string, cb callback.CbFn, opType operation.OpType)
This function takes three parameters:
name
: The name of the plugincb
: The callback function of the plugin, the signature of which is as follows:gotype CbFn func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error
This function takes three parameters:
ctx
: The context objectopCtx
: The operation context objectopCtx
has the following structure:gotype OpContext struct { Col *mongo.Collection `opt:"-"` Doc any // filter also can be used as query Filter any Updates any Replacement any MongoOptions any ModelHook any }
When
opType
isoperation.OpTypeBeforeInsert
oroperation.OpTypeAfterInsert
, the value ofopCtx.Doc
may not benil
, and its type may be*struct
or[]*struct
.When
opType
isoperation.OpTypeBeforeUpdate
oroperation.OpTypeAfterUpdate
, the values ofopCtx.Filter
andopCtx.Updates
may not benil
.When
opType
isoperation.OpTypeBeforeDelete
oroperation.OpTypeAfterDelete
, the value ofopCtx.Filter
may not benil
.When
opType
isoperation.OpTypeBeforeUpsert
oroperation.OpTypeAfterUpsert
, the values ofopCtx.Filter
andopCtx.Updates
may not benil
.When
opType
isoperation.OpTypeBeforeFind
oroperation.OpTypeAfterFind
, the value ofopCtx.Filter
may not benil
, and if it is the latter, the value ofopCtx.Doc
may not benil
.The
MongoOptions
andModelHook
beingnil
depends on whether the user sets them during the execution of theMongoDB
operations.opts
: Optional parameters
opType
: The type of the plugin, the supported types are as follows:type description operation.OpTypeBeforeInsert
Execute before inserting the document operation.OpTypeAfterInsert
Execute after inserting the document operation.OpTypeBeforeUpdate
Execute before updating the document operation.OpTypeAfterUpdate
Execute after updating the document operation.OpTypeBeforeDelete
Execute before deleting the document operation.OpTypeAfterDelete
Execute after deleting the document operation.OpTypeBeforeUpsert
Execute before saving the document operation.OpTypeAfterUpsert
Execute after saving the document operation.OpTypeBeforeFind
Execute before finding the document operation.OpTypeAfterFind
Execute after finding the document