Last active
August 29, 2015 13:56
-
-
Save Blackmist/8962662 to your computer and use it in GitHub Desktop.
Node-Red output node for Windows Azure Table Storage.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/x-red" data-template-name="azure table"> | |
<div class="form-row"> | |
<label for="node-input-tablename"><i class="icon-tasks"></i> Table</label> | |
<input type="text" id="node-input-tablename" placeholder="Table name"> | |
</div> | |
<div class="form-row"> | |
<label for="node-input-storageaccount"><i class="icon-tag"></i> Account</label> | |
<input type="text" id="node-input-storageaccount" placeholder="storage account"> | |
</div> | |
<div class="form-row"> | |
<label for="node-input-storagekey"><i class="icon-lock"></i> Key</label> | |
<input type="text" id="node-input-storagekey" placeholder="storage key"> | |
</div> | |
<div class="form-row"> | |
<label for="node-input-name"><i class="icon-tag"></i> Name</label> | |
<input type="text" id="node-input-name" placeholder="Name"> | |
</div> | |
</script> | |
<script type="text/x-red" data-help-name="azure table"> | |
<p>Writes the <b>msg.payload</b> to Windows Azure Table storage.</p> | |
<p><strong>Table</strong> - the table name, if storing data to Windows Azure Table Storage</p> | |
<p><strong>Account</strong> - the storage account to use for the table. This value can also be specified by setting the AZURE_STORAGE_ACCOUNT environment variable.</p> | |
<p><strong>Key</strong> - the key for the storage account. This value can also be specified by setting the AZURE_STORAGE_ACCESS_KEY environment variable. | |
<p><strong>Name</strong> - the name of this azure node configuration</p> | |
</script> | |
<script type="text/javascript"> | |
RED.nodes.registerType('azure table',{ | |
category: 'storage-output', | |
defaults: { | |
name: {value:""}, | |
storageaccount: {value:""}, | |
storagekey: {value:""}, | |
tablename: {value:"", required:true} | |
}, | |
color:"#C0DEED", | |
inputs:1, | |
outputs:0, | |
icon: "db.png", | |
label: function() { | |
return this.name||this.tablename||'azure table'; | |
}, | |
labelStyle: function() { | |
return this.name?"node_label_italic":""; | |
} | |
}); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var RED = require(process.env.NODE_RED_HOME+"/red/red"); | |
var azure = require('azure'); | |
function AzureNode(n) { | |
RED.nodes.createNode(this,n); | |
this.tablename = n.tablename; | |
this.storageaccount = n.storageaccount; | |
this.storagekey = n.storagekey; | |
if(this.storageaccount==="" || this.storageaccount==="") { | |
this.tableSvc =azure.createTableService().withFilter(new azure.ExponentialRetryPolicyFilter()); | |
} else { | |
this.tableSvc = azure.createTableService(this.storageaccount, this.storagekey).withFilter(new azure.ExponentialRetryPolicyFilter()); | |
} | |
var node = this; | |
//create table | |
this.tableSvc.createTableIfNotExists(this.tablename, function callBack(error) { | |
if(error) { | |
node.warn("Error creating table: " + error); | |
} | |
}); | |
this.on("input",function(msg) { | |
var entity = msg; | |
//delete req/res since they are not needed for storing to table | |
delete entity['req']; | |
delete entity['res']; | |
//If there is not already a Partion and Row key, add them based on | |
//datetime | |
if(!('PartitionKey' in entity) && !('RowKey' in entity)) { | |
var d = new Date; | |
var month = d.getUTCMonth(); | |
var day = d.getUTCDate(); | |
var year = d.getUTCFullYear(); | |
entity.PartitionKey = month + '-' + day + '-' + year; | |
entity.RowKey=d.toUTCString(); | |
} | |
this.tableSvc.insertEntity(this.tablename, entity, function callBack(error) { | |
if(error) { | |
node.warn("Error inserting entity: " + error); | |
}; | |
}); | |
}); | |
}; | |
RED.nodes.registerType("azure table",AzureNode); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment