Skip to content

Commit b56a307

Browse files
committed
Merge branch 'dev-in-node' into develop
* dev-in-node: bump npm version initial version of in node for querying
2 parents 85a4588 + a778b63 commit b56a307

File tree

3 files changed

+119
-14
lines changed

3 files changed

+119
-14
lines changed

77-cloudant-cf.html

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,46 @@
137137
</script>
138138
</script>
139139

140+
<script type="text/x-red" data-template-name="cloudant in">
141+
<div class="form-row">
142+
<label for="node-input-service"><i class="fa fa-folder-close"></i> Service</label>
143+
<select id="node-input-service">
144+
<option value="" disabled></option>
145+
<option value="_ext_"> External service</option>
146+
</select>
147+
</div>
148+
149+
<div class="form-row hide" id="node-input-external-details">
150+
<label for="node-input-cloudant"><i class=" fa fa-bookmark"></i> Server</label>
151+
<input type="text" id="node-input-cloudant">
152+
</div>
153+
154+
<div class="form-row">
155+
<label for="node-input-database"><i class="fa fa-briefcase"></i> Database</label>
156+
<input type="text" id="node-input-database" placeholder="database">
157+
</div>
158+
159+
<div class="form-row">
160+
<label for="node-input-design-doc"><i class="fa fa-search"></i> Search Idx.</label>
161+
<input type="text" id="node-input-design" style="width: 30%" placeholder="design document">
162+
/
163+
<input type="text" id="node-input-index" style="width: 30%" placeholder="index name">
164+
</div>
165+
166+
<div class="form-row">
167+
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
168+
<input type="text" id="node-input-name" placeholder="Name">
169+
</div>
170+
171+
<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();
176+
});
177+
</script>
178+
</script>
179+
140180
<script type="text/javascript">
141181
RED.nodes.registerType("cloudant out", {
142182
category: "storage-output",
@@ -160,6 +200,27 @@
160200
oneditprepare: oneditprepare
161201
});
162202

203+
RED.nodes.registerType("cloudant in", {
204+
category: "storage-input",
205+
color: "rgb(114, 199, 231)",
206+
defaults: {
207+
service : { value: "", required: true },
208+
cloudant: { type: "cloudant", validate: validateServer },
209+
name : { value: "" },
210+
database: { value: "", required: true },
211+
design : { value: "", required: true },
212+
index : { value: "", required: true }
213+
},
214+
inputs : 1,
215+
outputs: 1,
216+
icon : "cloudant.png",
217+
label : label,
218+
labelStyle: function() {
219+
return this.name?"node_label_italic":"";
220+
},
221+
oneditprepare: oneditprepare
222+
});
223+
163224
function oneditprepare() {
164225
var select = $('#node-input-service');
165226
var node = this;
@@ -228,3 +289,14 @@
228289
and selecting the <b>remove</b> option for the node.
229290
</p>
230291
</script>
292+
293+
<script type="text/x-red" data-help-name="cloudant in">
294+
<p>
295+
A node for searching a Cloudant database using a Search Index.
296+
</p>
297+
<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.
301+
</p>
302+
</script>

77-cloudant-cf.js

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,7 @@ module.exports = function(RED) {
125125
this.payonly = n.payonly || false;
126126
this.database = n.database;
127127
this.cloudant = n.cloudant;
128-
129-
if (n.service == "_ext_") {
130-
var cloudantConfig = RED.nodes.getNode(this.cloudant);
131-
if (cloudantConfig) {
132-
this.url = cloudantConfig.url;
133-
}
134-
}
135-
else if (n.service != "") {
136-
var cloudantConfig = cfEnv.getService(n.service);
137-
if (cloudantConfig) {
138-
this.url = cloudantConfig.credentials.url;
139-
}
140-
}
128+
this.url = _getUrl(this, n);
141129

142130
if (this.url) {
143131
var node = this;
@@ -220,4 +208,49 @@ module.exports = function(RED) {
220208
}
221209
};
222210
RED.nodes.registerType("cloudant out", CloudantOutNode);
211+
212+
function CloudantInNode(n) {
213+
RED.nodes.createNode(this,n);
214+
215+
this.design = n.design;
216+
this.index = n.index;
217+
this.database = n.database;
218+
this.cloudant = n.cloudant;
219+
this.url = _getUrl(this, n);
220+
221+
if (this.url) {
222+
var node = this;
223+
224+
var nano = require('nano')(node.url);
225+
var db = nano.use(node.database);
226+
227+
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+
});
237+
});
238+
}
239+
}
240+
RED.nodes.registerType("cloudant in", CloudantInNode);
241+
242+
function _getUrl(node, n) {
243+
if (n.service == "_ext_") {
244+
var cloudantConfig = RED.nodes.getNode(node.cloudant);
245+
if (cloudantConfig) {
246+
return cloudantConfig.url;
247+
}
248+
}
249+
else if (n.service != "") {
250+
var cloudantConfig = cfEnv.getService(n.service);
251+
if (cloudantConfig) {
252+
return cloudantConfig.credentials.url;
253+
}
254+
}
255+
}
223256
};

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.0.2",
3+
"version" : "0.2.0",
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)