-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hooks v2 #72
Comments
I will try to help, if you can explain to me what you did in hooks v1 will be helpfull |
Thanks! |
Hi thanks for explain the beginning! Now i'm currently trying to implement a better way to hooks... And I liked this one: package hook
type Operation int
const (
Insert Operation = iota
Update
Delete
)
type Moment int
const (
After Moment = iota
Before
)
// Hook ...
type Hook struct {
FuncName string // FuncName represent default name to call
Prior int // Prior represent the priority of this hook
}
// Processor ...
type Processor struct {
Hooks map[Moment]map[Operation]Hook
} So I'm thinking to create a // InsertOne insert one document into the collection
// Reference: https://docs.mongodb.com/manual/reference/command/insert/
func (c *Collection) InsertOne(ctx context.Context, doc interface{}, opts ...qOpts.InsertOneOptions) (result *InsertOneResult, err error) {
hook.Call(hook.Processor[After][Insert], &doc)
res, err := c.collection.InsertOne(ctx, doc)
if res != nil {
result = &InsertOneResult{InsertedID: res.InsertedID}
}
hook.Call(hook.Processor[Before][Insert], &doc)
return
} Here is an example of what i'm calling |
Thanks! |
@jiangz222 I believe the way I'm coding, it is more advantageous for the user because he does not need to pass the option in each operation, he could just register hooks before client operations and that's it. All the hard work is for I haven't tested it yet, but I believe we could use a struct in second parameter in Update operation. Or we can refactor to add more flexibility... |
@mirusky Actually, the current implement of hook is flexible. the real problem is #73. type UserHook struct {
}
func (u *UserHook) BeforeInsert() error {
}
func (u *UserHook) AfterInsert() error {
}
func (c *Collection) InsertOne(ctx context.Context, doc interface{}) (result *InsertOneResult, err error) {
// use doc directly and hook callback method of struct of doc.(UserType)
if err = hook.Do(doc, hook.BeforeInsert); err != nil {
return
}
res, err := c.collection.InsertOne(ctx, doc)
if res != nil {
result = &InsertOneResult{InsertedID: res.InsertedID}
}
if err = hook.Do(doc, hook.AfterInsert); err != nil {
return
}
return
} Above solution is supported and is the first choice to hook, and I already test it, you can check more here. |
So how to resolve a multiple "BeforeInsert" hook? Like user has custom Methods in struct, so he needs use "BeforeInsert" Method to call all others? Like: type UserHook struct {
}
func (u *UserHook) BeforeInsert() error {
u.MyCustomHook()
u.MyCustomHook2()
}
func (u *UserHook) AfterInsert() error {
}
func (u *UserHook) MyCustomHook() error {
}
func (u *UserHook) MyCustomHook2() error {
} |
@mirusky |
Because Qmgo is not a typical ORM, so After #78 supported(hooks of |
#71 implement the Hooks v1 in qmgo: More about hooks | Hooks详情介绍
But more jobs need to do:
default and custom fields in update operation
is blocked now Support default fields and custom fields #62Update
operation.The text was updated successfully, but these errors were encountered: