Skip to content

Added ability to use Cloudant views #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions 77-cloudant-cf.html
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@
<select id="node-input-search" style="width:68%;">
<option value="_id_">_id</option>
<option value="_idx_">search index</option>
<option value="_view_">view</option>
<option value="_all_">all documents</option>
</select>
</div>
<div id="node-search-index-form" class="form-row">
<label>&nbsp;</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">
<input type="text" id="node-input-index" style="width:30%" placeholder="index/view name">
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
Expand Down Expand Up @@ -214,7 +215,7 @@
searchBySelect.change(function() {
var searchBy = searchBySelect.val();

if (searchBy === "_idx_") {
if (searchBy === "_idx_" || searchBy === "_view_") {
node._def.defaults.design.required = true;
node._def.defaults.index.required = true;
searchForm.show();
Expand Down
18 changes: 14 additions & 4 deletions 77-cloudant-cf.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ module.exports = function(RED) {
}

node.on("input", function(msg) {
if (err) {
return node.error(err.description, err);
if (err) {
return node.error(err.description, err);
}

delete msg._msgid;
Expand Down Expand Up @@ -271,6 +271,11 @@ module.exports = function(RED) {
sendDocumentOnPayload(err, body, msg);
});
}
else if (node.search === "_view_") {
db.view(node.design, node.index, options, function(err, body) {
sendDocumentOnPayload(err, body, msg);
});
}
else if (node.search === "_all_") {
options.include_docs = options.include_docs || true;

Expand Down Expand Up @@ -313,9 +318,14 @@ module.exports = function(RED) {
if ("rows" in body) {
msg.payload = body.rows.
map(function(el) {
if (el.doc._id.indexOf("_design/") < 0) {
return el.doc;
if (el.doc) {
if (el.doc._id.indexOf("_design/") < 0) {
return el.doc;
}
} else {
return el;
}

}).
filter(function(el) {
return el !== null && el !== undefined;
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@ in JSON format, it will be transformed before being stored.
For **update** and **delete**, you must pass the `_id` and the `_rev`as part
of the input `msg` object.

To **search** for a document you have two options: get a document directly by
its `_id` or use an existing [search index](https://cloudant.com/for-developers/search/)
from the database. For both cases, the query should be passed in the
`msg.payload` input object as a string.
To **search** for a document you have three options:
* get a document directly by its `_id`
* use an existing [search index](https://cloudant.com/for-developers/search/)
from the database
* use an existing [view](https://console.bluemix.net/docs/services/Cloudant/api/using_views.html#using-views)

When getting documents by id, the `payload` will be the desired `_id` value.

For `search indexes`, the query should follow the format `indexName:value`.

For `views`, `payload` should be set be set to an object containing key/value pairs
as defined in the Query string section in the [Cloudant documentation](https://console.bluemix.net/docs/services/Cloudant/api/using_views.html#using-views)

Authors
-------
* Luiz Gustavo Ferraz Aoqui - [[email protected]](mailto:[email protected])
* Túlio Pascoal
* Adam Hammond
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-red-node-cf-cloudant",
"version": "0.2.17",
"version": "0.3.1",
"description": "A Node-RED node to access Cloudant and couchdb databases",
"dependencies": {
"cfenv": "1.0.0",
Expand Down Expand Up @@ -32,6 +32,9 @@
},
{
"name": "Igor Leão"
},
{
"name": "Adam Hammond"
}
],
"maintainers": [
Expand Down