Skip to content

asos-martinsmith/azure-functions-cosmosdb-extension

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

80 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Azure WebJobs CosmosDB Extensions

This repo contains binding extensions for the Azure WebJobs SDK intended for working with Azure CosmosDB's various APIs. See the Azure WebJobs SDK repo for more information on WebJobs.

NoSQL Api

The extension for working with the NoSQL API is located in the general WebJobs Extension repo

MongoDB Api

To configure the binding, add the Mongo connection string as an app setting or environment variable using the setting name CosmosDB. The name of the setting can be changed with the ConnectionStringKey property of the binding attribute.

Reading Documents

The extension currently supports getting either a database or a collection.

public void Inputs(
    [CosmosDBMongo(DatabaseName)] IMongoDatabase db,
    [CosmosDBMongo(DatabaseName, CollectionName)] IMongoCollection<BsonDocument> coll)
{
    Assert.IsNotNull(db);
    Assert.IsNotNull(coll);
}

Writing Documents

In this example, the newItem object is upserted into the ItemCollection collection of the ItemDb database.

public static void InsertDocument(
    [QueueTrigger("sample")] QueueData trigger,
    [CosmosDBMongo("DatabaseName", "ItemCollection")] out BsonDocument newItem)
{
    newItem = new BsonDocument();
}

Simple C# objects can also be automatically converted into BSON. The following will write a simple document to the service.

public static void InsertDocument(
    [QueueTrigger("sample")] QueueData trigger,
    [CosmosDBMongo("DatabaseName", "ItemCollection")] out ItemDoc newItem)
{
    newItem = new ItemDoc()
    {
        Text = "sample text"
    };
}

If you need more control, you can also specify a parameter of type IMongoClient. The following example uses MongoClient to query for all documents.

public static void DocumentClient(
    [QueueTrigger("sample")] QueueData trigger,
    [CosmosDBMongo] IMongoClient client,
    TraceWriter log)
{
    var documents = client.getDatabase("Database").getCollection<BsonDocument>("Collection").find();

    foreach (BsonDocument d in documents)
    {
        log.Info(d);
    }
}

Triggers

There is a separate attribute for writing functions which trigger on changes to a Cosmos Mongo collection. CosmosDBMongoTrigger has a few additional configurations available compared to the basic attribute. Because the trigger needs to keep track of which data has already been seen, it uses an additional collection. By default, it uses a collection named leases in the same database as the source collection. However LeaseConnectionStringKey, LeaseDatabaseName and LeaseCollectionName can be used if desired to place it elsewhere.

public static void Trigger(
    [CosmosDBMongoTrigger(DatabaseName, MonitoredCollectionName)] IEnumerable<BsonDocument> docs,
    ILogger logger)
{
    foreach (BsonDocument doc in docs)
    {
        logger.LogInformation("Doc triggered");
    }
}

Private Preview

Until the extension becomes available in the portal and extension bundles, it can be used by directly installing the extension. Either use whatever tool/template to create a CosmosDB Trigger and modify it as follows, or use the example projects for C# and Typescript.

  1. Remove the extension bundle from host.json if it exists.
  2. Run func extensions install --package Microsoft.Azure.WebJobs.Extensions.CosmosDb.Mongo --version 1.0.2
  3. Modify the function.json and/or annotations to
    • "type":"cosmosDBMongoTrigger"
    • "datatype":"binary"
    • createLeaseCollectionIfNotExists can be removed
    • Rename connectionStringSetting to connectionStringKey
  4. Add the Mongo connection string as an app setting or environment variable using the setting name CosmosDB
  5. Change the function file itself to take a language specific binary type and deserialize it with the language specific BSON driver.

About

Extensions to support CosmosDB functionality on Azure Functions

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 99.6%
  • TypeScript 0.4%