Hi I'm John Papa.
Let's create a node application that talks to Azure's Cosmos DB
using the new SQL API, following the quick start here.
You can use this link to follow these steps https://aka.ms/cosmos-quickstart
I'm going to start by creating my own directory here
I call it cosmos-sql-api-quickstart, and then I navigate inside of it
Then I'm going to refresh my instance of Visual Studio Code
so it is pointing to the folder that I just went to
Next I'm going to open up the terminal and clear it out
and go into the Azure extensions
where you can see I have a few extensions. The one we are going to play with is Cosmos DB
We'll right click and create an account
And I'll name my account cosmos-sql-api-quickstart
choose SQL and create a new resource group
now you pick the name that you want, as I like to prefix mine with my name
Then I'm going to choose a location that is closest to me. All of this is following the quickstart
Then you'll see that it's creating the Cosmos DB
This will take somewhere between, I don't know, 1 and 3 minutes
So in the meantime, I'm going to create a new file called app.js
and another file called config.js
these are the steps inside setting up your application in the quick start
Now Command J will get rid of the bottom bar, and Command B the left sidebar
Now I'm going to init a new package.json file
This will allow me to save packages as dependencies right to this file.
As you might have guessed, the next step is to install a pacakge
This is going to be the azure cosmos package
This is so we can interact with the SDK to talk to Cosmos DB from our new application
Now we'll go back and we see our Cosmos DB is ready
and we'll enter our endpoint primary key
and here's a trick, you can copy the connection string into your file and paste it
now this is your secure information
so notice that you're seeing my url to my database
and then my account key or password
I'm going to put the endpoint in here
Then I'll put my primary key up here inside the config.primaryKey
these will disappear by the time you try this, so don't try mine
and of course don't share yours
then I copy the next steps which is basically all of the data
that the quick start asks you to enter in
it's sample data for a family
next we're going to export the module called config
now we're going to the app.js file
and we're going to import the cosmos client from the library
then we're going to import the configuration
we're going to set up our client to talk to it
we'll identify the database and container ID
the database is our database and the container is like a table
and then we're going to set up the exit functionality so we can press any key to get out of here
this is some basic housekeeping
and finally we have the create database command at the bottom
now this is the key code. This kicks everything off for us.
now once we have this
we can run it in our terminal by typing node app.js
and then we'll see the output. We created the database, and we read the database.
and there is nothing in it yet
so the next step puts something in it
notice in the tools we can see in the sidebar there is nothing but a family database
next we're going to create a container
once we have the container we'll use create container and read container to read it
and down below in our kickoff code
we'll put in create container and read container
again this is from the quick start
we run it and we see we created the database and the container, and then we are reading the container
so back in the Azure tools,
this time in the family database, once we refresh
you'll see a family container
and there are no documents in it yet
documents are our data
the next step is we can go create an item
again, right from the quick start, we create a family item
it's going to read the data out of config.js
and once we have that function
we'll paste that down under the read container
and create two family items
so now we are inserting Anderson 1 and Wakefield 7
and we go back to our documents and refresh in the Cosmos DB VS Code extension
and we see those, right here
alright, so we keep moving along. once we have that data, what else can we do?
we can actually query our Cosmos DB resources
this is a cool part
even though it's a Mongo database, or document DB, we can write a query with it
using different languages like SQL
so we paste that code in
now we can see we queried for that character
what about updating items?
we can replace items too, so that's the next step in the quick start
we're going to replace an item using this function here
it's going to find this item and change the grade from 5 to 6
we'll place that down here under query container to replace the item and query again
now we can see we created the container and it has grade 6
and last, let's go ahead and delete an item
this is also right from the quick start
simple code to delete an item in the container
and then we'll put that code under query container
it's going to delete the family item called Anderson
we'll run the node app
at the bottom you can see we deleted that item
and of course you can go query this in the Azure tools and check it out
I encourage you to try everything you see right here from the quick start  https://aka.ms/cosmos-quickstart
and if you'd like any help, you can find me on Twitter at @john_papa
Thanks for watching
