-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks.go
56 lines (49 loc) · 1.34 KB
/
hooks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package nidhi
import "context"
// Hooks are callbacks that are called on store operations.
//
// The passed values can if modified will change for the op.
type Hooks struct {
// OnCreate is called on [Store.Create].
OnCreate OnCreateHook
// OnGet is called on [Store.Get].
OnGet OnGetHook
// OnQuery is called on [Store.Query].
OnQuery OnQueryHook
// OnDelete is called on [Store.Delete].
OnDelete OnDeleteHook
// OnDeleteMany is called on [Store.DeleteMany].
OnDeleteMany OnDeleteManyHook
// OnReplace is called on [Store.Replace].
OnReplace OnReplaceHook
// OnUpdate is called on [Store.Update].
OnUpdate OnUpdateHook
// OnUpdateMany is called on [Store.UpdateMany].
OnUpdateMany OnUpdateManyHook
}
// HookContext is the context passed to [Hooks]
type HookContext struct {
context.Context
idFn func(any) string
setIdFn func(any, string)
}
// NewHookContext returns a [HookContext].
func NewHookContext[T any](ctx context.Context, store *Store[T]) *HookContext {
return &HookContext{
Context: ctx,
idFn: func(t any) string {
return store.idFn(t.(*T))
},
setIdFn: func(t any, s string) {
store.setIdFn(t.(*T), s)
},
}
}
// SetId sets the id field of the document.
func (s *HookContext) SetId(v any, id string) {
s.setIdFn(v, id)
}
// Id returns the id of the document
func (s *HookContext) Id(v any) string {
return s.idFn(v)
}