/
document.clj
255 lines (199 loc) · 14.5 KB
/
document.clj
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
(ns clarango.document
(:require [clarango.utilities.http-utility :as http])
(:use [clarango.utilities.core-utility :only [remove-options-map filter-out-options-map filter-out-collection-name filter-out-database-name]]
[clarango.utilities.uri-utility :only [build-resource-uri]]))
(defn get-by-key
"Gets a document by its key.
Takes the document key as first argument.
Takes optional a collection name and a db name as further arguments.
If omitted by user, the default db and collection will be used.
Also optional as argument is another map containing further options:
{'rev' revision_id} (replace the single quotes with double quotes)
- rev is the document revision; if the current document revision_id does not match the given one, an error is thrown
The option map might be passed in an arbitrary position after the first two arguments."
[& args]
(http/get-uri [:body] (apply build-resource-uri "document" (remove-options-map args)) (filter-out-options-map args)))
(defn get-by-keys
"Gets a number of documents in a single request.
Takes a vector of keys as first argument.
Optionally takes a collection name and a db as further arguments.
If omitted the default db and collection will be used."
[keys & args]
{:pre [(or (vector? keys) (string? keys))]}
(http/put-uri [:body "documents"] (build-resource-uri "simple/lookup-by-keys" nil nil (filter-out-database-name args)) {:collection (filter-out-collection-name args) :keys (if (string? keys) (vec keys) (vec (map #(if (string? %) (name %) %) keys)))}))
(defn get-by-example
"Gets a document or a number of documents out of a collection by giving an example to match.
Takes the example as a map as first argument.
Takes optional a collection name and a db name as further arguments.
If omitted by user, the default db and collection will be used.
Also optional as argument is another map containing further options:
{'skip' skip, 'limit' limit} (replace the single quotes with double quotes)
- skip meaning the (number of?) documents to skip in the result
- limit meaning the maximum amount of documents to return
The option map might be passed in an arbitrary position after the first two arguments."
[example & args] ; what happens here if there is a db explicitely passed to this method? Do we nee a filterout-db-name-from-args too?
{:pre [(map? example)]}
(http/put-uri [:body "result"] (build-resource-uri "simple/by-example" nil nil (filter-out-database-name args)) (merge {:example example :collection (filter-out-collection-name args)} (filter-out-options-map args))))
(defn get-first-by-example
"Gets the first document out of a collection that matches an example.
Takes the example as a map as first argument.
Takes optional a collection name and a db name as further arguments.
If omitted by user, the default db and collection will be used."
[example & args] ; what happens here if there is a db explicitely passed to this method? Do we nee a filterout-db-name-from-args too?
{:pre [(map? example)]}
(http/put-uri [:body "document"] (build-resource-uri "simple/first-example" nil nil (filter-out-database-name args)) {:example example :collection (filter-out-collection-name args)}))
(defn get-info
"Gets information about a document by its key.
Takes the document key as first argument.
Takes optional a collection name and a db name as further arguments.
If omitted by user, the default db and collection will be used.
Also optional as argument is another map containing further options:
{'rev' revision_id, 'policy' 'error/last'} (replace the single quotes with double quotes)
- rev is the document revision
- policy meaning the desired behaviour in case the given revision number does not match the latest document revision
-> 'error' meaning that an error is thrown if the given revision_id does not match the revision_id in the document
-> 'last' meaning the document is still returned even if the given revision_id does not match the revision_id in the document
The option map might be passed in an arbitrary position after the first two arguments."
[& args]
(http/head-uri [:headers] (apply build-resource-uri "document" (remove-options-map args)) (filter-out-options-map args)))
(defn create
"Creates a document.
First argument: A map that represents the document.
If you want to specify a key by yourself, add it as the :_key parameter to the document map or use method create-with-key.
If you would like the key to be created automatically, just leave this parameter out.
Takes optional a collection name and a db name as further arguments.
If omitted by user, the default db and collection will be used.
Also optional as argument is another map containing further options:
{'createCollection' true/false, 'waitForSync' true/false} (replace the single quotes with double quotes)
- createCollection meaning if the collection should be created if it does not exist yet;
- waitForSync meaning if the server response should wait until the document is saved to disk;
The option map might be passed in an arbitrary position after the first argument."
[document & args]
{:pre [(map? document)]}
(http/post-uri [:body] (apply build-resource-uri "document/?collection=" nil (remove-options-map args)) document (filter-out-options-map args)))
(defn create-with-key
"Creates a document with a given key.
First argument: A map that represents the document.
Second argument: The key for the new document (string or clojure keyword).
Takes optional a collection name and a db name as further arguments.
If omitted by user, the default db and collection will be used.
Also optional as argument is another map containing further options:
{'createCollection' true/false, 'waitForSync' true/false} (replace the single quotes with double quotes)
- createCollection meaning if the collection should be created if it does not exist yet;
- waitForSync meaning if the server response should wait until the document is saved to disk;
The option map might be passed in an arbitrary position after the first argument."
[document key & args]
{:pre [(map? document) (or (keyword? key) (string? key))]}
(http/post-uri [:body] (apply build-resource-uri "document/?collection=" nil (remove-options-map args)) (assoc document :_key key) (filter-out-options-map args)))
(defn- create-multi
"THIS METHOD DOES NOT WORK YET!
Creates multiple documents at a time.
First argument is a vector of documents.
Takes optional a collection name and a db name as further arguments.
If omitted by user, the default db and collection will be used."
[documents & args]
{:pre [(vector? documents)]}
;; what about the document key if the user desires to specify it by himself?
;; Should he just pass it in the json document? or allow it as optional argument?
(http/post-multi-uri [:body] (build-resource-uri "batch") documents (filter-out-collection-name args) (filter-out-database-name args)))
(defn replace-by-key
"Replaces a document with a map representing the new document.
First argument: A map representing the new document.
Second argument: The document key.
Takes optional a collection name and a db name as further arguments.
If omitted by user, the default db and collection will be used.
Also optional as argument is another map containing further options:
{'waitForSync' true/false, 'rev' revision_id, 'policy' 'error/last'} (replace the single quotes with double quotes)
- waitForSync meaning if the server response should wait until the document is saved to disk
- rev is the document revision
- policy meanins the desired behaviour in case the given revision number does not match the latest document revision
-> 'error' meaning that an error is thrown if the given revision_id does not match the revision_id in the document
-> 'last' meaning the document is still replaced even if the given revision_id does not match the revision_id in the document
The option map might be passed in an arbitrary position after the first two arguments."
[new-document & args]
{:pre [(map? new-document)]}
(http/put-uri [:body] (apply build-resource-uri "document" (remove-options-map args)) new-document (filter-out-options-map args)))
(defn replace-by-example
"Replaces a document or a number of documents out of a collection by giving an example to match.
First argument: A map representing the new document.
Second argument: The example map.
Takes optional a collection name and a db name as further arguments.
If omitted by user, the default db and collection will be used.
Also optional as argument is another map containing further options:
{'waitForSync' true/false, 'limit' limit} (replace the single quotes with double quotes)
- waitForSync meaning if the server response should wait until the document is saved to disk
- limit meaning the maximum amount of documents that will be replaced
The option map might be passed in an arbitrary position after the first two arguments."
[new-document example & args]
{:pre [(map? new-document) (map? example)]}
(http/put-uri [:body] (build-resource-uri "simple/replace-by-example" nil nil (filter-out-database-name args)) (merge {:example example :newValue new-document :collection (filter-out-collection-name args)} (filter-out-options-map args))))
(defn update-by-key
"Updates a document with a number of key value pairs. Inserts them into the existing document.
First argument: A map containing the new key/value pairs.
Second argument: The document key.
Takes optional a collection name and a db name as further arguments.
If omitted by user, the default db and collection will be used.
Also optional as argument is another map containing further options:
{'waitForSync' true/false, 'keepNull' true/false, 'rev' revision_id, 'policy' 'error/last'} (replace the single quotes with double quotes)
- waitForSync meaning if the server response should wait until the document is saved to disk;
- keepNull meaning if the key/value pair should be deleted in the document
if the argument map contains it with a null as value;
- rev is the document revision
- policy meanins the desired behaviour in case the given revision number does not match the latest document revision
-> 'error' meaning that an error is thrown if the given revision_id does not match the revision_id in the document
-> 'last' meaning the document is still updated even if the given revision_id does not match the revision_id in the document
The option map might be passed in an arbitrary position after the first two arguments."
[document-properties & args]
{:pre [(map? document-properties)]}
(http/patch-uri [:body] (apply build-resource-uri "document" (remove-options-map args)) document-properties (filter-out-options-map args)))
(defn update-by-example
"Updates a document or a number of documents out of a collection by giving an example to match.
First argument: A map containing the new key/value pairs.
Second argument: The example map.
Takes optional a collection name and a db name as further arguments.
If omitted by user, the default db and collection will be used.
Also optional as argument is another map containing further options:
{'waitForSync' true/false, 'limit' limit, 'keepNull' true/false} (replace the single quotes with double quotes)
- waitForSync meaning if the server response should wait until the document is saved to disk
- limit meaning the maximum amount of documents that will be updated
- keepNull meaning if the key/value pair should be deleted in the document
The option map might be passed in an arbitrary position after the first two arguments."
[document-properties example & args]
{:pre [(map? document-properties) (map? example)]}
(http/put-uri [:body] (build-resource-uri "simple/update-by-example" nil nil (filter-out-database-name args)) (merge {:example example :newValue document-properties :collection (filter-out-collection-name args)} (filter-out-options-map args))))
(defn delete-by-key
"Deletes a document by its id.
Takes the document key as first argument.
Takes optional a collection name and a db name as further arguments.
If omitted by user, the default db and collection will be used.
Also optional as argument is another map containing further options:
{'waitForSync' true/false, 'rev' revision_id, 'policy' 'error/last'} (replace the single quotes with double quotes)
- waitForSync meaning if the server response should wait until the document is saved to disk;
- rev is the document revision
- policy meanins the desired behaviour in case the given revision number does not match the latest document revision
-> 'error' meaning that an error is thrown if the given revision_id does not match the revision_id in the document
-> 'last' meaning the document is still deleted even if the given revision_id does not match the revision_id in the document
The option map might be passed in an arbitrary position after the first argument."
[& args]
(http/delete-uri [:body] (apply build-resource-uri "document" (remove-options-map args)) (filter-out-options-map args)))
(defn delete-by-example
"Deletes a document or a number of documents out of a collection by giving an example to match.
Takes the example as a map as first argument.
Takes optional a collection name and a db name as further arguments.
If omitted by user, the default db and collection will be used.
Also optional as argument is another map containing further options:
{'waitForSync' true/false, 'limit' limit} (replace the single quotes with double quotes)
- waitForSync meaning if the server response should wait until the document is saved to disk
- limit meaning the maximum amount of documents that will be deleted
The option map might be passed in an arbitrary position after the first two arguments."
[example & args]
{:pre [(map? example)]}
(http/put-uri [:body] (build-resource-uri "simple/remove-by-example" nil nil (filter-out-database-name args)) (merge {:example example :collection (filter-out-collection-name args)} (filter-out-options-map args))))
(defn delete-by-keys
"Deletes a number of documents in a single request.
Takes a vector of keys to delete as first argument.
Optionally takes a collection name and a db as further arguments.
If omitted the default db and collection will be used."
[keys & args]
{:pre [(or (vector? keys) (string? keys))]}
(http/put-uri [:body] (build-resource-uri "simple/remove-by-keys" nil nil (filter-out-database-name args)) {:collection (filter-out-collection-name args) :keys (if (string? keys) (vec keys) (vec (map #(if (string? %) (name %) %) keys)))}))