-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.go
More file actions
172 lines (137 loc) · 4.11 KB
/
document.go
File metadata and controls
172 lines (137 loc) · 4.11 KB
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
package tsclient
import (
"bytes"
"encoding/json"
"net/http"
"net/url"
"reflect"
"strconv"
"emperror.dev/errors"
)
// ErrNotSlice is returned by Import if a type other than a slice is given as input.
const ErrNotSlice = errors.Sentinel("slice expected")
// Insert inserts a document into the collection.
// The inserted document is unmarshaled to `out` if it is not nil.
func (c *Client) Insert(collection string, doc interface{}, out interface{}) (err error) {
resp, err := c.Request("POST", "/collections/"+collection+"/documents", WithJSONBody(doc))
if err != nil || out == nil {
return
}
return json.Unmarshal(resp, out)
}
// Upsert inserts a document into the collection, updating it if it already exists.
// The inserted document is unmarshaled to `out` if it is not nil.
func (c *Client) Upsert(collection string, doc interface{}, out interface{}) (err error) {
resp, err := c.Request("POST", "/collections/"+collection+"/documents",
WithJSONBody(doc),
WithURLValues(url.Values{"action": {"upsert"}}),
)
if err != nil || out == nil {
return
}
return json.Unmarshal(resp, out)
}
type importResponse struct {
Success bool `json:"success"`
}
// Import imports the documents into the collection.
// It returns an error if s is not a slice.
func (c *Client) Import(collection, action string, s interface{}) (ok []bool, err error) {
slice := []interface{}{}
val := reflect.ValueOf(s)
if val.Kind() != reflect.Slice {
return nil, ErrNotSlice
}
for i := 0; i < val.Len(); i++ {
slice = append(slice, val.Index(i).Interface())
}
return c.ImportSlice(collection, action, slice)
}
// ImportSlice imports a slice of documents into the collection.
func (c *Client) ImportSlice(collection, action string, s []interface{}) (ok []bool, err error) {
b := new(bytes.Buffer)
enc := json.NewEncoder(b)
for _, doc := range s {
err = enc.Encode(doc)
if err != nil {
return nil, err
}
}
resp, err := c.Request("POST", "/collections/"+collection+"/documents/import",
WithBody(b),
WithHeader(http.Header{
"Content-Type": {"application/json"},
}),
)
if err != nil {
return
}
dec := json.NewDecoder(bytes.NewReader(resp))
for dec.More() {
var r importResponse
err = dec.Decode(&r)
if err != nil {
return
}
ok = append(ok, r.Success)
}
return
}
// Document retrieves a document from the collection by ID.
// The document is unmarshaled to `out` if it is not nil.
func (c *Client) Document(collection, id string, out interface{}) (string, error) {
resp, err := c.Request("GET", "/collections/"+collection+"/documents/"+id)
if err != nil {
return "", err
}
s := struct {
ID string `json:"id"`
}{}
err = json.Unmarshal(resp, &s)
if err != nil {
return "", err
}
if out == nil {
return s.ID, nil
}
return s.ID, json.Unmarshal(resp, out)
}
// UpdateDocument updates a document in the collection by ID.
// The updated document is unmarshaled to `out` if it is not nil.
func (c *Client) UpdateDocument(collection, id string, doc, out interface{}) error {
resp, err := c.Request("PATCH", "/collections/"+collection+"/documents/"+id,
WithJSONBody(doc))
if err != nil || out == nil {
return err
}
return json.Unmarshal(resp, out)
}
// DeleteDocument deletes a document in the collection.
// The deleted document is unmarshaled to `out` if it is not nil.
func (c *Client) DeleteDocument(collection, id string, out interface{}) error {
resp, err := c.Request("DELETE", "/collections/"+collection+"/documents/"+id)
if err != nil || out == nil {
return err
}
return json.Unmarshal(resp, out)
}
// DeleteQuery deletes documents in the collection matching filter.
// Returns the number of deleted documents.
func (c *Client) DeleteQuery(collection, filter string, batchSize int) (deleted int, err error) {
v := url.Values{"filter_by": {filter}}
if batchSize != 0 {
v["batch_size"] = []string{strconv.Itoa(batchSize)}
}
resp, err := c.Request("DELETE", "/collections/"+collection+"/documents", WithURLValues(v))
if err != nil {
return
}
s := struct {
NumDeleted int `json:"num_deleted"`
}{}
err = json.Unmarshal(resp, &s)
if err != nil {
return
}
return s.NumDeleted, nil
}