Sync update.

This commit is contained in:
kolban
2016-03-04 18:05:24 -06:00
parent 20b3144d79
commit 7c6e07fca4
4 changed files with 217 additions and 33 deletions
+53 -14
View File
@@ -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