Page 12 - MSDN Magazine, June 2018
P. 12
The setting goes in the Values section of local. settings.json.Youcancopytheconnectionstring by right-clicking the Cosmos DB account in the Cosmos DB extension pane and then pasting it into the json file. The string will start with:
"mydbconnection": "AccountEndpoint=https://yourdb.documents.azure.com/
In an upcoming enhancement to the Azure
Function Core Tools, the presence of the
cosmosDB type in the function.json file will
trigger the extension to automatically install a
package with the logic needed to run and debug
your project locally. At this time, though, you’ll need to install this package manually. Let’s do that now.
In the terminal window, be sure you’re pointed to the root folder, NinjaFunctions, and enter the following Azure Functions CLI command: func extensions install -p Microsoft.Azure.WebJobs.Extensions.CosmosDb -v 3.0.0-beta7 Note that I’m installing the beta version that’s current as I’m writ- ing this article. You can check its NuGet page for the latest version if you do have to install it manually (bit.ly/2EyLNCw). This will create a new folder called functions-extensions in your project (see Figure 2). The folder contains a .NET Standard 2.0 project, which is driven by the project file, extensions.csproj. The csproj code listing is in Figure 3.
Adding the Tiny Bit of Code
Let’s get back to the AddNinjaDocuments function. The function.json file is all set, so the next step is to complete the index.js file, which is where the function’s logic lives. You can delete all of the default sample code. All you really need for the function is code to bind the incoming JSON to the function’s output; in other words, what gets sent to the Cosmos DB collection:
module.exports = function (context, req) { context.bindings.outputDocument=req.body; context.done();
};
But it’s helpful to have a little bit of debugging info. So, rather than the pure minimalist version, replace the entire contents of the default sample code with the following:
module.exports = function (context, req) { context.log('HTTP request received.'); try {
context.bindings.outputDocument = req.body; }
catch (error) { context.log(error);
}
context.bindings.res = { status: 201, body: "Insert succeeded." }; context.done();
};
All you really need for the function is code to bind the incoming JSON to the function’s output; in other words, what gets sent to the
Cosmos DB collection.
The JavaScript code is quite different from the C#functioncodeIwroteinthepreviousarticles. You can learn about the structure of JavaScript in Azure Functions at bit.ly/2GQ9eJt.
The context being passed in to the function is used by the runtime to pass data in and out of the function. I named the Cosmos DB bind- ing “outputDocument” in the function.json configuration. Now I’m setting that binding to whatever body is encapsulated in the incoming HTTP request. The template created a binding named res for the HTTP response, which I’m
using to relay the success of the function. The context.done method signals to the runtime that the function is complete.
Running the Function in Visual Studio Code
So, that’s all there is to the function! Everything else is taken care of by the application settings, the function configuration and Azure Functions APIs. You can go ahead and run the function in Visual Studio Code. While it’s absolutely possible to debug, set break- points and explore variables, let’s just run the function app, which you can do in the terminal with the command:
func start
This will display the brightly colored Azure Functions logo in the terminal window and then output some processing info. At the end of all this output, you’ll see the URL where the function is running. If there are multiple functions in your project, the URLs will be listed separately for each. In Postman or Fiddler, build a
Figure 3 The extensions.csproj Folder
with References to the Cosmos DB Extension
Figure 2 The New Functions- Extensions Folder to House the Cosmos DB Extension
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <WarningsAsErrors></WarningsAsErrors> <DefaultItemExcludes>**</DefaultItemExcludes>
</PropertyGroup> <ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDb" Version="3.0.0-beta7" />
<PackageReference Include="Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator" Version="1.0.0-beta2" />
</ItemGroup> </Project>
Figure 4 JSON Code for a Document To Be Inserted into the Database
{
"Name": "Kacy Catanzaro", "ServedInOniwaban": false, "Clan": "American Ninja Warriors", "Equipment": [
{
"EquipmentName": "Muscles", "EquipmentType": "Tool"
}, {
"EquipmentName": "Spunk",
"EquipmentType": "Tool" }
],
"DateOfBirth": "1/14/1990" }
8 msdn magazine
Data Points