Skip to content

Commit 73d6934

Browse files
committed
add query by document id to in node
1 parent b81347f commit 73d6934

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

77-cloudant-cf.html

Lines changed: 14 additions & 5 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</label>
161+
<select id="node-input-search">
162+
<option value="_id_">by _id</option>
163+
<option value="_idx_">by 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
},

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({ payload: 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

0 commit comments

Comments
 (0)