-
Notifications
You must be signed in to change notification settings - Fork 153
Hooks (简体中文)
当前为Qmgo
的Hook
实现第一个版本,后面不排除有重构和使用方式的变更.
一般的Hook
会通过被操作文档实现hook
回调方法,以实现Hook
功能。但是Qmgo
的很多操作入参没有文档,同时我们认为这是一种很简洁的操作方式.
所以Qmgo
的Hook v1
和一般的Hook
实现会有不同:
- 用户自己实现Hook的结构体方法,通过具体某个操作的
options
传递给Qmgo
,Qmgo
自动回调 - 如果
Hook
操作失败,暂时没有做回滚数据库操作的功能
实现Insert Hook
,用户需要实现结构体方法:
BeforeInsert() error
AfterInsert() error
在InsertOne
流程中
-
通过自定义结构(下例的
User
)实现Insert
的Hook
的方法 -
通过
options.InsertOneOptions
传入来实现Hook
回调。 -
如果使用文档
User
来直接实现Hook
的方法,在BeforeInsert()
中修改文档,修改后的文档会被插入数据库
type User struct {
Name string `bson:"name"`
Age int `bson:"age"`
}
func (u *User) BeforeInsert() error {
fmt.Println("before insert called")
return nil
}
func (u *User) AfterInsert() error {
fmt.Println("before insert called")
return nil
}
// --- running codes:
u := &User{Name: "Alice", Age: 7}
_, err := cli.InsertOne(context.Background(), u, options.InsertOneOptions{
InsertHook: u,
})
在InsertMany
流程中
-
通过自定义结构(下例的
User
)实现Insert
的Hook
方法, -
通过
options.InsertManyOptions
传入来实现Hook
回调。 -
如果使用文档
User
来直接实现Hook
方法,在BeforeInsert()
中修改文档,修改后的文档会被插入数据库 -
因为插入的是多个文档,而
hook
是针对每个文档都会发生,所以Hook
会根据插入文档数量被回调多次
type User struct {
Name string `bson:"name"`
Age int `bson:"age"`
}
func (u *User) BeforeInsert() error {
fmt.Println("before insert called")
return nil
}
func (u *User) AfterInsert() error {
fmt.Println("before insert called")
return nil
}
// --- running codes:
u1 := &User{Name: "jz", Age: 7}
u2 := &User{Name: "xm", Age: 7}
us := []*User{u1, u2}
_, err := cli.InsertMany(ctx, us, options.InsertManyOptions{
InsertHook: us,
})
实现Update
操作的Hook
,用户需要实现结构体方法:
BeforeUpdate() error
AfterUpdate() error
在UpdateOne
/UpdateAll
流程中
-
通过自定义结构(下例中的
MyUpdateHook
)实现Insert
的Hook
方法 -
通过
options.UpdateOptions
传入来实现Hook
回调。 -
如果使用文档
User
来直接实现Hook
方法,在方法中对文档进行操作,不会 对写入数据库中的文档产生影响。
type MyUpdateHook struct {
beforeUpdateCount int
afterUpdateCount int
}
func (u *MyUpdateHook) BeforeUpdate() error {
u.beforeUpdateCount++
return nil
}
func (u *MyUpdateHook) AfterUpdate() error {
u.afterUpdateCount++
return nil
}
u := User{Name: "jz", Age: 7}
uh := &MyUpdateHook{}
_, err := cli.InsertOne(context.Background(), u)
ast.NoError(err)
err = cli.UpdateOne(ctx, bson.M{"name": "jz"}, bson.M{operator.Set: bson.M{"age": 27}}, options.UpdateOptions{
UpdateHook: uh,
})
cli.UpdateAll(ctx, bson.M{"name": "jz"}, bson.M{operator.Set: bson.M{"age": 27}}, options.UpdateOptions{
UpdateHook: uh,
})
实现Remove
操作的Hook
,用户需要实现结构体方法:
BeforeRemove() error
AfterRemove() error
在Remove
/RemoveAll
流程中
- 自定义结构(下例中的
MyRemoveHook
)实现Remove
的Hook
方法 - 通过
options.RemoveOptions
传入来实现Hook
回调。
type MyRemoveHook struct {
beforeCount int
afterCount int
}
func (m *MyRemoveHook) BeforeRemove() error {
m.beforeCount++
return nil
}
func (m *MyRemoveHook) AfterRemove() error {
m.afterCount++
return nil
}
rh := &MyRemoveHook{}
err = cli.Remove(ctx, bson.M{"age": 17}, options.RemoveOptions{
RemoveHook: rh,
})
rh = &MyRemoveHook{}
_, err = cli.RemoveAll(ctx, bson.M{"age": "7"}, options.RemoveOptions{
RemoveHook: rh,
})
实现Query
操作的Hook
,用户需要实现结构体方法:
BeforeQuery() error
AfterQuery() error
在Find().One/Find().All()
流程中,
- 自定义结构(下例中的
MyQueryHook
)实现Query
的Hook
方法 - 通过
options.FindOptions
传入来实现Hook
回调。
type MyQueryHook struct {
beforeCount int
afterCount int
}
func (q *MyQueryHook) BeforeQuery() error {
q.beforeCount++
return nil
}
func (q *MyQueryHook) AfterQuery() error {
q.afterCount++
return nil
}
qk := &MyQueryHook{}
err = cli.Find(ctx, bson.M{"age": 17}, options.FindOptions{
QueryHook: qk,
}).One(ur)
err = cli.Find(ctx, bson.M{"age": 17}, options.FindOptions{
QueryHook: qh,
}).All(&ur)