KTL SOLUTIONS

Document DB Client-side and Server-side Integrations

Share this post

So far, we have seen how to create a DocumentDB database, collections and documents. Now that we have saved data in DocumentDB, we need means to interact with it from outside. May be through an application that integrates with DocumentDB to retrieve and create documents. Such integrations that happen on the client such as an application are called as Client-side integrations.

Document DB offers a couple of options to interact using REST/HTTP API or SDK. REST API can be used just like any other REST API by making a HTTP request to the resource and providing valid authentication in the header. SDK’s are available for most platforms, you can download the SDK for .Net from NuGet.

Server-side integrations reside on the DocumentDB server itself. There are 3 forms of server-side integrations stored procedures, triggers and user-defined functions. In DocumentDB, server-side logic is written in JavaScript instead of SQL, which makes it a perfect companion to deal with JSON objects.

Integrate with DocumentDB using .Net and C# SDK

First step is to establish connection with DocumentDB, and for that we will need an endpoint and a key. The endpoint is the URL to the DocumentDB account, which has the form of https://accountname.documents.azure.com. The key contains your credentials to DocumentDB. There are two types of keys, master key and resource tokens. The master key grants total access to DocumentDB and you can do pretty much anything with it. You can think of it as the ‘sa’ account in the SQL Server world. It provides full access to the entire DocumentDB account and should not be distributed to end user machines. Resource tokens are based on user permissions and can only access specific resources.

Open Visual Studio 2017, create a project, install DocumentDB .Net SDK from NuGet. In your code,

Create an instance of DocumentClient by supplying an endpoint and a key. Once you have an instance, you can invoke methods to access DocumentDB resources. At this point, you can create, modify, and delete databases, collections, documents, and other resources.

Stored Procedures

Stored procedures are nothing but JavaScript functions. These functions uses ‘context’ to access collections. Input values are passed as function arguments. Inside Azure Portal, use Data Explorer and click ellipses (…) next to Items, click ‘New Stored Procedure’. Here is a simple stored procedure that returns a response message.

To execute a Stored Procedure, click Script Explorer, pick the stored procedure and click ‘Save & Execute’.

Triggers

Triggers are also JavaScript functions associated with a document action such as ‘create’ or ‘update’. Trigger word is a little misleading as they are not automatically triggered like in a traditional relational database.

User Defined Functions

User-defined functions are the third type of JavaScript server-side functions that are a great way to write custom business logic that can be called from within queries. User-defined functions can’t make changes to the database, they’re read-only.

In today’s digital world, we need enterprise level applications that work on web, mobile, Internet of Things (IoT), etc. And we need these applications to run faster and at greater scale. NoSQL fits the bill and is increasingly the preferred database to power up big enterprise level applications. Having said that, NoSQL is not as robust as SQL and does some compromise on ACID properties, so take it with a grain of salt. Also, it is very common for NoSQL and SQL to co-exists, making available the best of both worlds.

Hope you enjoyed reading my multi-series blog on NoSQL. Thanks all.

Related Posts

Checking Your CMMC Progress

Written by Alec Toloczko With Cybersecurity Maturity Model Certification (CMMC) requirements on the horizon, it’s crucial for organizations handling Controlled Unclassified Information (CUI) to adhere

Read More »