-
Notifications
You must be signed in to change notification settings - Fork 152
Hooks (简体中文)
当前为Qmgo的Hook实现第一个版本,后面不排除有使用方式的变更.
一般的Hook会通过被操作文档实现hook 回调方法,以实现Hook功能。但是Qmgo的很多操作入参没有文档,同时我们认为这是一种很简洁的操作方式.
所以Qmgo的Hook v1和一般的Hook实现会有不同:
-
用户自己实现Hook的结构体方法,通过具体某个操作的
options传递给Qmgo,Qmgo自动回调 -
这3个API
InsertOne、InsertManyandUpdateWithDocument的回调可以更简单,不依赖于options -
如果
Hook操作失败,暂时没有做回滚数据库操作的功能
实现Insert Hook,用户需要实现结构体方法:
BeforeInsert() error
AfterInsert() error在InsertOne流程中
-
通过自定义结构(下例的
User)实现Insert的Hook的方法 -
通过
options.InsertOneOptions传入来实现Hook回调 -
如果第二个参数
doc实现了Insert的Hook的方法,那么可以不传入options.InsertOneOptions -
如果使用文档
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
}
u := &User{Name: "Alice", Age: 7}
_, err := cli.InsertOne(context.Background(), u)
// 下面的例子也可以工作
// _, err := cli.InsertOne(context.Background(), u, options.InsertOneOptions{
// InsertHook: u,
// })在InsertMany流程中
-
通过自定义结构(下例的
User)实现Insert的Hook方法, -
通过
options.InsertManyOptions传入来实现Hook回调。 -
如果第二个参数
docs实现了Insert的Hook的方法,那么可以不传入options.InsertManyOptions -
如果使用文档
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
}
u1 := &User{Name: "Lucas", Age: 7}
u2 := &User{Name: "Alice", Age: 7}
us := []*User{u1, u2}
_, err := cli.InsertMany(ctx, us, options.InsertManyOptions{
InsertHook: us,
})
// 下面的代码也能够工作
// _, err := cli.InsertMany(ctx, us, options.InsertManyOptions{
// InsertHook: us,
// })实现Update操作的Hook,用户需要实现结构体方法:
BeforeUpdate() error
AfterUpdate() error-
通过自定义结构(下例中的
MyUpdateHook)实现Update的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: "Lucas", Age: 7}
uh := &MyUpdateHook{}
_, err := cli.InsertOne(context.Background(), u)
ast.NoError(err)
err = cli.UpdateOne(ctx, bson.M{"name": "Lucas"}, bson.M{operator.Set: bson.M{"age": 27}}, options.UpdateOptions{
UpdateHook: uh,
})
cli.UpdateAll(ctx, bson.M{"name": "Lucas"}, bson.M{operator.Set: bson.M{"age": 27}}, options.UpdateOptions{
UpdateHook: uh,
})-
通过自定义结构(下例中的
User)实现Update的Hook方法 -
通过
options.UpdateOptions传入来实现Hook回调。 -
如果第3个参数doc实现了
Update的Hook方法,则options.UpdateOptions可以不传入 -
如果使用文档
User来直接实现Hook方法,在方法中对文档进行操作,会将改写的文档更新到数据库。
type User struct {
Name string `bson:"name"`
Age int `bson:"age"`
beforeUpdate int
afterUpdate int
}
func (u *User) BeforeUpdate() error {
u.beforeUpdate++
return nil
}
func (u *User) AfterUpdate() error {
u.afterUpdate++
return nil
}
u := &User{}
err = cli.UpdateWithDocument(ctx, bson.M{"name": "Lucas"}, &u)
// 下面的代码也可以工作
// err = cli.UpdateWithDocument(ctx, bson.M{"name": "Lucas"}, &u, options.UpdateOptions{
// UpdateHook: u,
// })实现Upsert操作的Hook,用户需要实现结构体方法:
BeforeInsert() error
AfterInsert() error在Upsert流程中
-
通过自定义结构(下例的
User)实现Upsert的Hook的方法 -
通过
options.UpsertOptions传入来实现Hook回调 -
如果第二个参数
doc实现了Upsert的Hook的方法,那么可以不传入options.UpsertOptions -
如果使用文档
User来直接实现Hook的方法,在BeforeUpsert()中修改文档,修改后的文档会被插入数据库
type User struct {
Name string `bson:"name"`
Age int `bson:"age"`
}
func (u *User) BeforeUpsert() error {
return nil
}
func (u *User) AfterUpsert() error {
return nil
}
u := &User{Name: "Alice", Age: 7}
_, err = cli.Upsert(context.Background(), bson.M{"name": "Lucas"}, u)
// 下面的代码也可以运行
// _, err = cli.Upsert(context.Background(), bson.M{"name": "Lucas"}, u, options.UpsertOptions{
// UpsertHook: myHook,
//})实现Remove操作的Hook,用户需要实现结构体方法:
BeforeRemove() error
AfterRemove() error- 自定义结构(下例中的
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- 自定义结构(下例中的
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)