Skip to content

Commit ba0ac80

Browse files
committed
Merge branch 'develop'
* develop: update labels for search bump npm version and update README update help text for in node add query by document id to in node cleanning code sending query result via payload
2 parents 462cc44 + 8ec3ae7 commit ba0ac80

File tree

4 files changed

+61
-26
lines changed

4 files changed

+61
-26
lines changed

77-cloudant-cf.html

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,15 @@
157157
</div>
158158

159159
<div class="form-row">
160-
<label for="node-input-design-doc"><i class="fa fa-search"></i> Search Idx.</label>
160+
<label for="node-input-search"><i class="fa fa-search"></i> Search by</label>
161+
<select id="node-input-search">
162+
<option value="_id_">_id</option>
163+
<option value="_idx_">search index</option>
164+
</select>
165+
</div>
166+
167+
<div id="node-search-index-form" class="form-row">
168+
<label>&nbsp;</label>
161169
<input type="text" id="node-input-design" style="width: 30%" placeholder="design document">
162170
/
163171
<input type="text" id="node-input-index" style="width: 30%" placeholder="index name">
@@ -169,10 +177,10 @@
169177
</div>
170178

171179
<script>
172-
$("#node-input-operation").change(function() {
173-
var id = $("#node-input-operation option:selected").val();
174-
if (id == "delete") $(".node-input-payonly").hide();
175-
else $(".node-input-payonly").show();
180+
$("#node-input-search").change(function() {
181+
var searchType = $("#node-input-search option:selected").val();
182+
if (searchType == "_id_") $("#node-search-index-form").hide();
183+
else $("#node-search-index-form").show();
176184
});
177185
</script>
178186
</script>
@@ -208,6 +216,7 @@
208216
cloudant: { type: "cloudant", validate: validateServer },
209217
name : { value: "" },
210218
database: { value: "", required: true },
219+
search : { value: "_id_", required: true },
211220
design : { value: "", required: true },
212221
index : { value: "", required: true }
213222
},
@@ -292,11 +301,22 @@
292301

293302
<script type="text/x-red" data-help-name="cloudant in">
294303
<p>
295-
A node for searching a Cloudant database using a Search Index.
304+
A node for searching documents in a Cloudant database.
305+
</p>
306+
<p>
307+
Searching for a document can be done in two modes: directly by using the
308+
document <b>_id</b> or by using an existing <a
309+
href="https://cloudant.com/for-developers/search/" target="_blank">Search
310+
Index</a>.
311+
</p>
312+
<p>
313+
When querying using the <b>_id</b> option, the <code>_id</code> should be
314+
passed in the <code>msg.payload</code> as a string.
296315
</p>
297316
<p>
298-
Query is performed on existing <b>Search Indexes</b> stored on the desired
299-
database. The query argument should be passed on the <code>msg.payload</code>
300-
following the <code>indexName:value</code> pattern.
317+
To use an existing <b>Search Index</b> stored on the desired database, the
318+
query argument should be passed on the <code>msg.payload</code> following
319+
the <code>indexName:value</code> pattern. The index must be created beforehand
320+
on the database.
301321
</p>
302322
</script>

77-cloudant-cf.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,12 @@ module.exports = function(RED) {
212212
function CloudantInNode(n) {
213213
RED.nodes.createNode(this,n);
214214

215-
this.design = n.design;
216-
this.index = n.index;
217-
this.database = n.database;
218215
this.cloudant = n.cloudant;
219216
this.url = _getUrl(this, n);
217+
this.database = n.database;
218+
this.search = n.search;
219+
this.design = n.design;
220+
this.index = n.index;
220221

221222
if (this.url) {
222223
var node = this;
@@ -225,17 +226,24 @@ module.exports = function(RED) {
225226
var db = nano.use(node.database);
226227

227228
node.on("input", function(msg) {
228-
var query = { q: msg.payload };
229-
230-
db.search(node.design, node.index, query, function(err, doc) {
231-
if (!err) {
232-
node.send(doc);
233-
} else {
234-
node.error(err);
235-
}
236-
});
229+
if (node.search === "_id_") {
230+
var id = msg.payload;
231+
db.get(id, sendDocumentOnPayload);
232+
}
233+
else if (node.search === "_idx_") {
234+
var query = { q: msg.payload };
235+
db.search(node.design, node.index, query, sendDocumentOnPayload);
236+
}
237237
});
238238
}
239+
240+
function sendDocumentOnPayload(err, body) {
241+
if (!err) {
242+
node.send({ payload: body });
243+
} else {
244+
node.error(err);
245+
}
246+
}
239247
}
240248
RED.nodes.registerType("cloudant in", CloudantInNode);
241249

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
node-red-node-cf-cloudant
22
=========================
3-
A [Node-RED](http://nodered.org) node to `insert`, `update` and `delete` documents
3+
A pair of [Node-RED](http://nodered.org) nodes to work with documents
44
in a [Cloudant](http://cloudant.com) database that is integrated with
55
[IBM Bluemix](http://bluemix.net).
66

@@ -13,13 +13,20 @@ npm install node-red-node-cf-cloudant
1313

1414
Usage
1515
-----
16-
Allows basic access to a [Cloudant](http://cloudant.com) database. Currently
17-
it only have one node that supports `insert`, `update` and `delete`
18-
operations.
16+
Allows basic access to a [Cloudant](http://cloudant.com) database to
17+
`insert`, `update`, `delete` and `search` for documents.
1918

2019
To **insert** a new document into the database you have the option to store
2120
the entire `msg` object or just the `msg.payload`. If the input value is not
2221
in JSON format, it will be transformed before being stored.
2322

2423
For **update** and **delete**, you must pass the `_id` and the `_rev`as part
2524
of the input `msg` object.
25+
26+
To **search** for a document you have two options: get a document directly by
27+
its `_id` or use an existing [search index](https://cloudant.com/for-developers/search/)
28+
from the database. For both cases, the query should be passed in the
29+
`msg.payload` input object as a string.
30+
31+
When getting documents by id, the `payload` will be the desired `_id` value.
32+
For `search indexes`, the query should follow the format `indexName:value`.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name" : "node-red-node-cf-cloudant",
3-
"version" : "0.2.0",
3+
"version" : "0.2.1",
44
"description" : "A Node-RED node to write to a Cloudant database on Bluemix",
55
"dependencies" : {
66
"nano" : "5.10.0"

0 commit comments

Comments
 (0)