Tuesday, August 21, 2018

Azure CosmosDB JavaScript SDK 2.0: Public Preview

The Azure Cosmos DB team has released the latest version (version 2.0 RC) of the JavaScript SDK for SQL API, now in public preview!
It is available in npm now and install it to your project using following command. You can get the latest version through npm

npm install @azure/cosmos

Cosmos DB is
A globally distributed, multi-model database service. It offers turnkey global distribution, guarantees single-digit millisecond latencies at the 99th percentile, and elastic scaling of throughput and storage.
Microsoft supports a JavaScript SDK For the SQL API to enable development against Azure Cosmos DB from JavaScript and Node.js projects. Now the Version 2.0 of the SDK is written completely in Typescript, and we’ve redesigned the object model and added support for promises as well.

What’s Inside

Microsoft has redesigned the object model as of the user’s request and it’s so much easy to use with Cosmos DB. And it is totally written in Typescript.
If you are an existing user of the API, you’ll noticed that the entire API surface hangs off DocumentDBClient. While the previous design makes it easy to find the entry point for methods, it also came at the cost of a cluttered IntelliSense experience.    

Previously it was difficult to do operations off databases, collections, or documents since each method needed to reference the URL of that resource. 
As a result, Microsoft has created a new top level CosmosClient class to replace DocumentDBClient, and split up its methods into modular Database, Container, and Items classes.
For example, in the new SDK, you can create a new database, container, and add an item to it, all in 10 lines of code!

This is called a “builder” pattern, and it allows developers to reference resources based on the resource hierarchy of Cosmos DB, which is similar to the way your brain thinks about Cosmos DB. For example, to create an item, we first reference its database and container, and call items.create().

Containers and items

The latest introduce to CosmosDB SDk is Containers and items. Since CosmosDB Supports multiple API models, this concept has introduced and it replaces the previous Collection and Document concepts. Simply previous “Collection” now known as “Container.”
Single account can have one or more databases, and a database consists of one or more containers. Depending on the API, the container is projected as either a collection (SQL or Mongo API), graph (Gremlin API), or table (Tables API)

It Supports for promises.

Finally, developers can get the advantage of promises and so you no longer have write custom code to wrap the SDK yourself. Now, you can use async/await directly against the SDK
Previously you would have to do something like following code to create a database, collection, and add a document in the SDK.

Now users have await calls and simply use it for your synchronous operations. And by using createIfNotExists() for databases and containers, which wraps the logic to read the database, check the status code, and create it if it doesn’t exist.
Here is the new API Operations to create a database, collection, and add a document in the SDK.

Happy Coding with CosmosDB API

No comments:

Post a Comment