node-red-node-cf-cloudant
Version:
A Node-RED node to access Cloudant and couchdb databases
333 lines (310 loc) • 13.1 kB
HTML
<!--
Copyright 2014,2016 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script type="text/x-red" data-template-name="cloudant">
<div class="form-row">
<label for="node-config-input-host"><i class="fa fa-bookmark"></i> Host</label>
<input class="input-append-left" type="text" id="node-config-input-host" placeholder="[username].cloudant.com or http://your-server.com">
</div>
<div class="form-row">
<label for="node-config-input-username"><i class="fa fa-user"></i> Username</label>
<input type="text" id="node-config-input-username">
</div>
<div class="form-row">
<label for="node-config-input-pass"><i class="fa fa-lock"></i> Password</label>
<input type="password" id="node-config-input-pass">
</div>
<div class="form-row">
<label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-config-input-name" placeholder="Name">
</div>
</script>
<script type="text/javascript">
RED.nodes.registerType("cloudant",{
category: "config",
color: "rgb(114, 199, 231)",
defaults: {
host: {value:"", required:true},
name: {value:""}
},
credentials: {
username: {type:"text"},
pass: {type:"password"}
},
label: function() {
return this.name || this.host;
}
});
</script>
<script type="text/x-red" data-template-name="cloudant out">
<div class="form-row">
<label for="node-input-service"><i class="fa fa-folder-close"></i> Service</label>
<select id="node-input-service" style="width:68%;">
<!-- <option value="" disabled></option> -->
<option value="_ext_"> External cloudant or couchdb service</option>
</select>
</div>
<div class="form-row hide" id="node-input-external-details">
<label for="node-input-cloudant"><i class="fa fa-bookmark"></i> Server</label>
<input type="text" id="node-input-cloudant">
</div>
<div class="form-row">
<label for="node-input-database"><i class="fa fa-briefcase"></i> Database</label>
<input type="text" id="node-input-database" placeholder="database" style="width:65%;">
</div>
</div>
<div class="form-row">
<label for="node-input-operation"><i class="fa fa-wrench"></i> Operation</label>
<select type="text" id="node-input-operation" style="width:68%;">
<option value="insert">insert</option>
<option value="delete">remove</option>
</select>
</div>
<div class="form-row node-input-payonly">
<label> </label>
<input type="checkbox" id="node-input-payonly" placeholder="Only"
style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-payonly" style="width:65%;">Only store msg.payload object?</label>
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name" style="width:65%;">
</div>
<script>
$("#node-input-operation").change(function() {
var id = $("#node-input-operation option:selected").val();
if (id == "delete") $(".node-input-payonly").hide();
else $(".node-input-payonly").show();
});
</script>
</script>
<script type="text/x-red" data-template-name="cloudant in">
<div class="form-row">
<label for="node-input-service"><i class="fa fa-folder-close"></i> Service</label>
<select id="node-input-service" style="width:68%;">
<!-- <option value="" disabled></option> -->
<option value="_ext_"> External cloudant or couchdb service</option>
</select>
</div>
<div class="form-row hide" id="node-input-external-details">
<label for="node-input-cloudant"><i class="fa fa-bookmark"></i> Server</label>
<input type="text" id="node-input-cloudant">
</div>
<div class="form-row">
<label for="node-input-database"><i class="fa fa-briefcase"></i> Database</label>
<input type="text" id="node-input-database" placeholder="database" style="width:65%;">
</div>
<div class="form-row">
<label for="node-input-search"><i class="fa fa-search"></i> Search by</label>
<select id="node-input-search" style="width:68%;">
<option value="_id_">_id</option>
<option value="_idx_">search index</option>
<option value="_all_">all documents</option>
</select>
</div>
<div id="node-search-index-form" class="form-row">
<label> </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">
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name" style="width:65%;">
</div>
</script>
<script type="text/javascript">
RED.nodes.registerType("cloudant out", {
category: "storage-output",
color: "rgb(114, 199, 231)",
defaults: {
name: { value: "" },
cloudant: { type:"cloudant", validate:validateServer},
database: { value:"", required:true, validate:validateDatabase },
service: { value:"", required:true },
payonly: { value:false },
operation: { value:"insert" }
},
inputs: 1,
outputs: 0,
icon: "cloudant.png",
align: "right",
label: label,
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: oneditprepare
});
RED.nodes.registerType("cloudant in", {
category: "storage-input",
color: "rgb(114, 199, 231)",
defaults: {
name : { value:"" },
cloudant: { type:"cloudant", validate:validateServer },
database: { value:"", required:true, validate:validateDatabase },
service : { value:"", required:true },
search : { value:"_id_", required:true },
design : { value:"" },
index : { value:"" }
},
inputs : 1,
outputs: 1,
icon : "cloudant.png",
label : label,
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: oneditprepare
});
function oneditprepare() {
var node = this;
var serviceSelect = $('#node-input-service');
var searchBySelect = $('#node-input-search');
var searchForm = $('#node-search-index-form');
$.getJSON('cloudant/vcap/',function(data) {
var last = serviceSelect.children().last();
var opts = [];
for (var i=0; i < data.length; i++) {
var selected = node.service == data[i].name;
opts.push('<option value="' + data[i].name + '"' + (selected ? " selected":"") + '>' + data[i].name + '</option>');
}
if (opts.length == 0) {
node.service = "_ext_";
serviceSelect.find("option").filter(function() {
return $(this).val() == node.service;
}).attr('selected', true);
} else {
last.before(opts.join(""));
}
serviceSelect.change();
});
serviceSelect.change(function() {
var service = serviceSelect.val();
if (service == "_ext_") {
$("#node-input-external-details").show();
} else {
$("#node-input-external-details").hide();
}
});
searchBySelect.change(function() {
var searchBy = searchBySelect.val();
if (searchBy === "_idx_") {
node._def.defaults.design.required = true;
node._def.defaults.index.required = true;
searchForm.show();
} else {
node._def.defaults.design.required = false;
node._def.defaults.index.required = false;
searchForm.hide();
}
});
}
function label() {
return this.name || this.database || "cloudant";
}
function validateServer(v) {
return this.service != "_ext_" || v != "_ADD_";
}
// https://wiki.apache.org/couchdb/HTTP_database_API#Naming_and_Addressing
function validateDatabase(v) {
return (v.indexOf('_') !== 0) && (v.search(/[A-Z\s\\/]/g) < 0)
}
</script>
<script type="text/x-red" data-help-name="cloudant out">
<p>
A simple Cloudant output node. Stores <b>msg</b> in a chosen database.
</p>
<p>
Cloudant generates a random id for the <code>_id</code> property if one
is not specified and a new document will be <b>inserted</b> each time.
You can <b>insert</b> only the <code>msg.payload</code> by checking the
checkbox in the configuration window.
</p>
<p>
If you want to <b>update</b> an existing document you must specify the
latest values for <code>_id</code> and <code>_rev</code> in your object.
If they don't match the current values stored in the database you will
receive a <b>conflict error</b> message, so make sure you are using the
most updated information for the document.
</p>
<p>
It is also possible to <b>delete</b> documents from the database by
providing values for <code>_id</code> and <code>_rev</code> and selecting
the <b>remove</b> option for the node. You can pass these values in the
<code>msg</code> object itself or as an object in the
<code>msg.payload</code>.
</p>
<p>
The <b>database name</b> must follow these rules:
<ul>
<li>No spaces</li>
<li>All letters in small caps</li>
<li>The first character can't be <code>_</code></li>
</ul>
</p>
<p>
Your document should avoid having top-level fields that start with
<code>_</code>, with exceptions for <code>_id</code>, <code>_rev</code>
and other <a href="https://wiki.apache.org/couchdb/HTTP_Document_API#Special_Fields">
CouchDB reserved words</a>.
</p>
</script>
<script type="text/x-red" data-help-name="cloudant in">
<p>
A node for searching documents in a Cloudant database.
</p>
<p>
Searching for documents can be done in three modes: directly by using the
document's <b>_id</b>, by using an existing <a
href="https://cloudant.com/for-developers/search/" target="_blank">Search
Index</a> or retrieving <b>all documents</b> stored in your database.
</p>
<p>
When querying using the <b>_id</b> option, the value for the document's
<code>_id</code> should be passed in the <code>msg.payload</code> as a
string.
</p>
<p>
To use an existing <b>Search Index</b> stored on the desired database,
the query argument should be passed on the <code>msg.payload</code> as a
string following the <code>indexName:value</code> pattern. Keep in mind
that <i>the index must be created beforehand in the database.</i> and
referenced here by its <code>design document/index name</code>.
</p>
<p>
When querying using a <b>Search Index</b> you can pass the search
parameters as an object in <code>msg.payload</code>.
</p>
<p>
For example, you can pass an object like this:
<p>
<code>{ query: "abc*", limit: 100 }</code>
</p>
<p>
to change the value of <code>limit</code>. You can find more information
about <b>Search Index</b> parameters in the <a
href="https://docs.cloudant.com/api.html?http#queries" target="_blank">
official Cloudant documentation</a>.
</p>
<p>
The last method to retrieve documents is to simply get all of them by
selecting the option <b>all documents</b>.
</p>
<p>
The <b>database name</b> must follow these rules:
<ul>
<li>No spaces</li>
<li>All letters in small caps</li>
<li>The first character can't be <code>_</code></li>
</ul>
</p>
</script>