>> Learn how to use
Azure CosmosDB with
the JavaScript SDK in this episode
of Azure Tips and Tricks.
Azure CosmosDB is an awesome
serverless database solution
that easily scales globally.
You can use it with any application
including from JavaScript.
Let's see how that works.
I'll start by creating
a new Azure CosmosDB.
I can do that from here in
the Azure Portal, and here we go.
First, I need to choose my
"Existing Resource Group",
and next I'll give it a name.
Let's call it tipsandtrickscosmos,
and now, I need to choose
the API that I'II use.
I'll leave it at "Core SQL",
but there are lots of
other options that you can use.
The API that you choose
determines the way that
you talk to CosmosDB.
Also, I'll choose another location,
and finally, I will
disable "Geo-Redundancy".
We don't need to replicate all data
geographically for
this demo, and that's it.
"Review and Create" and "Create".
This will take a while,
so I'll skip to when it's done.
Here is the Azure CosmosDB.
A CosmosDB can have
multiple databases that have
collections that in turn have
documents that contain the data.
Let's create a new collection.
This also creates
a new database for us,
so let's put in a "Database
id" and also a name
for the "Collection",
and finally a "Partition key".
CosmosDB uses this to scale
its performance. That's it.
Okay. Now, let's create
some documents that will
query later from JavaScript.
So new document, and
I'll put in this.
The documents are all
described in JSON format.
Okay and let's add another one
and paste in the values,
and that's it.
All right. Let's move on.
I'm going to create a Node.js
application that will
talk to the CosmosDB.
Before I can do that, I need to
install "Node" on my local PC.
You can do that from this URL and
follow the simple
installation instructions.
I'll skip to when it's done.
Now, I'm in Visual Studio Code
and I've opened
a folder called "Node",
and there's nothing in there yet.
First, I will open
a "Terminal" window and now
I'll install a reference to
the CosmosDB for JavaScript,
and there it is.
Next, I'll create a new file
and I will call it
"App.js" I've already prepared
the code, which is this.
This code works with
the CosmosDB SDK.
We'll fill these with
the actual values from our CosmosDB,
and it will execute
the query with this code.
Once it is done, it will loop through
the results and write
the results to the console.
All right. Let's fill in the values.
To make it easy, I've
split the screen between
Visual Studio Code
and the Azure Portal.
The first value is
the Cosmos DB endpoint.
That is this value,
and next is the key that we
need to access the CosmosDB.
Finally, we need to fill in
the database and collection name.
We can find those in
the "Data Explorer" window,
that is tipsdatabase and
tipscollection. Okay. That's it.
Now, back to the "Terminal"
and from here,
I can run the application
by running node App.js,
and there it is.
This is the JSON of the documents
that we've put into the CosmosDB.
You've just seen how easy it is
to use CosmosDB from JavaScript.
The CosmosDB SDK for JavaScript
makes it very easy to use
CosmosDB from your Node.js and
front-end applications.
Go check it out.
