Giter VIP home page Giter VIP logo

azure.functions.cosmos.extension's Introduction

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);
            }
        }
    }
}

azure.functions.cosmos.extension's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.