#CosmosDB Trigger

The Azure Cosmos DB Trigger uses the Azure Cosmos DB Change Feed to listen for inserts and updates across partitions. The change feed publishes inserts and updates, not deletions.

Full documentation can be found on azure.com.

#Events

This setup describe how to write the data received, when someone accesses the Function App at api/cosmos via a POST request , to Cosmos DB

#Serverless.yml

# serverless.yml

functions:
  cosmos:
    handler: src/handlers/cosmos.write
    events:
      - http: true
        methods:
          - POST
        authLevel: anonymous
      - cosmosDB:
        direction: out
        name: record # name of input parameter in function signature
        databaseName: sampleDB
        collectionName: sampleCollection
        connectionStringSetting: COSMOS_DB_CONNECTION # name of appsetting with the connection string
        createIfNotExists: true # A boolean value to indicate whether the collection is created when it doesn't exist.

#Sample post data

{
  "name": "John Henry",
  "employeeId": "123456",
  "address": "A town nearby"
}

#Handler

// src/handlers/cosmos.js

'use strict';
const uuidv4 = require('uuid/v4');

module.exports.write = async function(context, req) {
  context.log('JavaScript HTTP trigger function processed a request.');

  const input = req.body;

  const timestamp = Date.now();
  const uuid = uuidv4(); //

  const output = JSON.stringify({
    id: uuid,
    name: input.name,
    employeeId: input.employeeId,
    address: input.address,
    timestamp: timestamp,
  });

  context.bindings.record = output;

  context.log('Finish writing to CosmosDB');
};

Have questions?

Head over to the forums to search for your questions and issues or post a new one.