forked from a8m/documentdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
documentdb.go
286 lines (252 loc) · 7.17 KB
/
documentdb.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//
// This project start as a fork of `github.com/nerdylikeme/go-documentdb` version
// but changed, and may be changed later
//
// Goal: add the full functionality of documentdb, align with the other sdks
// and make it more testable
//
package documentdb
import "reflect"
type Config struct {
MasterKey string
}
type DocumentDB struct {
client Clienter
}
// Create DocumentDBClient
func New(url string, config Config) *DocumentDB {
client := &Client{}
client.Url = url
client.Config = config
return &DocumentDB{client}
}
// TODO: Add `requestOptions` arguments
// Read database by self link
func (c *DocumentDB) ReadDatabase(link string) (db *Database, err error) {
err = c.client.Read(link, &db)
if err != nil {
return nil, err
}
return
}
// Read collection by self link
func (c *DocumentDB) ReadCollection(link string) (coll *Collection, err error) {
err = c.client.Read(link, &coll)
if err != nil {
return nil, err
}
return
}
// Read document by self link
func (c *DocumentDB) ReadDocument(link string, doc interface{}) (err error) {
err = c.client.Read(link, &doc)
return
}
// Read sporc by self link
func (c *DocumentDB) ReadStoredProcedure(link string) (sproc *Sproc, err error) {
err = c.client.Read(link, &sproc)
if err != nil {
return nil, err
}
return
}
// Read udf by self link
func (c *DocumentDB) ReadUserDefinedFunction(link string) (udf *UDF, err error) {
err = c.client.Read(link, &udf)
if err != nil {
return nil, err
}
return
}
// Read all databases
func (c *DocumentDB) ReadDatabases() (dbs []Database, err error) {
return c.QueryDatabases("")
}
// Read all collections by db selflink
func (c *DocumentDB) ReadCollections(db string) (colls []Collection, err error) {
return c.QueryCollections(db, "")
}
// Read all sprocs by collection self link
func (c *DocumentDB) ReadStoredProcedures(coll string) (sprocs []Sproc, err error) {
return c.QueryStoredProcedures(coll, "")
}
// Read all udfs by collection self link
func (c *DocumentDB) ReadUserDefinedFunctions(coll string) (udfs []UDF, err error) {
return c.QueryUserDefinedFunctions(coll, "")
}
// Read all collection documents by self link
// TODO: use iterator for heavy transactions
func (c *DocumentDB) ReadDocuments(coll string, docs interface{}) (err error) {
return c.QueryDocuments(coll, "", docs)
}
// Read all databases that satisfy a query
func (c *DocumentDB) QueryDatabases(query string) (dbs []Database, err error) {
data := struct {
Databases []Database `json:"Databases,omitempty"`
Count int `json:"_count,omitempty"`
}{}
if len(query) > 0 {
err = c.client.Query("dbs", query, &data)
} else {
err = c.client.Read("dbs", &data)
}
if dbs = data.Databases; err != nil {
dbs = nil
}
return
}
// Read all db-collection that satisfy a query
func (c *DocumentDB) QueryCollections(db, query string) (colls []Collection, err error) {
data := struct {
Collections []Collection `json:"DocumentCollections,omitempty"`
Count int `json:"_count,omitempty"`
}{}
if len(query) > 0 {
err = c.client.Query(db + "colls/", query, &data)
} else {
err = c.client.Read(db + "colls/", &data)
}
if colls = data.Collections; err != nil {
colls = nil
}
return
}
// Read all collection `sprocs` that satisfy a query
func (c *DocumentDB) QueryStoredProcedures(coll, query string) (sprocs []Sproc, err error) {
data := struct {
Sprocs []Sproc `json:"StoredProcedures,omitempty"`
Count int `json:"_count,omitempty"`
}{}
if len(query) > 0 {
err = c.client.Query(coll + "sprocs/", query, &data)
} else {
err = c.client.Read(coll + "sprocs/", &data)
}
if sprocs = data.Sprocs; err != nil {
sprocs = nil
}
return
}
// Read all collection `udfs` that satisfy a query
func (c *DocumentDB) QueryUserDefinedFunctions(coll, query string) (udfs []UDF, err error) {
data := struct {
Udfs []UDF `json:"UserDefinedFunctions,omitempty"`
Count int `json:"_count,omitempty"`
}{}
if len(query) > 0 {
err = c.client.Query(coll + "udfs/", query, &data)
} else {
err = c.client.Read(coll + "udfs/", &data)
}
if udfs = data.Udfs; err != nil {
udfs = nil
}
return
}
// Read all documents in a collection that satisfy a query
func (c *DocumentDB) QueryDocuments(coll, query string, docs interface{}) (err error) {
data := struct {
Documents interface{} `json:"Documents,omitempty"`
Count int `json:"_count,omitempty"`
}{Documents: docs}
if len(query) > 0 {
err = c.client.Query(coll + "docs/", query, &data)
} else {
err = c.client.Read(coll + "docs/", &data)
}
return
}
// Create database
func (c *DocumentDB) CreateDatabase(body interface{}) (db *Database, err error) {
err = c.client.Create("dbs", body, &db)
if err != nil {
return nil, err
}
return
}
// Create collection
func (c *DocumentDB) CreateCollection(db string, body interface{}) (coll *Collection, err error) {
err = c.client.Create(db + "colls/", body, &coll)
if err != nil {
return nil, err
}
return
}
// Create stored procedure
func (c *DocumentDB) CreateStoredProcedure(coll string, body interface{}) (sproc *Sproc, err error) {
err = c.client.Create(coll + "sprocs/", body, &sproc)
if err != nil {
return nil, err
}
return
}
// Create user defined function
func (c *DocumentDB) CreateUserDefinedFunction(coll string, body interface{}) (udf *UDF, err error) {
err = c.client.Create(coll + "udfs/", body, &udf)
if err != nil {
return nil, err
}
return
}
// Create document
func (c *DocumentDB) CreateDocument(coll string, doc interface{}) error {
id := reflect.ValueOf(doc).Elem().FieldByName("Id")
if id.IsValid() && id.String() == "" {
id.SetString(uuid())
}
return c.client.Create(coll + "docs/", doc, &doc)
}
// TODO: DRY, but the sdk want that[mm.. maybe just client.Delete(self_link)]
// Delete database
func (c *DocumentDB) DeleteDatabase(link string) error {
return c.client.Delete(link)
}
// Delete collection
func (c *DocumentDB) DeleteCollection(link string) error {
return c.client.Delete(link)
}
// Delete collection
func (c *DocumentDB) DeleteDocument(link string) error {
return c.client.Delete(link)
}
// Delete stored procedure
func (c *DocumentDB) DeleteStoredProcedure(link string) error {
return c.client.Delete(link)
}
// Delete user defined function
func (c *DocumentDB) DeleteUserDefinedFunction(link string) error {
return c.client.Delete(link)
}
// Replace database
func (c *DocumentDB) ReplaceDatabase(link string, body interface{}) (db *Database, err error) {
err = c.client.Replace(link, body, &db)
if err != nil {
return nil, err
}
return
}
// Replace document
func (c *DocumentDB) ReplaceDocument(link string, doc interface{}) error {
return c.client.Replace(link, doc, &doc)
}
// Replace stored procedure
func (c *DocumentDB) ReplaceStoredProcedure(link string, body interface{}) (sproc *Sproc, err error) {
err = c.client.Replace(link, body, &sproc)
if err != nil {
return nil, err
}
return
}
// Replace stored procedure
func (c *DocumentDB) ReplaceUserDefinedFunction(link string, body interface{}) (udf *UDF, err error) {
err = c.client.Replace(link, body, &udf)
if err != nil {
return nil, err
}
return
}
// Execute stored procedure
func (c *DocumentDB) ExecuteStoredProcedure(link string, params, body interface{}) (err error) {
err = c.client.Execute(link, params, &body)
return
}