Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataRecovery node #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions json-db.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@



</script>
<script type="text/x-red" data-template-name="DataRecovery">
<div class="form-row" id="node-input-server-row">
<label for="node-input-collection"><i class="icon-tasks"></i> Collection</label>
<input type="text" id="node-input-collection">
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-flag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Node-RED">
</div>
<div class="form-row">
<label for="node-input-path"><i class="icon-floppy-save"></i> Data Path</label>
<input type="text" id="node-input-path" placeholder="/">
</div>
<div class="form-row">
<input type="checkbox" style="display: inline-block; width: auto; vertical-align: top;" id="node-input-update" />
<label style="width: auto;" for="node-input-update"><i class="icon-log-in"></i>Merge Data</label>
</div>
<div class="form-row">
<input type="checkbox" style="display: inline-block; width: auto; vertical-align: top;" id="node-input-error" />
<label style="width: auto;" for="node-input-error"><i class="icon-log-in"></i>Always send the message</label>
</div>
<div class="form-tips">When the DataPath doesn't exists, the Node will trigger an error and will stop the flow unless you checked "Always send the message".
The message sent will contain a <code>msg.error</code> with the error.</div>
</script>
<script type="text/x-red" data-help-name="DataIn">
<p>Use DataIn to <b>store</b> data into the Json-DB.</p>
Expand Down Expand Up @@ -106,6 +130,31 @@
<p>You can also get the whole data by just setting <code>/</code> as DataPath</p>


</script>
<script type="text/x-red" data-help-name="DataRecovery">
<p>Use DataRecovery to <b>persist/recover on startup</b> data from the Json-DB.</p>
<p>The DataRecovery node is used to store/retrieve information into the chosen collection.
You select which collection you want to use and the path to the object.</p>
<p><code>The msg.payload</code> of the output will contain the data retrieved from the Json-DB.</p>
<p>If you chose to merge the data, the node will automatically merge Arrays with others Arrays and Objects with others Objects. You can't merge Array with Object and vice versa</p>
<p>In case of error and if you check "Always send the message", the <code>msg.error</code> will contain the error that has been triggered.
The rest of the properties of the msg will stay unchanged. If you activated the <i>Status</i> in the configuration panel of Node-RED, you'll
be able to see directly the last Error message on top of the node.</p>
<p>The DataPath represent the path through the object. If the DataPath doesn't exists, an <b>Error</b> will be thrown.</p>
<code>
{
test:
{
data: "First"
},
test2 : "Second"
}
</code>
<p>To change the "First" you have to set as Data Path : <code>/test/data</code></p>
<p>To change "Second" : <code>/test2</code></p>
<p>You can also get the whole data by just setting <code>/</code> as DataPath</p>


</script>

<script type="text/x-red" data-template-name="json-db-collection">
Expand Down Expand Up @@ -181,5 +230,25 @@
return this.name ? "node_label_italic" : "";
}

});
RED.nodes.registerType('DataRecovery', {
category: 'storage-output',
defaults: {
collection: {type: "json-db-collection", required: true},
name: {value: ""},
path: {value: '/', required: true},
error: {value: false, required: true}
},
color: "#6699FF",
inputs: 1,
outputs: 1,
icon: "json-db-out.png",
label: function () {
return this.name || this.path;
},
labelStyle: function () {
return this.name ? "node_label_italic" : "";
}

});
</script>
42 changes: 42 additions & 0 deletions json-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,47 @@ module.exports = function (RED) {

RED.nodes.registerType("DataOut", DataOut);

function DataRecovery(n) {
RED.nodes.createNode(this, n);
this.collection = RED.nodes.getNode(n.collection);
this.dataPath = n.path;
this.override = !n.update;
var node = this;

node.on('startup', function (msg) {
var path = node.dataPath;
try {
var data = node.collection.db.getData(path);
msg.payload = data;
node.status({fill: "green", shape: "dot", text: "No Error"});
node.send(msg);
} catch (error) {
if (node.sendError) {
msg.error = error.toString();
node.send(msg);
node.status({fill: "yellow", shape: "ring", text: error.toString()});
} else {
node.error(error);
node.status({fill: "red", shape: "dot", text: error.toString()});
}
}
});

setTimeout(function () {
node.emit('startup', {});
}, 3000);

this.on("input", function (msg) {
var path = node.dataPath;
try {
node.collection.db.push(path, msg.payload, node.override);
} catch (error) {
node.error(error);
}
});

}

RED.nodes.registerType("DataRecovery", DataRecovery);
}