• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Using Azure DocDB with Node.js

Weyzox

Virtual Reality Engineer
W Rep
0
0
0
Rep
0
W Vouches
0
0
0
Vouches
0
Posts
152
Likes
128
Bits
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:

  1. const

    endpoint =

    'https://album-docdb.documents.azure.com:443/'

    ;

  2. const

    authKey =

    'PD2vfKJjiq64wGgfY7FnjJ5kTqVpqSOoZqPGkgtKzLcgS6'

    +
  3. 'FH1NRoXKR6qByMdGZICN0xUA3VwX64fbg84qoaGw'

    ;

With these parameters in place, we can connect to DocDB from Node.js

  1. const

    documentdb =

    require(

    'documentdb'

    )

    ;
  2. const

    DocumentClient =

    documentdb.DocumentClient

    ;

  3. const

    endpoint =

    'https://album-docdb.documents.azure.com:443/'

    ;
  4. const

    authKey =

    'PD2vfKJjiq64wGgfY7FnjJ5kTqVpqSOoZqPGkgtKzLcgS6'

    +
  5. 'FH1NRoXKR6qByMdGZICN0xUA3VwX64fbg84qoaGw'

    ;

  6. const

    client =

    new

    DocumentClient(

    endpoint,

    {
  7. masterKey:

    authKey
  8. }

    )

    ;

Once the connection is established, let's create a database instance from our Node.js code

  1. const

    databaseDefinition =

    {

    id:

    'PhotoAlbum'

    }

    ;
  2. const

    collectionDefinition =

    {

    id:

    'AlbumUsers'

    }

    ;

  3. client.createDatabase

    (

    databaseDefinition,

    (

    err,

    db)

    =>

    {
  4. if

    (

    err)

    {
  5. console.log

    (

    err,

    err.stack

    )

    ;
  6. return

    ;
  7. }
  8. console.log

    (

    'DB created!'

    )

    ;

  9. client.createCollection

    (

    db._self,

    collectionDefinition,

    (

    err,

    col)

    =>

    {
  10. if

    (

    err)

    {
  11. console.log

    (

    err,

    err.stack

    )

    ;
  12. return

    ;
  13. }
  14. console.log

    (

    'Collection created!'

    )

    ;

  15. /* More code to come here */

  16. }

    )

    ;
  17. }

    )

    ;

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:

  1. console.log

    (

    'Collection created!'

    )

    ;

  2. // New code goes here
  3. const

    user =

    {
  4. id:

    'johnny'

    ,
  5. email:

    '[email protected]'

    ,
  6. pass:

    'pass'

    ,
  7. firstName:

    'Johnny'

    ,
  8. lastName:

    'Cage'
  9. }

    ;

  10. client.createDocument

    (

    col._self,

    sampleDocument,

    (

    err,

    doc)

    =>

    {
  11. if

    (

    err)

    {
  12. console.log

    (

    err,

    err.stack

    )

    ;
  13. return

    ;
  14. }
  15. 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:

  1. const

    query =

    'SELECT * FROM docs d'

    ;

  2. client.queryDocuments

    (

    col._self,

    query)

    .toArray

    (

    (

    err,

    res)

    =>

    {
  3. if

    (

    err)

    {
  4. console.log

    (

    err,

    err.stack

    )

    ;
  5. return

    ;
  6. }
  7. console.log

    (

    res)

    ;
  8. }

    )

    ;

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!

  1. const

    documentdb =

    require(

    'documentdb'

    )

    ;

  2. const

    DocumentClient =

    documentdb.DocumentClient

    ;

  3. const

    endpoint =

    'https://album-docdb.documents.azure.com:443/'

    ;

  4. const

    authKey =

    'PD2vfKJjiq64wGgfY7FnjJ7kTqVpqSOoZqPGkgtKzLcgS6'

    +
  5. 'FH1NRoXKR6qByMdGZICN0xUA3VwX64fbg84qoaGw'

    ;

  6. const

    client =

    new

    DocumentClient(

    endpoint,

    {
  7. masterKey:

    authKey
  8. }

    )

    ;

  9. const

    databaseDefinition =

    {

    id:

    'PhotoAlbum'

    }

    ;
  10. const

    collectionDefinition =

    {

    id:

    'AlbumUsers'

    }

    ;
  11. const

    user =

    {
  12. id:

    'johnny'

    ,
  13. email:

    '[email protected]'

    ,
  14. pass:

    'pass'

    ,
  15. firstName:

    'Johnny'

    ,
  16. lastName:

    'Cage'
  17. }

    ;


  18. client.createDatabase

    (

    databaseDefinition,

    (

    err,

    db)

    =>

    {
  19. if

    (

    err)

    {
  20. console.log

    (

    err,

    err.stack

    )

    ;
  21. return

    ;
  22. }
  23. console.log

    (

    'DB created!'

    )

    ;

  24. client.createCollection

    (

    db._self,

    collectionDefinition,

    (

    err,

    col)

    =>

    {

  25. if

    (

    err)

    {
  26. console.log

    (

    err,

    err.stack

    )

    ;
  27. return

    ;
  28. }
  29. console.log

    (

    'Collection created!'

    )

    ;

  30. client.createDocument

    (

    col._self,

    user,

    (

    err,

    doc)

    =>

    {
  31. if

    (

    err)

    {
  32. console.log

    (

    err,

    err.stack

    )

    ;
  33. return

    ;
  34. }
  35. console.log

    (

    'Document created!'

    )

    ;

  36. const

    query =

    'SELECT * FROM docs d'

    ;

  37. client.queryDocuments

    (

    col._self,

    query)

    .toArray

    (

    (

    err,

    res)

    =>

    {
  38. if

    (

    err)

    {
  39. console.log

    (

    err,

    err.stack

    )

    ;
  40. return

    ;
  41. }
  42. console.log

    (

    res)

    ;
  43. }

    )

    ;
  44. }

    )

    ;

  45. }

    )

    ;
  46. }

    )

    ;


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,508

356,407

356,420

Top