-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
187 lines (168 loc) · 4.64 KB
/
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package simplestore
import (
"context"
"cloud.google.com/go/firestore"
"google.golang.org/api/iterator"
)
type Query struct {
q firestore.Query
tb *targetBuilder
transaction *firestore.Transaction
}
// QuerySafe starts a new query for target
// target must be a pointer to slice of pointers to structs.
// target is also used as destination of `GetAll()`.
func (c *Client) QuerySafe(target any) (*Query, error) {
collection, tb, err := c.getCollectionRef(target)
if err != nil {
return nil, err
}
return &Query{
q: collection.Query,
tb: tb,
transaction: c.FirestoreTransaction,
}, nil
}
// Query starts a new query for target
// target must be a pointer to slice of pointers to structs.
// target is also used as destination of `GetAll()`.
// Panic if inappropriate target is specified.
func (c *Client) Query(target any) *Query {
q, err := c.QuerySafe(target)
if err != nil {
panic(err)
}
return q
}
// QueryGroupSafe starts a new query for target as collection group
// target must be a pointer to slice of pointers to structs.
// target is also used as destination of `GetAll()`.
func (c *Client) QueryGroupSafe(target any) (*Query, error) {
cgroup, tb, err := c.getCollectionGroupRef(target)
if err != nil {
return nil, err
}
return &Query{
q: cgroup.Query,
tb: tb,
transaction: c.FirestoreTransaction,
}, nil
}
// QueryGroup starts a new query for target as collection group
// target must be a pointer to slice of pointers to structs.
// target is also used as destination of `GetAll()`.
// Panic if inappropriate target is specified.
func (c *Client) QueryGroup(target any) *Query {
q, err := c.QueryGroupSafe(target)
if err != nil {
panic(err)
}
return q
}
// QueryNested starts a new query for target under the document specified by `parent`
// parent must be a pointer to a struct.
// target must be a pointer to slice of pointers to structs.
// target is also used as destination of `GetAll()`.
func (c *Client) QueryNested(parent any, target any) (*Query, error) {
cgroup, tb, err := c.getNestedCollectionRef(parent, target)
if err != nil {
return nil, err
}
return &Query{
q: cgroup.Query,
tb: tb,
transaction: c.FirestoreTransaction,
}, nil
}
// Iter runs query and calls callback for each document
// A pointer to a struct is passed.
func (q *Query) Iter(ctx context.Context, f func(o any) error) error {
var iter *firestore.DocumentIterator
if q.transaction == nil {
iter = q.q.Documents(ctx)
} else {
iter = q.transaction.Documents(q.q)
}
defer iter.Stop()
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
return err
}
dst := q.tb.createElement()
err = doc.DataTo(dst)
if err != nil {
return err
}
err = f(dst)
if err != nil {
return err
}
}
return nil
}
// GetAll runs query and retrieve all results
// results are stored to `target` passed in `Query()`, `QueryGroup()` or `QueryNested()`
func (q *Query) GetAll(ctx context.Context) error {
return q.Iter(ctx, func(o any) error {
q.tb.append(o)
return nil
})
}
// Where sets the condition for the query
func (q *Query) Where(path, op string, value interface{}) *Query {
newQ := *q
newQ.q = q.q.Where(path, op, value)
return &newQ
}
// OrderBy sets the order of the query result
func (q *Query) OrderBy(path string, dir firestore.Direction) *Query {
newQ := *q
newQ.q = q.q.OrderBy(path, dir)
return &newQ
}
// Offset sets the offset of the query
func (q *Query) Offset(n int) *Query {
newQ := *q
newQ.q = q.q.Offset(n)
return &newQ
}
// Limit sets the max count of documents of the query
func (q *Query) Limit(n int) *Query {
newQ := *q
newQ.q = q.q.Limit(n)
return &newQ
}
// LimitToLast sets the max count of documents of the query
func (q *Query) LimitToLast(n int) *Query {
newQ := *q
newQ.q = q.q.LimitToLast(n)
return &newQ
}
// StartAt sets the start position of the query
func (q *Query) StartAt(docSnapshotOrFieldValues ...interface{}) *Query {
newQ := *q
newQ.q = q.q.StartAt(docSnapshotOrFieldValues...)
return &newQ
}
// StartAt sets the start position of the query
func (q *Query) StartAfter(docSnapshotOrFieldValues ...interface{}) *Query {
newQ := *q
newQ.q = q.q.StartAfter(docSnapshotOrFieldValues...)
return &newQ
}
// EndAt sets the end position of the query
func (q *Query) EndAt(docSnapshotOrFieldValues ...interface{}) *Query {
newQ := *q
newQ.q = q.q.EndAt(docSnapshotOrFieldValues...)
return q
}
// EndBefore set the end position of the query
func (q *Query) EndBefore(docSnapshotOrFieldValues ...interface{}) *Query {
newQ := *q
newQ.q = q.q.EndBefore(docSnapshotOrFieldValues...)
return q
}