Table Read and Insert Function Azure Node.js Easy Tables
Node.js is one one of the the virtually popular growing platforms for development. I started a series of posts dedicated to Node.js , starting with two manufactures most Node.js and Microsoft SQL Server ( yous can run across role ane and role 2 ) . This weblog discusses how y'all can apply Node.js and Microsoft Azure Table Storage . Open source solutions (OSS), are very suitable for the implementation of platform independent and/or deject applications. Node.js is widely used in the actual implementation of such cloud applications. I will try demonstrate in several blogs some details how to employ Node.js with Microsoft Azure Storage .
What is Azure Storage?
Microsoft Azure storage services permit us to store/retrieve the NON RELATIONAL data to/from Microsoft Deject environment. (For relational data, SQL Azure services are used).
In Microsoft Azure Storage, the data can exist stored in 4 different formats (v.i.z. Blobs, Tables and Queues, File Storage (in preview )). The retrieval/storage of the above data is done in RESTful way.
- Hulk storage stores file data. A blob can be any type of text or binary data, such every bit a document, media file, or application installer.
- Table storage stores structured datasets. Table storage is a NoSQL key-attribute information store, which allows for rapid evolution and fast access to large quantities of data.
- Queue storage provides reliable messaging for workflow processing and for advice betwixt components of deject services.
- File storage offers shared storage for legacy applications using the standard SMB 2.i protocol. Azure virtual machines and deject services can share file data across application components via mounted shares, and on-premise applications can admission file information in a share via the File service Rest API.
This article is focused on how to handle Azure Table services with Node.js
Table Storage
The Azure Table storage service stores large amounts of structured data. The service is a NoSQL datastore which accepts authenticated calls from inside and outside the Azure deject.
The Table service contains the following components:
Table Entities:
Table entities represent the units of information stored in a table and are similar to rows in a typical relational database table. Each entity defines a collection of backdrop. Each property is primal/value pair defined by its name, value, and the value'south data type. Entities must define the following iii arrangement properties equally part of the holding collection:
- PartitionKey – The PartitionKey property stores cord values that place the partition that an entity belongs to. This ways that entities with the same PartitionKey values belong in the same partitioning. Partitions, as discussed later, are integral to the scalability of the tabular array.
- RowKey – The RowKey property stores cord values that uniquely identify entities within each partition.
- Timestamp – The Timestamp belongings provides traceability for an entity. A timestamp is a DateTime value that tells you the last time the entity was modified. A timestamp is sometimes referred to as the entity's version. Modifications to timestamps are ignored because the table service maintains the value for this holding during all inserts and update operations.
Consider PartitionKey, RowKey in your design. Call up of PartitionKey and RowKey as being a primary index.
Table Partitions:
Azure Tables use keys that enable efficient querying, and you can utilize one—the PartitionKey—for load balancing when the table service decides it's fourth dimension to spread your table over multiple servers. A tabular array doesn't have a specified schema.
Partitions represent a collection of entities with the same PartitionKey values. Partitions are always served from one partition server and each partition server can serve one or more partitions.
Dealing with an Azure Table Storage
Y'all can utilize different NodeJS packages to handle Azure Tabular array Storage. In this postal service we volition cover azure and azure-table-node Node packages.
- Microsoft Azure SDK for Node.js
It is an official Microsoft Azure SDK for Node.js. This project provides a Node.js bundle that makes it easy to consume and manage Microsoft Azure Services.
The listed snippets below demonstrate how to manage Azure Tabular array service using Microsoft Azure SDK for Node.js.
This is like shooting fish in a barrel to do since the Azure SDK volition wait for credentials using surroundings variables starting time. The magical environs variable names are AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY.
- Create a table service
Set credentials using surroundings variables
1: var azure = require('azure');
2: 3: //using enviroment variables for credentials
4: var tableService = azure.createTableService(); // implicitly use env variables
v: 6: tableService = azure.createTableService( 7: process.env.AZURE_STORAGE_ACCOUNT, eight: procedure.env.AZURE_STORAGE_ACCESS_KEY); // explicit
ix: Gear up credentials explicitly using local variables
one: var accessKey = '[accountKey]';
two: var storageAccount = '[accountName]';
3: four: var tableService = azure.createTableService(
5: accessKey, storageAccount); // explicit - Insert an entity
i: var tableService = azure.createTableService();
2: 3: //insert an entity
4: var task1 = {
five: PartitionKey : 'myPartitionKey',
vi: RowKey: 'i',
7: Description: 'Row clarification',
8: DueDate: new Date(2011, 12, 14, 12)
9: }; ten: 11: tableService.insertEntity('tasktable', task1, role(error){
12: if(!error){
13: // Entity inserted
14: } 15: }); - Query entities
1: //query an entity
2: var tableService = azure.createTableService();
3: tableService.queryEntity('demotable', 'myPartitionKey', 'one', function(mistake, serverEntity){
four: if(!error){
v: // Entity available in serverEntity variable
vi: } 7: }); - azure-table-node:
It is a simplified Azure Tabular array Storage client library for Node.js that supported:
- creating, deleting and listing tables
- creating, updating, querying and deleting entities
- batch operation support
- generating SAS (Shared Access Signature) and using it for hallmark
Lawmaking samples below prove how to use azure-table-node module to work with Azure Table Storage
- Ready Azure Storage credentials
1: var azureTable = require('azure-tabular array-node')
2: three: //set azure storage credentials
4: azureTable.setDefaultClient({ 5: accountUrl: 'http://[accountName].table.cadre.windows.net/',
six: accountName: '[accountName]',
7: accountKey: '[accountKey]'
8: }); - Create an Azure Table
1: //create azure table
two: app.get("/createTable", office (req, res) {
3: iv: var client = azureTable.getDefaultClient();
5: customer.createTable('testtable', part (err, information) {
six: }); 7: eight: client.insertEntity('testtable', {
9: PartitionKey: 'tests',
10: RowKey: '1',
11: value1: 'ABCDEFG'
12: }, function (err, data) {
13: res.write("Got mistake :-( " + err);
14: }); 15: 16: res.cease("Table created.");
17: }); - Display an Azure Table
ane: //display an azure tabular array
two: app.get("/displayTable", function (req, res) {
3: 4: var client = azureTable.getDefaultClient();
5: 6: client.queryEntities('testtable', {
7: query: azureTable.Query.create('PartitionKey', '==', 'tests')
8: 9: }, function (err, data, continuation) {
ten: if (err) {
11: res.writeHead(500, { 'Content-Type': 'text/plain' });
12: res.write("Got fault :-( " + err);
13: res.end("");
fourteen: return;
15: } 16: 17: var json = JSON.stringify(information);
eighteen: res.writeHead(200, { 'Content-Type': 'text/plain' })
19: 20: res.end("Table displayed: " + json);
21: }); 22: 23: });
- List all Azure Tables
1: //list all azure tables
ii: app.become("/listTables", function (req, res) {
3: 4: var client = azureTable.getDefaultClient();
5: half dozen: client.listTables(function (err, data, continuation) {
7: if (err) {
8: res.writeHead(500, { 'Content-Type': 'text/plain' });
9: res.write("Got error :-( " + err);
10: res.end("");
eleven: return;
12: } 13: 14: res.writeHead(200, { 'Content-Type': 'text/plain' })
fifteen: 16: for (var i = 0; i < data.length; i++) {
17: res.write("Table[" + i + "]: " + data[i] + " " );
18: } xix: 20: res.terminate("Tables listed." + data);
21: }); 22: 23: });
- Delete an Azure Table
1: //delete azure tabular array
2: app.get("/deleteTable", office (req, res) {
3: 4: var client = azureTable.getDefaultClient();
5: client.deleteTable('testtable', function (err, data) {
6: }); 7: res.terminate("Tabular array testtable has been deleted.");
8: ix: });
There'due south so much more to larn about Azure Tabular array services and Node. The features and capabilities of table storage continue to grow. This post is just an intro how to start, covering the base cases and most popular Node.js modules for Azure Tables. Information technology will be useful for both – JavaScript developers who don't have feel with Microsoft Azure and Azure developers who accept less experience with JavaScript and/or Node.js.
Developers on the Microsoft Azure Platform should become familiar with how Tabular array Storage works and how information technology differs from the relational databases they are used to. Knowing how tabular array storage works will help you decide if it is a good fit for your particular requirements.
Y'all tin download source code from Git repository .

If you want more information about how to utilize Microsoft Azure Storage & Node.js experience free to contact me at mmateev@infragistics.com
You can acquire more about Node.js , Microsoft Azure and related events like Azure Bootcamp Bulgaria if yous follow us on Twitter @mihailmateev and @Infragistics and stay in bear on Facebook, Google+, LinkedIn and Infragistics Friends User Group !
Source: https://www.infragistics.com/community/blogs/b/mihail_mateev/posts/how-to-manage-microsoft-azure-table-storage-with-node-js
0 Response to "Table Read and Insert Function Azure Node.js Easy Tables"
Post a Comment