Weyzox
Virtual Reality Engineer
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2
900 XP
Moving your project to a cloud might be a difficult task especially since there are so many different cloud services out there. AWS is the most popular one and Azure is a very close competitor. In this article I will show you how to start using Azure, namely its NoSQL database service - DocDB.
NOTE: Azure is a great platform, however it has significantly less tutorials and examples available online (compared to AWS).
Before we start to write the code we will need to prepare the dependencies. Install the following moudule with npm:
npm install --save documentdb
Unlike AWS that keeps the code for all the services in one folder, Azure separates the clients into a smaller dedicated NPM modules.
In order to start using DocDB you'll have to do a bit of preparation. Create your database instance from Azure control panel, and find an "Authorization Key". Your credentials should look like the following:
With these parameters in place, we can connect to DocDB from Node.js
Once the connection is established, let's create a database instance from our Node.js code
As you see, it is not hard at all to create a DB and collection with DocDB. The only issue that I see here is that we're building a "Pyramid of Doom" - we got a callback within the callback within the callback.
Now we have a database and a collection, let's add a document. We'll create our first user by inserting more code after collection creation:
DocDB is schemaless, just like most NoSQL Document-oriented databases. This means that we don't have to specify "schema", a structure of the objects that we will insert into the collection *before* creating this collection. This feature greatly simplifies the development and release of new version. Imagine that in the next release we decide to include the optoinal "about" text to each user. Only think that we'll have to do in this case is to add that key to the object. Existing objects will continue without "about" text, while the new ones will be created with it.
Finally, let's see how to find our object in DB:
Query language resembles that of MongoDB. In fact, these databases (DocDB and MongoDB) are very similar. Microsoft even released MongoDB driver for DocumentDB, meaning that if your code relies on Mongo, all you have to do is replace the driver, which will make the transition very easy.
For your reference, here is the complete source code that we have created in this post. Don't forget to replace the keys and endpoint names so that this code connects to your own database!
Download
NOTE: Azure is a great platform, however it has significantly less tutorials and examples available online (compared to AWS).
Before we start to write the code we will need to prepare the dependencies. Install the following moudule with npm:
npm install --save documentdb
Unlike AWS that keeps the code for all the services in one folder, Azure separates the clients into a smaller dedicated NPM modules.
In order to start using DocDB you'll have to do a bit of preparation. Create your database instance from Azure control panel, and find an "Authorization Key". Your credentials should look like the following:
- const
endpoint =
'https://album-docdb.documents.azure.com:443/'
;
- const
authKey =
'PD2vfKJjiq64wGgfY7FnjJ5kTqVpqSOoZqPGkgtKzLcgS6'
+
- 'FH1NRoXKR6qByMdGZICN0xUA3VwX64fbg84qoaGw'
;
With these parameters in place, we can connect to DocDB from Node.js
- const
documentdb =
require(
'documentdb'
)
;
- const
DocumentClient =
documentdb.DocumentClient
;
- const
endpoint =
'https://album-docdb.documents.azure.com:443/'
;
- const
authKey =
'PD2vfKJjiq64wGgfY7FnjJ5kTqVpqSOoZqPGkgtKzLcgS6'
+
- 'FH1NRoXKR6qByMdGZICN0xUA3VwX64fbg84qoaGw'
;
- const
client =
new
DocumentClient(
endpoint,
{
- masterKey:
authKey
- }
)
;
Once the connection is established, let's create a database instance from our Node.js code
- const
databaseDefinition =
{
id:
'PhotoAlbum'
}
;
- const
collectionDefinition =
{
id:
'AlbumUsers'
}
;
- client.createDatabase
(
databaseDefinition,
(
err,
db)
=>
{
- if
(
err)
{
- console.log
(
err,
err.stack
)
;
- return
;
- }
- console.log
(
'DB created!'
)
;
- client.createCollection
(
db._self,
collectionDefinition,
(
err,
col)
=>
{
- if
(
err)
{
- console.log
(
err,
err.stack
)
;
- return
;
- }
- console.log
(
'Collection created!'
)
;
- /* More code to come here */
- }
)
;
- }
)
;
As you see, it is not hard at all to create a DB and collection with DocDB. The only issue that I see here is that we're building a "Pyramid of Doom" - we got a callback within the callback within the callback.
Now we have a database and a collection, let's add a document. We'll create our first user by inserting more code after collection creation:
- console.log
(
'Collection created!'
)
;
- // New code goes here
- const
user =
{
- id:
'johnny'
,
- email:
'[email protected]'
,
- pass:
'pass'
,
- firstName:
'Johnny'
,
- lastName:
'Cage'
- }
;
- client.createDocument
(
col._self,
sampleDocument,
(
err,
doc)
=>
{
- if
(
err)
{
- console.log
(
err,
err.stack
)
;
- return
;
- }
- console.log
(
'Document created!'
)
;
DocDB is schemaless, just like most NoSQL Document-oriented databases. This means that we don't have to specify "schema", a structure of the objects that we will insert into the collection *before* creating this collection. This feature greatly simplifies the development and release of new version. Imagine that in the next release we decide to include the optoinal "about" text to each user. Only think that we'll have to do in this case is to add that key to the object. Existing objects will continue without "about" text, while the new ones will be created with it.
Finally, let's see how to find our object in DB:
- const
query =
'SELECT * FROM docs d'
;
- client.queryDocuments
(
col._self,
query)
.toArray
(
(
err,
res)
=>
{
- if
(
err)
{
- console.log
(
err,
err.stack
)
;
- return
;
- }
- console.log
(
res)
;
- }
)
;
Query language resembles that of MongoDB. In fact, these databases (DocDB and MongoDB) are very similar. Microsoft even released MongoDB driver for DocumentDB, meaning that if your code relies on Mongo, all you have to do is replace the driver, which will make the transition very easy.
For your reference, here is the complete source code that we have created in this post. Don't forget to replace the keys and endpoint names so that this code connects to your own database!
- const
documentdb =
require(
'documentdb'
)
;
- const
DocumentClient =
documentdb.DocumentClient
;
- const
endpoint =
'https://album-docdb.documents.azure.com:443/'
;
- const
authKey =
'PD2vfKJjiq64wGgfY7FnjJ7kTqVpqSOoZqPGkgtKzLcgS6'
+
- 'FH1NRoXKR6qByMdGZICN0xUA3VwX64fbg84qoaGw'
;
- const
client =
new
DocumentClient(
endpoint,
{
- masterKey:
authKey
- }
)
;
- const
databaseDefinition =
{
id:
'PhotoAlbum'
}
;
- const
collectionDefinition =
{
id:
'AlbumUsers'
}
;
- const
user =
{
- id:
'johnny'
,
- email:
'[email protected]'
,
- pass:
'pass'
,
- firstName:
'Johnny'
,
- lastName:
'Cage'
- }
;
- client.createDatabase
(
databaseDefinition,
(
err,
db)
=>
{
- if
(
err)
{
- console.log
(
err,
err.stack
)
;
- return
;
- }
- console.log
(
'DB created!'
)
;
- client.createCollection
(
db._self,
collectionDefinition,
(
err,
col)
=>
{
- if
(
err)
{
- console.log
(
err,
err.stack
)
;
- return
;
- }
- console.log
(
'Collection created!'
)
;
- client.createDocument
(
col._self,
user,
(
err,
doc)
=>
{
- if
(
err)
{
- console.log
(
err,
err.stack
)
;
- return
;
- }
- console.log
(
'Document created!'
)
;
- const
query =
'SELECT * FROM docs d'
;
- client.queryDocuments
(
col._self,
query)
.toArray
(
(
err,
res)
=>
{
- if
(
err)
{
- console.log
(
err,
err.stack
)
;
- return
;
- }
- console.log
(
res)
;
- }
)
;
- }
)
;
- }
)
;
- }
)
;
Download
You must upgrade your account or reply in the thread to view the hidden content.