Skip to content

Commit 462cc44

Browse files
committed
Merge branch 'develop'
* develop: bump npm version initial version of in node for querying refactoring code
2 parents 0319840 + b56a307 commit 462cc44

File tree

3 files changed

+187
-87
lines changed

3 files changed

+187
-87
lines changed

77-cloudant-cf.html

Lines changed: 140 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@
8888
</script>
8989

9090
<script type="text/x-red" data-template-name="cloudant out">
91-
9291
<div class="form-row">
9392
<label for="node-input-service"><i class="fa fa-folder-close"></i> Service</label>
9493
<select id="node-input-service">
@@ -136,7 +135,138 @@
136135
else $(".node-input-payonly").show();
137136
});
138137
</script>
138+
</script>
139+
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>
139170

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+
180+
<script type="text/javascript">
181+
RED.nodes.registerType("cloudant out", {
182+
category: "storage-output",
183+
color: "rgb(114, 199, 231)",
184+
defaults: {
185+
service: { value: "", required: true },
186+
cloudant: { type: "cloudant", validate: validateServer},
187+
name: { value: "" },
188+
database: { value: "", required: true },
189+
payonly: { value: false },
190+
operation: { value: "insert" }
191+
},
192+
inputs: 1,
193+
outputs: 0,
194+
icon: "cloudant.png",
195+
align: "right",
196+
label: label,
197+
labelStyle: function() {
198+
return this.name?"node_label_italic":"";
199+
},
200+
oneditprepare: oneditprepare
201+
});
202+
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+
224+
function oneditprepare() {
225+
var select = $('#node-input-service');
226+
var node = this;
227+
228+
$.getJSON('cloudant/vcap/',function(data) {
229+
var last = select.children().last();
230+
var opts = [];
231+
232+
for (var i=0; i < data.length; i++) {
233+
var selected = node.service == data[i].name;
234+
opts.push(
235+
'<option value="' + data[i].name + '"' + (selected ? " selected":"") + '>' +
236+
data[i].name +
237+
'</option>'
238+
);
239+
}
240+
241+
if (opts.length == 0) {
242+
node.service = "_ext_";
243+
select.find("option").filter(function() {
244+
return $(this).val() == node.service;
245+
}).attr('selected', true);
246+
} else {
247+
last.before(opts.join(""));
248+
}
249+
250+
select.change();
251+
});
252+
253+
select.change(function() {
254+
var service = select.val();
255+
if (service == "_ext_") {
256+
$("#node-input-external-details").show();
257+
} else {
258+
$("#node-input-external-details").hide();
259+
}
260+
});
261+
}
262+
263+
function label() {
264+
return this.name || this.database || "cloudant";
265+
}
266+
267+
function validateServer(v) {
268+
return this.service != "_ext_" || v != "_ADD_";
269+
}
140270
</script>
141271

142272
<script type="text/x-red" data-help-name="cloudant out">
@@ -160,76 +290,13 @@
160290
</p>
161291
</script>
162292

163-
<script type="text/javascript"> //js functions
164-
(function() {
165-
166-
function oneditprepare() {
167-
var select = $('#node-input-service');
168-
var node = this;
169-
170-
$.getJSON('cloudant/vcap/',function(data) {
171-
var last = select.children().last();
172-
var opts = [];
173-
174-
for (var i=0; i < data.length; i++) {
175-
var selected = node.service == data[i].name;
176-
opts.push(
177-
'<option value="' + data[i].name + '"' + (selected ? " selected":"") + '>' +
178-
data[i].name +
179-
'</option>'
180-
);
181-
}
182-
183-
if (opts.length == 0) {
184-
node.service = "_ext_";
185-
select.find("option").filter(function() {
186-
return $(this).val() == node.service;
187-
}).attr('selected', true);
188-
} else {
189-
last.before(opts.join(""));
190-
}
191-
192-
select.change();
193-
});
194-
195-
select.change(function() {
196-
var service = select.val();
197-
if (service == "_ext_") {
198-
$("#node-input-external-details").show();
199-
} else {
200-
$("#node-input-external-details").hide();
201-
}
202-
});
203-
}
204-
205-
function label() {
206-
return this.name || this.database || "cloudant";
207-
}
208-
209-
function validateServer(v) {
210-
return this.service != "_ext_" || v != "_ADD_";
211-
}
212-
213-
RED.nodes.registerType("cloudant out", {
214-
category: "storage-output",
215-
color: "rgb(114, 199, 231)",
216-
defaults: {
217-
service: { value: "", required: true },
218-
cloudant: { type: "cloudant", validate: validateServer},
219-
name: { value: "" },
220-
database: { value: "", required: true },
221-
payonly: { value: false },
222-
operation: { value: "insert" }
223-
},
224-
inputs: 1,
225-
outputs: 0,
226-
icon: "cloudant.png",
227-
align: "right",
228-
label: label,
229-
labelStyle: function() {
230-
return this.name?"node_label_italic":"";
231-
},
232-
oneditprepare: oneditprepare
233-
});
234-
})();
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>
235302
</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)