2013-10-30 21:38:23 +01:00
|
|
|
|
2014-06-29 00:37:19 +02:00
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var lvldb = require('level');
|
2013-10-30 21:38:23 +01:00
|
|
|
|
2014-06-29 00:37:19 +02:00
|
|
|
function LevelNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.dbname = n.db;
|
2020-02-17 23:42:56 +01:00
|
|
|
this.encoding = n.encoding || "utf8";
|
2015-05-21 13:37:48 +02:00
|
|
|
this.ready = false;
|
2013-11-10 18:01:19 +01:00
|
|
|
var node = this;
|
2020-02-17 23:42:56 +01:00
|
|
|
lvldb(this.dbname, {valueEncoding:this.encoding}, function(err, db) {
|
2014-06-29 00:37:19 +02:00
|
|
|
if (err) { node.error(err); }
|
|
|
|
node.db = db;
|
2015-05-21 13:37:48 +02:00
|
|
|
node.db.on('ready', function() { node.ready = true; });
|
|
|
|
node.db.on('closing', function() { node.ready = false; });
|
2014-06-29 00:37:19 +02:00
|
|
|
});
|
2015-05-21 13:37:48 +02:00
|
|
|
node.on('close', function() {
|
|
|
|
if (node.db) {
|
|
|
|
node.ready = false;
|
|
|
|
node.db.close();
|
|
|
|
}
|
2013-11-10 18:01:19 +01:00
|
|
|
});
|
|
|
|
}
|
2014-06-29 00:37:19 +02:00
|
|
|
RED.nodes.registerType("leveldbase",LevelNode);
|
2013-10-30 21:38:23 +01:00
|
|
|
|
|
|
|
|
2014-06-29 00:37:19 +02:00
|
|
|
function LevelDBNodeIn(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.level = n.level;
|
|
|
|
this.levelConfig = RED.nodes.getNode(this.level);
|
2013-10-30 21:38:23 +01:00
|
|
|
|
2015-05-21 13:37:48 +02:00
|
|
|
var node = this;
|
|
|
|
node.on("input", function(msg) {
|
|
|
|
if (node.levelConfig && node.levelConfig.ready) {
|
2015-04-14 18:23:31 +02:00
|
|
|
var key = msg.topic.toString();
|
|
|
|
if (key && (key.length > 0)) {
|
2014-06-29 00:37:19 +02:00
|
|
|
node.levelConfig.db.get(msg.topic, function(err, value) {
|
|
|
|
if (err) {
|
|
|
|
//node.warn(err);
|
|
|
|
// for some reason they treat nothing found as an error...
|
|
|
|
msg.payload = null; // so we should return null
|
|
|
|
}
|
|
|
|
else { msg.payload = value; }
|
|
|
|
node.send(msg);
|
|
|
|
});
|
2013-11-10 18:01:19 +01:00
|
|
|
}
|
2015-05-21 13:37:48 +02:00
|
|
|
else { node.error("Cannot make key string from msg.topic"); }
|
|
|
|
}
|
2015-05-21 15:12:49 +02:00
|
|
|
else { node.error("Database not ready",msg); }
|
2015-05-21 13:37:48 +02:00
|
|
|
});
|
2013-11-10 18:01:19 +01:00
|
|
|
}
|
2014-06-29 00:37:19 +02:00
|
|
|
RED.nodes.registerType("leveldb in",LevelDBNodeIn);
|
|
|
|
|
|
|
|
|
|
|
|
function LevelDBNodeOut(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.level = n.level;
|
|
|
|
this.operation = n.operation;
|
|
|
|
this.levelConfig = RED.nodes.getNode(this.level);
|
|
|
|
|
2015-05-21 13:37:48 +02:00
|
|
|
var node = this;
|
|
|
|
node.on("input", function(msg) {
|
|
|
|
if (node.levelConfig && node.levelConfig.ready) {
|
2015-04-14 18:23:31 +02:00
|
|
|
var key = msg.topic.toString();
|
|
|
|
if (key && (key.length > 0)) {
|
2014-06-29 00:37:19 +02:00
|
|
|
if (node.operation === "delete") {
|
|
|
|
node.levelConfig.db.del(msg.topic);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.levelConfig.db.put(msg.topic, msg.payload, function(err) {
|
2017-06-05 20:21:25 +02:00
|
|
|
if (err) { node.error(err,msg); }
|
2014-06-29 00:37:19 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-06-05 20:21:25 +02:00
|
|
|
else { node.error("Cannot make key string from msg.topic",msg); }
|
2015-05-21 13:37:48 +02:00
|
|
|
}
|
2015-05-21 15:12:49 +02:00
|
|
|
else { node.error("Database not ready",msg); }
|
2015-05-21 13:37:48 +02:00
|
|
|
});
|
2013-11-10 18:01:19 +01:00
|
|
|
}
|
2014-06-29 00:37:19 +02:00
|
|
|
RED.nodes.registerType("leveldb out",LevelDBNodeOut);
|
2013-10-30 21:38:23 +01:00
|
|
|
}
|