mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
make LevelDb node aware of db ready/not ready to reduce errors.
This commit is contained in:
parent
17b1a9a785
commit
b03db617e2
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Copyright 2013 IBM Corp.
|
||||
* Copyright 2013,2015 IBM Corp.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@ -21,13 +21,19 @@ module.exports = function(RED) {
|
||||
function LevelNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
this.dbname = n.db;
|
||||
this.ready = false;
|
||||
var node = this;
|
||||
lvldb(this.dbname, function(err, db) {
|
||||
if (err) { node.error(err); }
|
||||
node.db = db;
|
||||
node.db.on('ready', function() { node.ready = true; });
|
||||
node.db.on('closing', function() { node.ready = false; });
|
||||
});
|
||||
this.on('close', function() {
|
||||
if (node.db) { node.db.close(); }
|
||||
node.on('close', function() {
|
||||
if (node.db) {
|
||||
node.ready = false;
|
||||
node.db.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("leveldbase",LevelNode);
|
||||
@ -38,9 +44,9 @@ module.exports = function(RED) {
|
||||
this.level = n.level;
|
||||
this.levelConfig = RED.nodes.getNode(this.level);
|
||||
|
||||
if (this.levelConfig) {
|
||||
var node = this;
|
||||
node.on("input", function(msg) {
|
||||
var node = this;
|
||||
node.on("input", function(msg) {
|
||||
if (node.levelConfig && node.levelConfig.ready) {
|
||||
var key = msg.topic.toString();
|
||||
if (key && (key.length > 0)) {
|
||||
node.levelConfig.db.get(msg.topic, function(err, value) {
|
||||
@ -53,14 +59,10 @@ module.exports = function(RED) {
|
||||
node.send(msg);
|
||||
});
|
||||
}
|
||||
else {
|
||||
node.error("Cannot make key string from msg.topic");
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
this.error("LevelDB database name not configured");
|
||||
}
|
||||
else { node.error("Cannot make key string from msg.topic"); }
|
||||
}
|
||||
else { node.warn("Database not ready"); }
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("leveldb in",LevelDBNodeIn);
|
||||
|
||||
@ -71,9 +73,9 @@ module.exports = function(RED) {
|
||||
this.operation = n.operation;
|
||||
this.levelConfig = RED.nodes.getNode(this.level);
|
||||
|
||||
if (this.levelConfig) {
|
||||
var node = this;
|
||||
node.on("input", function(msg) {
|
||||
var node = this;
|
||||
node.on("input", function(msg) {
|
||||
if (node.levelConfig && node.levelConfig.ready) {
|
||||
var key = msg.topic.toString();
|
||||
if (key && (key.length > 0)) {
|
||||
if (node.operation === "delete") {
|
||||
@ -85,14 +87,10 @@ module.exports = function(RED) {
|
||||
});
|
||||
}
|
||||
}
|
||||
else {
|
||||
node.error("Cannot make key string from msg.topic");
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
this.error("LevelDB database name not configured");
|
||||
}
|
||||
else { node.error("Cannot make key string from msg.topic"); }
|
||||
}
|
||||
else { node.warn("Database not ready"); }
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("leveldb out",LevelDBNodeOut);
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
{
|
||||
"name" : "node-red-node-leveldb",
|
||||
"version" : "0.0.2",
|
||||
"version" : "0.0.3",
|
||||
"description" : "A Node-RED node to read and write to a LevelDB database",
|
||||
"dependencies" : {
|
||||
"level" : "0.18.0"
|
||||
"level" : "1.0.0"
|
||||
},
|
||||
"repository" : {
|
||||
"type":"git",
|
||||
|
Loading…
Reference in New Issue
Block a user