Skip to content

Commit 1ff72ad

Browse files
committed
* support search index parameters
* add "all documents" option
1 parent fbe2f57 commit 1ff72ad

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

77-cloudant-cf.html

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
<select id="node-input-search">
162162
<option value="_id_">_id</option>
163163
<option value="_idx_">search index</option>
164+
<option value="_all_">all documents</option>
164165
</select>
165166
</div>
166167

@@ -318,19 +319,34 @@
318319
A node for searching documents in a Cloudant database.
319320
</p>
320321
<p>
321-
Searching for a document can be done in two modes: directly by using the
322-
document's <b>_id</b> or by using an existing <a
322+
Searching for documents can be done in three modes: directly by using the
323+
document's <b>_id</b>, by using an existing <a
323324
href="https://cloudant.com/for-developers/search/" target="_blank">Search
324-
Index</a>.
325+
Index</a> or retrieving <b>all documents</b> stored in your database.
325326
</p>
326327
<p>
327-
When querying using the <b>_id</b> option, the value for <code>_id</code>
328-
should be passed in the <code>msg.payload</code> as a string.
328+
When querying using the <b>_id</b> option, the value for the document's
329+
<code>_id</code> should be passed in the <code>msg.payload</code> as a
330+
string.
329331
</p>
330332
<p>
331-
To use an existing <b>Search Index</b> stored on the desired database, the
332-
query argument should be passed on the <code>msg.payload</code> following
333-
the <code>indexName:value</code> pattern. <i>The index must be created
334-
beforehand on the database.</i>
333+
To use an existing <b>Search Index</b> stored on the desired database,
334+
the query argument should be passed on the <code>msg.payload</code> as a
335+
string following the <code>indexName:value</code> pattern. Keep in mind
336+
that <i>the index must be created beforehand in the database.</i> and
337+
referenced here by its <code>design document/index name</code>.
338+
</p>
339+
<p>
340+
When querying using a <b>Search Index</b> you can pass the search
341+
parameters as an object in <code>msg.payload</code>. For example, you
342+
can pass an object like this: <code>{ query: "abc*", limit: 100 }</code>
343+
to change the value of <code>limit</code>. You can find more information
344+
on the accepted parameters in the <a
345+
href="https://docs.cloudant.com/api.html?http#queries" target="_blank">
346+
official Cloudant documentation</a>.
347+
</p>
348+
<p>
349+
The last method to retrieve documents is to simply get all of them by
350+
selecting the option <b>all documents</b>.
335351
</p>
336352
</script>

77-cloudant-cf.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,21 @@ module.exports = function(RED) {
242242
});
243243
}
244244
else if (node.search === "_idx_") {
245-
options.q = options.q || formatSearchQuery(msg.payload);
245+
options.query = options.query || options.q || formatSearchQuery(msg.payload);
246+
options.include_docs = options.include_docs || true;
246247
options.limit = options.limit || 200;
247248

248249
db.search(node.design, node.index, options, function(err, body) {
249250
sendDocumentOnPayload(err, body, msg);
250251
});
251252
}
253+
else if (node.search === "_all_") {
254+
options.include_docs = options.include_docs || true;
255+
256+
db.list(options, function(err, body) {
257+
sendDocumentOnPayload(err, body, msg);
258+
});
259+
}
252260
});
253261
}
254262
});
@@ -280,9 +288,23 @@ module.exports = function(RED) {
280288

281289
function sendDocumentOnPayload(err, body, msg) {
282290
if (!err) {
283-
msg.payload = body.rows;
284291
msg.cloudant = body;
285-
} else {
292+
293+
if ("rows" in body) {
294+
msg.payload = body.rows.
295+
map(function(el) {
296+
if (el.doc._id.indexOf("_design/") < 0) {
297+
return el.doc;
298+
}
299+
}).
300+
filter(function(el) {
301+
return el !== null && el !== undefined;
302+
});
303+
} else {
304+
msg.payload = body;
305+
}
306+
}
307+
else {
286308
msg.payload = null;
287309

288310
if (err.description === "missing") {

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.8",
3+
"version" : "0.2.9",
44
"description" : "A Node-RED node to access a Cloudant database on Bluemix",
55
"dependencies" : {
66
"cfenv" : "1.0.0",

0 commit comments

Comments
 (0)