-
Notifications
You must be signed in to change notification settings - Fork 1
/
cosmosdb.go
411 lines (367 loc) · 15.6 KB
/
cosmosdb.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//
// This library started as a fork of `github.com/nerdylikeme/go-documentdb`
//
package gocosmosdb
import (
"errors"
"reflect"
"time"
"github.com/intwinelabs/logger"
)
// Config - Stores configuration for the gocosmosdb client
type Config struct {
MasterKey string
Debug bool
Verbose bool
PartitionKeyStructField string // eg. "Id"
PartitionKeyPath string // slash denoted path eg. "/id"
RetryWaitMin time.Duration
RetryWaitMax time.Duration
RetryMax int
Pooled bool
}
// CosmosDB - Struct that stores the client and logger
type CosmosDB struct {
client *apiClient
Config Config
Logger *logger.Logger
}
// New - Creates CosmosDB Client and returns it
func New(url string, config Config, log *logger.Logger) *CosmosDB {
client := newAPIClient(&config)
client.uri = url
client.config = config
client.logger = log
return &CosmosDB{client, config, log}
}
// GetURI - returns the CosmosDB URI
func (c *CosmosDB) GetURI() string {
return c.client.getURI()
}
// GetConfig - returns the CosmosDB config
func (c *CosmosDB) GetConfig() Config {
return c.client.getConfig()
}
// EnableDebug - enables the CosmosDB debugging
func (c *CosmosDB) EnableDebug() {
c.client.enableDebug()
}
// DisableDebug - disables the CosmosDB debugging
func (c *CosmosDB) DisableDebug() {
c.client.disableDebug()
}
// ReadDatabase - Retrieves a database resource by performing a GET on the database resource.
// db, err := client.ReadDatabase("dbs/{db-id}")
func (c *CosmosDB) ReadDatabase(link string, opts ...CallOption) (db *Database, err error) {
_, err = c.client.read(link, &db, opts...)
if err != nil {
return nil, err
}
return
}
// ReadCollection - Retrieves a collection by performing a GET on a specific collection resource.
// coll, err := client.ReadCollection("dbs/{db-id}/colls/{coll-id}")
func (c *CosmosDB) ReadCollection(link string, opts ...CallOption) (coll *Collection, err error) {
_, err = c.client.read(link, &coll, opts...)
if err != nil {
return nil, err
}
return
}
// ReadDocument - Retrieves a document by performing a GET on a specific document resource and marshals the document into that passed docStruct
// err = client.ReadDocument("dbs/{db-id}/colls/{coll-id}/docs/{doc-id}", &docStruct)
func (c *CosmosDB) ReadDocument(link string, doc interface{}, opts ...CallOption) (resp *Response, err error) {
resp, err = c.client.read(link, &doc, opts...)
return
}
// ReadStoredProcedure - Retrieves a stored procedure by performing a GET on a specific stored procedure resource.
// sproc, err := client.ReadStoredProcedure("dbs/{db-id}/sprocs/{sproc-id}")
func (c *CosmosDB) ReadStoredProcedure(link string, opts ...CallOption) (sproc *Sproc, err error) {
_, err = c.client.read(link, &sproc, opts...)
if err != nil {
return nil, err
}
return
}
// ReadUserDefinedFunction - Retrieves a user defined function by performing a GET on a specific user defined function resource.
// udf, err := client.ReadUserDefinedFunction("dbs/{db-id}/udfs/{udf-id}")
func (c *CosmosDB) ReadUserDefinedFunction(link string, opts ...CallOption) (udf *UDF, err error) {
_, err = c.client.read(link, &udf, opts...)
if err != nil {
return nil, err
}
return
}
// ReadDatabases - Retrieves all databases by performing a GET on a specific account.
// dbs, err := client.ReadDatabases("dbs")
func (c *CosmosDB) ReadDatabases(opts ...CallOption) (dbs []Database, err error) {
return c.QueryDatabases("", opts...)
}
// ReadCollections - Retrieves all collections by performing a GET on a specific database.
// colls, err := client.ReadCollections("dbs/{db-id}/colls")
func (c *CosmosDB) ReadCollections(db string, opts ...CallOption) (colls []Collection, err error) {
return c.QueryCollections(db, "", opts...)
}
// ReadStoredProcedures - Retrieves all stored procedures by performing a GET on a specific database.
// sprocs, err := client.ReadStoredProcedures("dbs/{db-id}/sprocs")
func (c *CosmosDB) ReadStoredProcedures(coll string, opts ...CallOption) (sprocs []Sproc, err error) {
return c.QueryStoredProcedures(coll, "", opts...)
}
// ReadUserDefinedFunctions - Retrieves all user defined functions by performing a GET on a specific database.
// udfs, err := client.ReadUserDefinedFunctions("dbs/{db-id}/udfs")
func (c *CosmosDB) ReadUserDefinedFunctions(coll string, opts ...CallOption) (udfs []UDF, err error) {
return c.QueryUserDefinedFunctions(coll, "", opts...)
}
// ReadDocuments - Retrieves a stored procedure by performing a GET on a specific stored procedure resource.
// err = client.ReadDocuments("dbs/{db-id}/colls/{coll-id}/docs", &docStructSlice)
func (c *CosmosDB) ReadDocuments(coll string, docs interface{}, opts ...CallOption) (*Response, error) {
return c.QueryDocuments(coll, "", docs, opts...)
}
// QueryDatabases - Retrieves all databases that satisfy the passed query.
// dbs, err := client.QueryDatabases("SELECT * FROM ROOT r")
func (c *CosmosDB) QueryDatabases(query string, opts ...CallOption) (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, opts...)
} else {
_, err = c.client.read("dbs", &data, opts...)
}
if dbs = data.Databases; err != nil {
dbs = nil
}
return
}
// QueryCollections - Retrieves all collections that satisfy passed query.
// colls, err := client.QueryCollections("SELECT * FROM ROOT r")
func (c *CosmosDB) QueryCollections(db, query string, opts ...CallOption) (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, opts...)
} else {
_, err = c.client.read(db+"colls/", &data, opts...)
}
if colls = data.Collections; err != nil {
colls = nil
}
return
}
// QueryStoredProcedures - Retrieves all stored procedures that satisfy the passed query.
// colls, err := client.QueryStoredProcedures("SELECT * FROM ROOT r")
func (c *CosmosDB) QueryStoredProcedures(coll, query string, opts ...CallOption) (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, opts...)
} else {
_, err = c.client.read(coll+"sprocs/", &data, opts...)
}
if sprocs = data.Sprocs; err != nil {
sprocs = nil
}
return
}
// QueryUserDefinedFunctions - Retrieves all user defined functions that satisfy the passed query.
// colls, err := client.QueryUserDefinedFunctions("SELECT * FROM ROOT r")
func (c *CosmosDB) QueryUserDefinedFunctions(coll, query string, opts ...CallOption) (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, opts...)
} else {
_, err = c.client.read(coll+"udfs/", &data, opts...)
}
if udfs = data.Udfs; err != nil {
udfs = nil
}
return
}
// QueryDocuments - Retrieves all documents in a collection that satisfy the passed query and marshals them into the passed interface.
// err := client.QueryDocuments(coll, "SELECT * FROM ROOT r", &docs)
func (c *CosmosDB) QueryDocuments(coll, query string, docs interface{}, opts ...CallOption) (resp *Response, err error) {
data := struct {
Documents interface{} `json:"Documents,omitempty"`
Count int `json:"_count,omitempty"`
}{Documents: docs}
if len(query) > 0 {
resp, err = c.client.query(coll+"docs/", query, &data, opts...)
} else {
resp, err = c.client.read(coll+"docs/", &data)
}
return
}
// QueryDocumentsWithParameters - Retrieves all documents in a collection that satisfy a passed query with parameters and marshals them into the passed interface.
// err := client.QueryDocumentsWithParameters(coll, queryWithParams, &docs)
func (c *CosmosDB) QueryDocumentsWithParameters(coll string, query *QueryWithParameters, docs interface{}, opts ...CallOption) (resp *Response, err error) {
data := struct {
Documents interface{} `json:"Documents,omitempty"`
Count int `json:"_count,omitempty"`
}{Documents: docs}
if query != nil {
resp, err = c.client.queryWithParameters(coll+"docs/", query, &data, opts...)
} else {
err = errors.New("QueryWithParameters cannot be nil")
}
return
}
// QueryPartitionKeyRanges - Retrieves all partition ranges in a collection.
// pks, err := client.QueryPartitionKeyRanges(coll, "SELECT * FROM ROOT r")
func (c *CosmosDB) QueryPartitionKeyRanges(coll string, query string, opts ...CallOption) (ranges []PartitionKeyRange, err error) {
data := struct {
PartitionKeyRanges []PartitionKeyRange `json:"PartitionKeyRanges,omitempty"`
Count int `json:"_count,omitempty"`
}{}
if len(query) > 0 {
_, err = c.client.query(coll+"pkranges/", query, &data, opts...)
} else {
_, err = c.client.read(coll+"pkranges/", &data, opts...)
}
if ranges = data.PartitionKeyRanges; err != nil {
ranges = nil
}
return
}
// CreateDatabase - Creates a new database in the database account.
// db, err := client.CreateDatabase(`{ "id": "db-id" }`)
func (c *CosmosDB) CreateDatabase(body interface{}, opts ...CallOption) (db *Database, err error) {
_, err = c.client.create("dbs", body, &db, opts...)
if err != nil {
return nil, err
}
return
}
// CreateCollection - Creates a new collections in the database.
// coll, err := client.CreateCollection("dbs/{db-id}/", `{"id": "coll-id"}`)
func (c *CosmosDB) CreateCollection(db string, body interface{}, opts ...CallOption) (coll *Collection, err error) {
_, err = c.client.create(db+"colls/", body, &coll, opts...)
if err != nil {
return nil, err
}
return
}
// CreateStoredProcedure - Creates a new stored procedure in the collection.
// sprocBody := gocosmosdb.Sproc{
// Body: "function () {\r\n var context = getContext();\r\n var response = context.getResponse();\r\n\r\n response.setBody(\"Hello, World\");\r\n}",
// Id: "sproc_1"
// }
// sproc, err := client.CreateStoredProcedure("dbs/{db-id}/colls/{coll-id}/sprocs", &sprocBody)
func (c *CosmosDB) CreateStoredProcedure(coll string, body interface{}, opts ...CallOption) (sproc *Sproc, err error) {
_, err = c.client.create(coll+"sprocs/", body, &sproc, opts...)
if err != nil {
return nil, err
}
return
}
// CreateUserDefinedFunction - Creates a new user defined function in the collection.
// udfBody := gocosmosdb.UDF{
// Body: "function tax(income) {\r\n if(income == undefined) \r\n throw 'no input';\r\n if (income < 1000) \r\n return income * 0.1;\r\n else if (income < 10000) \r\n return income * 0.2;\r\n else\r\n return income * 0.4;\r\n}",
// Id: "simpleTaxUDF"
// }
// udf, err := client.CreateUserDefinedFunction("dbs/{db-id}/colls/{coll-id}/udfs", &udfBody)
func (c *CosmosDB) CreateUserDefinedFunction(coll string, body interface{}, opts ...CallOption) (udf *UDF, err error) {
_, err = c.client.create(coll+"udfs/", body, &udf, opts...)
if err != nil {
return nil, err
}
return
}
// CreateDocument - Creates a new document in the collection.
// err := client.CreateDocument("dbs/{db-id}/colls/{coll-id}", &doc)
func (c *CosmosDB) CreateDocument(coll string, doc interface{}, opts ...CallOption) (*Response, error) {
id := reflect.ValueOf(doc).Elem().FieldByName("Id")
if id.IsValid() && id.CanSet() && id.String() == "" {
id.SetString(genId())
}
if c.Config.PartitionKeyStructField != "" {
partKey := reflect.ValueOf(doc).Elem().FieldByName(c.Config.PartitionKeyStructField)
partKeyI := partKey.Interface()
opts = append(opts, PartitionKey(partKeyI))
}
return c.client.create(coll+"docs/", doc, &doc, opts...)
}
// UpsertDocument - Creates a new document or replaces the existing document with matching id in the collection.
// err := client.UpsertDocument("dbs/{db-id}/colls/{coll-id}", &doc)
func (c *CosmosDB) UpsertDocument(coll string, doc interface{}, opts ...CallOption) (*Response, error) {
id := reflect.ValueOf(doc).Elem().FieldByName("Id")
if id.IsValid() && id.CanSet() && id.String() == "" {
id.SetString(genId())
}
return c.client.upsert(coll+"docs/", doc, &doc, opts...)
}
// DeleteDatabase - Deletes a database from a database account.
// err := client.DeleteDatabase("dbs/{db-id}")
func (c *CosmosDB) DeleteDatabase(link string) (*Response, error) {
return c.client.delete(link)
}
// DeleteCollection - Deletes a collection from a database.
// err := client.DeleteCollection("dbs/{db-id}/colls/{coll-id}")
func (c *CosmosDB) DeleteCollection(link string) (*Response, error) {
return c.client.delete(link)
}
// DeleteDocument - Deletes a document from a collection.
// err := client.DeleteDocument("dbs/{db-id}/colls/{coll-id}/docs/{doc-id}")
func (c *CosmosDB) DeleteDocument(link string, opts ...CallOption) (*Response, error) {
return c.client.delete(link, opts...)
}
// DeleteStoredProcedure - Deletes a stored procedure from a collection.
// err := client.DeleteStoredProcedure("dbs/{db-id}/colls/{coll-id}/sprocs/{sproc-id}")
func (c *CosmosDB) DeleteStoredProcedure(link string) (*Response, error) {
return c.client.delete(link)
}
// DeleteUserDefinedFunction - Deletes a user defined function from a collection.
// err := client.DeleteUserDefinedFunction("dbs/{db-id}/colls/{coll-id}/udfs/{udf-id}")
func (c *CosmosDB) DeleteUserDefinedFunction(link string) (*Response, error) {
return c.client.delete(link)
}
// ReplaceDatabase - Replaces a existing database in a database account.
// db, err := client.ReplaceDatabase("dbs/{db-id}", "`{ "id": "new-db-id" }`)
func (c *CosmosDB) ReplaceDatabase(link string, body interface{}, opts ...CallOption) (db *Database, err error) {
_, err = c.client.replace(link, body, &db, opts...)
if err != nil {
return nil, err
}
return
}
// ReplaceDocument - Replaces a existing document in a collection.
// db, err := client.ReplaceDocument("dbs/{db-id}/colls/{coll-id}/docs/{doc-id}", &doc)
func (c *CosmosDB) ReplaceDocument(link string, doc interface{}, opts ...CallOption) (*Response, error) {
return c.client.replace(link, doc, &doc, opts...)
}
// ReplaceDocumentAsync - Replaces a document that has a matching etag.
// db, err := client.ReplaceDocumentAsync("dbs/{db-id}/colls/{coll-id}/docs/{doc-id}", &doc)
func (c *CosmosDB) ReplaceDocumentAsync(link string, doc interface{}, opts ...CallOption) (*Response, error) {
return c.client.replaceAsync(link, doc, &doc, opts...)
}
// ReplaceStoredProcedure - Replaces a stored procedure in a collection.
// db, err := client.ReplaceDatabase("dbs/{db-id}/colls/{coll-id}/sprocs/{sproc-id}", &sprocBody)
func (c *CosmosDB) ReplaceStoredProcedure(link string, body interface{}, opts ...CallOption) (sproc *Sproc, err error) {
_, err = c.client.replace(link, body, &sproc, opts...)
if err != nil {
return nil, err
}
return
}
// ReplaceUserDefinedFunction - Replaces a user defined function in a collection.
// db, err := client.ReplaceDatabase("dbs/{db-id}/colls/{coll-id}/udfs/{udf-id}", &udfBody)
func (c *CosmosDB) ReplaceUserDefinedFunction(link string, body interface{}, opts ...CallOption) (udf *UDF, err error) {
_, err = c.client.replace(link, body, &udf, opts...)
if err != nil {
return nil, err
}
return
}
// ExecuteStoredProcedure - Executes a stored procedure and marshals the data into the passed interface.
// err := client.ExecuteStoredProcedure("dbs/{db-id}/colls/{coll-id}/sprocs/{sproc-id}", []interface{}{p1, p2}, &docs)
func (c *CosmosDB) ExecuteStoredProcedure(link string, params, body interface{}, opts ...CallOption) (resp *Response, err error) {
resp, err = c.client.execute(link, params, &body, opts...)
return
}