Skip to content

This extension provides functionality for using the latest Azure.Cosmos .Net SDK for Cosmos DB bindings in Azure Functions.

License

Notifications You must be signed in to change notification settings

mobsites/Azure.Functions.Cosmos.Extension

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Azure.Functions.Cosmos.Extension

This extension provides functionality for using the latest Azure.Cosmos .Net SDK for Cosmos DB bindings in Azure Functions.

NOTE: CURRENTLY IN PREVIEW

Dependencies

.NETStandard 2.0
  • Azure.Cosmos (>= 4.0.0-preview3)
  • Microsoft.Azure.WebJobs (>= 3.0.14)
  • Microsoft.CSharp (>= 4.7.0)

Getting Started

Trigger

This extension only supports input and output bindings. Trigger support is forthcoming.

Input & Output Bindings

The Cosmos input and output binding examples shown in Microsoft docs are still relevant here.

Just use the new attribute name Cosmos:

[FunctionName("ReadData")]
public static IActionResult Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
    [Cosmos(
        databaseName: "Test", 
        containerName: "Data",
        PartitionKey = "SampleData",
        SqlQuery = "SELECT * FROM c",
        ConnectionStringSetting = "Cosmos")] IEnumerable<SampleData> sampleData,
    ILogger log)

Container Parameter Support

You can now specify a parameter of type CosmosContainer. And if you want to make sure it is created if it doesn't exist, just set CreateIfNotExists option to true:

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Azure.Cosmos;

namespace Azure.Functions.Cosmos.Extension.Sample
{
    public static class CreateContainer
    {
        [FunctionName("CreateContainer")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            [Cosmos(
                databaseName: "Test", 
                containerName: "Data",
                PartitionKey = "Discriminator",
                ConnectionStringSetting = "Cosmos",
                CreateIfNotExists = true)] CosmosContainer DataContainer,
            ILogger log)
        {
            try
            {
                CosmosContainer container = await DataContainer.ReadContainerAsync();
                log.LogInformation(container.Id);
                return new OkObjectResult(container.Id);
            }
            catch (CosmosException cex)
            {
                log.LogError(cex, cex.Message);
                return new BadRequestObjectResult(cex.Message);
            }
        }
    }
}

About

This extension provides functionality for using the latest Azure.Cosmos .Net SDK for Cosmos DB bindings in Azure Functions.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%