-
Notifications
You must be signed in to change notification settings - Fork 18
/
77-cloudant-cf.html
332 lines (310 loc) · 12.8 KB
/
77-cloudant-cf.html
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
<!--
Copyright 2014,2016 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script type="text/x-red" data-template-name="cloudant">
<div class="form-row">
<label for="node-config-input-host"><i class="fa fa-bookmark"></i> Host</label>
<input class="input-append-left" type="text" id="node-config-input-host" placeholder="[username].cloudant.com or http://your-server.com">
</div>
<div class="form-row">
<label for="node-config-input-username"><i class="fa fa-user"></i> Username</label>
<input type="text" id="node-config-input-username">
</div>
<div class="form-row">
<label for="node-config-input-pass"><i class="fa fa-lock"></i> Password</label>
<input type="password" id="node-config-input-pass">
</div>
<div class="form-row">
<label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-config-input-name" placeholder="Name">
</div>
</script>
<script type="text/javascript">
RED.nodes.registerType("cloudant",{
category: "config",
color: "rgb(114, 199, 231)",
defaults: {
host: {value:"", required:true},
name: {value:""}
},
credentials: {
username: {type:"text"},
pass: {type:"password"}
},
label: function() {
return this.name || this.host;
}
});
</script>
<script type="text/x-red" data-template-name="cloudant out">
<div class="form-row">
<label for="node-input-service"><i class="fa fa-folder-close"></i> Service</label>
<select id="node-input-service" style="width:68%;">
<!-- <option value="" disabled></option> -->
<option value="_ext_"> External cloudant or couchdb service</option>
</select>
</div>
<div class="form-row hide" id="node-input-external-details">
<label for="node-input-cloudant"><i class="fa fa-bookmark"></i> Server</label>
<input type="text" id="node-input-cloudant">
</div>
<div class="form-row">
<label for="node-input-database"><i class="fa fa-briefcase"></i> Database</label>
<input type="text" id="node-input-database" placeholder="database" style="width:65%;">
</div>
</div>
<div class="form-row">
<label for="node-input-operation"><i class="fa fa-wrench"></i> Operation</label>
<select type="text" id="node-input-operation" style="width:68%;">
<option value="insert">insert</option>
<option value="delete">remove</option>
</select>
</div>
<div class="form-row node-input-payonly">
<label> </label>
<input type="checkbox" id="node-input-payonly" placeholder="Only"
style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-payonly" style="width:65%;">Only store msg.payload object?</label>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name" style="width:65%;">
</div>
<script>
$("#node-input-operation").change(function() {
var id = $("#node-input-operation option:selected").val();
if (id == "delete") $(".node-input-payonly").hide();
else $(".node-input-payonly").show();
});
</script>
</script>
<script type="text/x-red" data-template-name="cloudant in">
<div class="form-row">
<label for="node-input-service"><i class="fa fa-folder-close"></i> Service</label>
<select id="node-input-service" style="width:68%;">
<!-- <option value="" disabled></option> -->
<option value="_ext_"> External cloudant or couchdb service</option>
</select>
</div>
<div class="form-row hide" id="node-input-external-details">
<label for="node-input-cloudant"><i class="fa fa-bookmark"></i> Server</label>
<input type="text" id="node-input-cloudant">
</div>
<div class="form-row">
<label for="node-input-database"><i class="fa fa-briefcase"></i> Database</label>
<input type="text" id="node-input-database" placeholder="database" style="width:65%;">
</div>
<div class="form-row">
<label for="node-input-search"><i class="fa fa-search"></i> Search by</label>
<select id="node-input-search" style="width:68%;">
<option value="_id_">_id</option>
<option value="_idx_">search index</option>
<option value="_all_">all documents</option>
</select>
</div>
<div id="node-search-index-form" class="form-row">
<label> </label>
<input type="text" id="node-input-design" style="width:30%" placeholder="design document">
/
<input type="text" id="node-input-index" style="width:30%" placeholder="index name">
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name" style="width:65%;">
</div>
</script>
<script type="text/javascript">
RED.nodes.registerType("cloudant out", {
category: "storage-output",
color: "rgb(114, 199, 231)",
defaults: {
name: { value: "" },
cloudant: { type:"cloudant", validate:validateServer},
database: { value:"", required:true, validate:validateDatabase },
service: { value:"", required:true },
payonly: { value:false },
operation: { value:"insert" }
},
inputs: 1,
outputs: 0,
icon: "cloudant.png",
align: "right",
label: label,
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: oneditprepare
});
RED.nodes.registerType("cloudant in", {
category: "storage-input",
color: "rgb(114, 199, 231)",
defaults: {
name : { value:"" },
cloudant: { type:"cloudant", validate:validateServer },
database: { value:"", required:true, validate:validateDatabase },
service : { value:"", required:true },
search : { value:"_id_", required:true },
design : { value:"" },
index : { value:"" }
},
inputs : 1,
outputs: 1,
icon : "cloudant.png",
label : label,
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: oneditprepare
});
function oneditprepare() {
var node = this;
var serviceSelect = $('#node-input-service');
var searchBySelect = $('#node-input-search');
var searchForm = $('#node-search-index-form');
$.getJSON('cloudant/vcap/',function(data) {
var last = serviceSelect.children().last();
var opts = [];
for (var i=0; i < data.length; i++) {
var selected = node.service == data[i].name;
opts.push('<option value="' + data[i].name + '"' + (selected ? " selected":"") + '>' + data[i].name + '</option>');
}
if (opts.length == 0) {
node.service = "_ext_";
serviceSelect.find("option").filter(function() {
return $(this).val() == node.service;
}).attr('selected', true);
} else {
last.before(opts.join(""));
}
serviceSelect.change();
});
serviceSelect.change(function() {
var service = serviceSelect.val();
if (service == "_ext_") {
$("#node-input-external-details").show();
} else {
$("#node-input-external-details").hide();
}
});
searchBySelect.change(function() {
var searchBy = searchBySelect.val();
if (searchBy === "_idx_") {
node._def.defaults.design.required = true;
node._def.defaults.index.required = true;
searchForm.show();
} else {
node._def.defaults.design.required = false;
node._def.defaults.index.required = false;
searchForm.hide();
}
});
}
function label() {
return this.name || this.database || "cloudant";
}
function validateServer(v) {
return this.service != "_ext_" || v != "_ADD_";
}
// https://wiki.apache.org/couchdb/HTTP_database_API#Naming_and_Addressing
function validateDatabase(v) {
return (v.indexOf('_') !== 0) && (v.search(/[A-Z\s\\/]/g) < 0)
}
</script>
<script type="text/x-red" data-help-name="cloudant out">
<p>
A simple Cloudant output node. Stores <b>msg</b> in a chosen database.
</p>
<p>
Cloudant generates a random id for the <code>_id</code> property if one
is not specified and a new document will be <b>inserted</b> each time.
You can <b>insert</b> only the <code>msg.payload</code> by checking the
checkbox in the configuration window.
</p>
<p>
If you want to <b>update</b> an existing document you must specify the
latest values for <code>_id</code> and <code>_rev</code> in your object.
If they don't match the current values stored in the database you will
receive a <b>conflict error</b> message, so make sure you are using the
most updated information for the document.
</p>
<p>
It is also possible to <b>delete</b> documents from the database by
providing values for <code>_id</code> and <code>_rev</code> and selecting
the <b>remove</b> option for the node. You can pass these values in the
<code>msg</code> object itself or as an object in the
<code>msg.payload</code>.
</p>
<p>
The <b>database name</b> must follow these rules:
<ul>
<li>No spaces</li>
<li>All letters in small caps</li>
<li>The first character can't be <code>_</code></li>
</ul>
</p>
<p>
Your document should avoid having top-level fields that start with
<code>_</code>, with exceptions for <code>_id</code>, <code>_rev</code>
and other <a href="https://wiki.apache.org/couchdb/HTTP_Document_API#Special_Fields">
CouchDB reserved words</a>.
</p>
</script>
<script type="text/x-red" data-help-name="cloudant in">
<p>
A node for searching documents in a Cloudant database.
</p>
<p>
Searching for documents can be done in three modes: directly by using the
document's <b>_id</b>, by using an existing <a
href="https://cloudant.com/for-developers/search/" target="_blank">Search
Index</a> or retrieving <b>all documents</b> stored in your database.
</p>
<p>
When querying using the <b>_id</b> option, the value for the document's
<code>_id</code> should be passed in the <code>msg.payload</code> as a
string.
</p>
<p>
To use an existing <b>Search Index</b> stored on the desired database,
the query argument should be passed on the <code>msg.payload</code> as a
string following the <code>indexName:value</code> pattern. Keep in mind
that <i>the index must be created beforehand in the database.</i> and
referenced here by its <code>design document/index name</code>.
</p>
<p>
When querying using a <b>Search Index</b> you can pass the search
parameters as an object in <code>msg.payload</code>.
</p>
<p>
For example, you can pass an object like this:
<p>
<code>{ query: "abc*", limit: 100 }</code>
</p>
<p>
to change the value of <code>limit</code>. You can find more information
about <b>Search Index</b> parameters in the <a
href="https://docs.cloudant.com/api.html?http#queries" target="_blank">
official Cloudant documentation</a>.
</p>
<p>
The last method to retrieve documents is to simply get all of them by
selecting the option <b>all documents</b>.
</p>
<p>
The <b>database name</b> must follow these rules:
<ul>
<li>No spaces</li>
<li>All letters in small caps</li>
<li>The first character can't be <code>_</code></li>
</ul>
</p>
</script>