Page 14 - MSDN Magazine, June 2018
P. 14

POST request using this URL. In the body section you can paste in the JSON listed in Figure 4 and then send the request. Figure 5 shows my Postman UI, with the request URL and body displayed, along with the response.
Verifying the Insert
While the HTTP response in Figure 5 does say that all went well, it’s nice to actually see the data that was sent to the database in the cloud. And thanks to the Azure Cosmos DB extension, you can verify that directly in Visual Studio Code. First, let’s be sure that the function app stops running. In the terminal window, press CTRL+C to shut down the host. You may need to press CTRL+C a second time to get the prompt back in the terminal.
Now, in the Cosmos DB extension, expand the account, the database and the collection. You may need to refresh the view with the refresh icon on the extension’s pane. Within the collection, you can see the document that was just added. Select that to open it in the editor where you’ll see not just the data you added, but the metadata added by Azure Cosmos DB, as shown in Figure 6.
Next Steps
The download that accompanies this article also includes two more functions I created to replace the other methods from the original Node.js API. One is to return data filtered by a name part and the other to return data based on the id. With these, rather than having output binding to Cosmos DB, there’s an input binding that defines the database query using the SQL API. The HTTP response spits out the JSON representation of the query results.
The context being passed in to the function is used by the runtime to pass data in and out of the function.
Once you’ve tested and debugged your Azure Functions in Visual Studio Code, you can also use the extension to publish the functions to your Azure account. You can learn more about that from the extension’s ReadMe.
I recommend taking a look at the download from my original article, which is also in a GitHub repository at github.com/julielerman/ AureliaDocDB. The API is in the mod- els folder and, as you’ll see, there’s a lot more code involved with mak- ing read and write calls into the DocumentDB than for the Azure Functions, thanks to its integ- ration with Cosmos DB. I think in my coding future, I’ll always consider Azure Functions as the first line of defense when it’s time to write another Web API! n
Julie lerman is a Microsoft Regional Direc- tor, Microsoft MVP, software team coach and consultant who lives in the hills of Ver- mont. You can find her presenting on data access and other topics at user groups and conferences around the world. She blogs at the thedatafarm.com/blog and is the author of “Programming Entity Framework,” as well as a Code First and a DbContext edition, all from O’Reilly Media. Follow her on Twitter: @julielerman and see her Pluralsight courses at juliel.me/PS-Videos.
Thanks to the following Microsoft technical expert for reviewing this article: Jeff Hollan
Data Points
Figure 5 Creating a Request to Call the AddNinjaDocuments Function
Figure 6 The New Document as Displayed by the Cosmos DB Extension 10 msdn magazine


































































































   12   13   14   15   16