mirror of
https://github.com/farcasclaudiu/node-red-contrib-couchdb.git
synced 2026-06-22 07:01:43 +03:00
Sync update.
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
# CouchDB nodes Node-RED
|
||||
Accessor nodes for Apache CouchDB from Node-RED.
|
||||
|
||||
## Installation
|
||||
Use `npm install node-red-contrib-couchdb` to install.
|
||||
|
||||
## Usage
|
||||
This package provides some nodes that can be used in conjunction with an Apache CouchDB. Specifically,
|
||||
we can insert new documents and query/retrieve existing documents. For the insertion, we supply the
|
||||
JavaScript document in the `msg.payload` property which could include the `_id` property to
|
||||
provide the identity. If we are updating the document, then a valid `_rev` property should also be present.
|
||||
|
||||
For a query/retrieval, we have the choice of either retrieving by id value or by a design document view search.
|
||||
For an id, specify the `_id` value as the `msg.payload`. For a design document view retrieval, supply
|
||||
the search key in `msg.payload`.
|
||||
|
||||
In both cases, the document is returned as the output `msg.payload`.
|
||||
|
||||
To connect to the database, the URL for the CouchDB server should be supplied along with the database name for
|
||||
the database to be accessed.
|
||||
|
||||
|
||||
## Contributing
|
||||
1. Fork it!
|
||||
2. Create your feature branch: `git checkout -b my-new-feature`
|
||||
3. Commit your changes: `git commit -am 'Add some feature'`
|
||||
4. Push to the branch: `git push origin my-new-feature`
|
||||
5. Submit a pull request
|
||||
|
||||
## History
|
||||
* 2015-03-04 - First release
|
||||
|
||||
## Credits
|
||||
Neil Kolban
|
||||
|
||||
## License
|
||||
Apache 2.0
|
||||
+118
-19
@@ -20,7 +20,7 @@
|
||||
inputs: 1,
|
||||
outputs: 1,
|
||||
icon: "db.png",
|
||||
color: "#3FADB5",
|
||||
color: "#73c5e7",
|
||||
label: function () {
|
||||
return this.name || "couchdb"
|
||||
},
|
||||
@@ -39,17 +39,52 @@
|
||||
},
|
||||
retrievalType: {
|
||||
value: "byId"
|
||||
},
|
||||
designDoc: {
|
||||
value: null
|
||||
},
|
||||
viewName: {
|
||||
value: null
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
RED.nodes.registerType('couchdbinsert',{
|
||||
// node definition
|
||||
category: "storage",
|
||||
inputs: 1,
|
||||
outputs: 0,
|
||||
icon: "db.png",
|
||||
color: "#73c5e7",
|
||||
label: function () {
|
||||
return this.name || "couchdb insert"
|
||||
},
|
||||
paletteLabel: "couchdb insert",
|
||||
align: "right",
|
||||
defaults: {
|
||||
name: {
|
||||
value: ""
|
||||
},
|
||||
serverUrl: {
|
||||
value: "",
|
||||
required: true
|
||||
},
|
||||
database: {
|
||||
value: "",
|
||||
required: true
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="couchdb">
|
||||
<p>A node for searching documents in a couchdb database.</p>
|
||||
<p>A node for searching documents in a CouchDB database.</p>
|
||||
<p>Searching for documents can be done in a few modes. Directly by using the documents
|
||||
<b>_id</b> or by using a key lookup in a view.</p>
|
||||
<p>When querying using the <b>_id</b> option, the value for the documents <code>_id</code>
|
||||
<p>When querying using the <b>by Id</b> option, the value for the documents <code>_id</code>
|
||||
should be passed in the <code>msg.payload</code> as a string.</p>
|
||||
<p>When querying using <b>by view</b> option, the value for the search key should
|
||||
be passed in the <code>msg.payload</code>.</p>
|
||||
<p>The database name must follow these rules:
|
||||
<ul>
|
||||
<li>No spaces</li>
|
||||
@@ -59,21 +94,85 @@ should be passed in the <code>msg.payload</code> as a string.</p>
|
||||
</p>
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="couchdbinsert">
|
||||
<p>A node for inserting documents in a CouchDB database.</p>
|
||||
<p>The document to be insert will be the JavaScript object found in <code>msg.payload</code>.
|
||||
To update an existing document, a valid <code>_rev</code> must also be present in the object.
|
||||
Before you use this node, make sure you understand the principles of working with CouchDB.</p>
|
||||
|
||||
<p>The database name must follow these rules:
|
||||
<ul>
|
||||
<li>No spaces</li>
|
||||
<li>All letters in lower case</li>
|
||||
<li>The first character must not be <code>_</code></li>
|
||||
</ul>
|
||||
</p>
|
||||
</script>
|
||||
|
||||
<!--
|
||||
*
|
||||
* couchdb
|
||||
*
|
||||
-->
|
||||
<script type="text/x-red" data-template-name="couchdb">
|
||||
<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">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-serverUrl"><i class="icon-tasks"></i> Server URL</label>
|
||||
<input type="text" id="node-input-serverUrl" placeholder="Server URL">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-database"><i class="icon-tasks"></i> Database</label>
|
||||
<input type="text" id="node-input-database" placeholder="Database">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-retrievalType"><i class="icon-tasks"></i> Retrieval Type</label>
|
||||
<input type="text" id="node-input-retrievalType" placeholder="Retrieval Type">
|
||||
</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">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-serverUrl"><i class="fa fa-server"></i> Server URL</label>
|
||||
<input type="text" id="node-input-serverUrl" placeholder="Server URL">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-database"><i class="fa fa-database"></i> Database</label>
|
||||
<input type="text" id="node-input-database" placeholder="Database">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-retrievalType"><i class="fa fa-random"></i> Type</label>
|
||||
<select type="text" id="node-input-retrievalType">
|
||||
<option value="byId">... by Id</option>
|
||||
<option value="byView">... by View</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-row node-input-designDoc">
|
||||
<label for="node-input-designDoc"><i class="fa fa-book"></i> Design doc</label>
|
||||
<input type="text" id="node-input-designDoc" placeholder="Design doc">
|
||||
</div>
|
||||
<div class="form-row node-input-viewName">
|
||||
<label for="node-input-viewName"><i class="fa fa-binoculars"></i> View name</label>
|
||||
<input type="text" id="node-input-viewName" placeholder="View name">
|
||||
</div>
|
||||
|
||||
<script>
|
||||
$("#node-input-retrievalType").change(function() {
|
||||
var type = $("#node-input-retrievalType").val();
|
||||
if (type == "byId") {
|
||||
$(".node-input-designDoc").hide();
|
||||
$(".node-input-viewName").hide();
|
||||
} else {
|
||||
$(".node-input-designDoc").show();
|
||||
$(".node-input-viewName").show();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</script>
|
||||
|
||||
<!--
|
||||
*
|
||||
* couchdbinsert
|
||||
*
|
||||
-->
|
||||
<script type="text/x-red" data-template-name="couchdbinsert">
|
||||
<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">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-serverUrl"><i class="fa fa-server"></i> Server URL</label>
|
||||
<input type="text" id="node-input-serverUrl" placeholder="Server URL">
|
||||
</div>
|
||||
<div class="form-row">
|
||||
<label for="node-input-database"><i class="fa fa-database"></i> Database</label>
|
||||
<input type="text" id="node-input-database" placeholder="Database">
|
||||
</div>
|
||||
</script>
|
||||
+53
-14
@@ -21,7 +21,9 @@
|
||||
* * database - The name of the database
|
||||
* * retrievalType - How we should retrieve a document
|
||||
* * byId
|
||||
* * ???
|
||||
* * byView
|
||||
* * designDoc - The name of the design document
|
||||
* * viewName - The name of a view
|
||||
* This module makes extensive use of the project called "dscape/nano"
|
||||
* found on Github at:
|
||||
*
|
||||
@@ -30,25 +32,62 @@
|
||||
*
|
||||
*/
|
||||
module.exports = function(RED) {
|
||||
/**
|
||||
* The CouchDBNode provides the implementation for retrieving data from the CouchDB
|
||||
* database.
|
||||
*/
|
||||
function CouchDBNode(config) {
|
||||
console.log("CouchDBNode: config: " + JSON.stringify(config));
|
||||
|
||||
var thisNode = this;
|
||||
var nano = require("nano")(config.serverUrl);
|
||||
var db = nano.use(config.database);
|
||||
this.on('input', function(msg) {
|
||||
|
||||
this.on("input", function(msg) {
|
||||
// Process the request here
|
||||
db.get(msg.payload, function(err, body) {
|
||||
console.log("We got a document: " + body);
|
||||
if (!err) {
|
||||
msg.payload = body;
|
||||
thisNode.send(msg);
|
||||
}
|
||||
});
|
||||
console.log("Process message: " + JSON.stringify(msg));
|
||||
});
|
||||
if (config.retrievalType == "byId") {
|
||||
db.get(msg.payload, function(err, body) {
|
||||
if (!err) {
|
||||
msg.payload = body;
|
||||
thisNode.send(msg);
|
||||
}
|
||||
}); // End of db.get
|
||||
} // End of byId
|
||||
else if (config.retrievalType == "byView") {
|
||||
db.view(config.designDoc, config.viewName, {
|
||||
key: msg.payload,
|
||||
include_docs: true
|
||||
}, function(err, body) {
|
||||
if (!err) {
|
||||
msg.payload = body.rows;
|
||||
thisNode.send(msg);
|
||||
}
|
||||
}); // End of db.view
|
||||
} // End of byView
|
||||
}); // End of on "input"
|
||||
RED.nodes.createNode(thisNode, config);
|
||||
} // End of couchDBNode definition
|
||||
} // End of CouchDBNode definition
|
||||
|
||||
/**
|
||||
* Insert data into the database
|
||||
* * serverUrl - The URL to reach the CouchDB server ... eg. http://localhost:5984
|
||||
* * database - The name of the database
|
||||
*/
|
||||
function CouchDBInsertNode(config) {
|
||||
var thisNode = this;
|
||||
var nano = require("nano")(config.serverUrl);
|
||||
var db = nano.use(config.database);
|
||||
this.on("input", function(msg) {
|
||||
// Process the insertion request here
|
||||
db.insert(msg.payload, function(err, body) {
|
||||
// Nothing to do on the callback
|
||||
if (err) {
|
||||
thisNode.warn("[" + config.type + ":" + config.name + "]: Error: " + JSON.stringify(err));
|
||||
}
|
||||
}); // End of db.insert
|
||||
}); // End of on "input"
|
||||
} // End of CouchDBInsertNode
|
||||
|
||||
RED.nodes.registerType("couchdb", CouchDBNode);
|
||||
}
|
||||
RED.nodes.registerType("couchdbinsert", CouchDBInsertNode);
|
||||
} // End of module.exports
|
||||
// End of file
|
||||
@@ -1,12 +1,21 @@
|
||||
// Test our understanding of nano
|
||||
var docId = "56d8e35efaa2604f06173dc5";
|
||||
var design = "dd1";
|
||||
var view = "company";
|
||||
|
||||
var nano = require("nano")("http://localhost:5984");
|
||||
var test1 = nano.use("test1");
|
||||
/*
|
||||
test1.get(docId, function(err, body, header) {
|
||||
dump(err, body, header);
|
||||
console.log("Got something!");
|
||||
});
|
||||
*/
|
||||
test1.view(design, view, {key: "MOTOVATE", include_docs: true}, function(err, body, header) {
|
||||
dump(err, body, header);
|
||||
console.log("Got something!");
|
||||
});
|
||||
|
||||
console.log("test-nanon: Core completed");
|
||||
|
||||
function dump(err, body, header) {
|
||||
|
||||
Reference in New Issue
Block a user