/
cmloader.go
52 lines (46 loc) · 1.23 KB
/
cmloader.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
package dataloader
import (
"context"
"github.com/target/goalert/user/contactmethod"
"time"
)
// CMLoader will load user contact methods from postgres.
type CMLoader struct {
*loader
store contactmethod.Store
}
// NewCMLoader will create a new CMLoader using the provided store for fetch operations.
func NewCMLoader(ctx context.Context, store contactmethod.Store) *CMLoader {
p := &CMLoader{
store: store,
}
p.loader = newLoader(ctx, loaderConfig{
Max: 100,
Delay: time.Millisecond,
IDFunc: func(v interface{}) string { return v.(*contactmethod.ContactMethod).ID },
FetchFunc: p.fetch,
})
return p
}
// FetchOne will fetch a single record from the store, batching requests to the store.
func (l *CMLoader) FetchOne(ctx context.Context, id string) (*contactmethod.ContactMethod, error) {
v, err := l.loader.FetchOne(ctx, id)
if err != nil {
return nil, err
}
if v == nil {
return nil, err
}
return v.(*contactmethod.ContactMethod), nil
}
func (l *CMLoader) fetch(ctx context.Context, ids []string) ([]interface{}, error) {
many, err := l.store.FindMany(ctx, ids)
if err != nil {
return nil, err
}
res := make([]interface{}, len(many))
for i := range many {
res[i] = &many[i]
}
return res, nil
}