forked from coze-cloud/clerk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
54 lines (46 loc) 路 911 Bytes
/
query.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
package clerk
import (
"context"
)
type query[T any] struct {
collection *Collection
filter map[string]any
sorting map[string]bool
skip int
take int
}
func NewQuery[T any](collection *Collection) *query[T] {
return &query[T]{
collection: collection,
filter: map[string]any{},
sorting: map[string]bool{},
skip: -1,
take: -1,
}
}
func (q *query[T]) Where(key string, value any) *query[T] {
q.filter[key] = value
return q
}
func (q *query[T]) SortBy(key string, asc bool) *query[T] {
q.sorting[key] = asc
return q
}
func (q *query[T]) Skip(skip int) *query[T] {
q.skip = skip
return q
}
func (q *query[T]) Take(take int) *query[T] {
q.take = take
return q
}
func (q *query[T]) Execute(ctx context.Context, querier Querier[T]) (<-chan T, error) {
return querier.Query(
ctx,
q.collection,
q.filter,
q.sorting,
q.skip,
q.take,
)
}