Skip to content

Hooks (简体中文)

jiangz222 edited this page Aug 30, 2020 · 15 revisions

About Hooks v1:

当前为QmgoHook实现第一个版本,后面不排除有重构和使用方式的变更.

一般的Hook会通过被操作文档实现hook 回调方法,以实现Hook功能。但是Qmgo的很多操作入参没有文档,同时我们认为这是一种很简洁的操作方式.

所以QmgoHook v1和一般的Hook实现会有不同:

  • 用户自己实现Hook的结构体方法,通过具体某个操作的options传递给QmgoQmgo 自动回调
  • 如果Hook操作失败,暂时没有做回滚数据库操作的功能

Insert Hook

实现Insert Hook,用户需要实现结构体方法:

BeforeInsert() error 
AfterInsert() error

InsertOne Hook

InsertOne流程中

  • 通过自定义结构(下例的User)实现InsertHook的方法

  • 通过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 Hook

InsertMany流程中

  • 通过自定义结构(下例的User)实现InsertHook方法,

  • 通过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

实现Update操作的Hook,用户需要实现结构体方法:

BeforeUpdate() error
AfterUpdate() error

UpdateOne/UpdateAll流程中

  • 通过自定义结构(下例中的MyUpdateHook)实现InsertHook方法

  • 通过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

实现Remove操作的Hook,用户需要实现结构体方法:

BeforeRemove() error
AfterRemove() error

Remove/RemoveAll流程中

  • 自定义结构(下例中的MyRemoveHook)实现RemoveHook方法
  • 通过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,
})

QueryHook

实现Query操作的Hook,用户需要实现结构体方法:

BeforeQuery() error
AfterQuery() error

Find().One/Find().All()流程中,

  • 自定义结构(下例中的MyQueryHook)实现QueryHook方法
  • 通过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)
Clone this wiki locally