From b0a9dc863c5a869d55582836a53b466e89aeece7 Mon Sep 17 00:00:00 2001 From: My Ho Date: Sat, 5 Oct 2019 21:11:55 -0400 Subject: [PATCH] doc: add cosmosdb events doc --- docs/providers/azure/events/cosmosdb.md | 90 +++++++++++++++++++++++++ docs/providers/azure/events/other.md | 41 +---------- 2 files changed, 91 insertions(+), 40 deletions(-) create mode 100644 docs/providers/azure/events/cosmosdb.md diff --git a/docs/providers/azure/events/cosmosdb.md b/docs/providers/azure/events/cosmosdb.md new file mode 100644 index 000000000..16c824d2e --- /dev/null +++ b/docs/providers/azure/events/cosmosdb.md @@ -0,0 +1,90 @@ + + + + +### [Read this on the main serverless docs site](https://www.serverless.com/framework/docs/providers/azure/events/cosmosdb) + + + +# 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](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2). + +# 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](https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2#output---javascript-examples) + +## Serverless.yml + +```yml +# serverless.yml + +functions: + cosmos: + handler: src/handlers/cosmos.write + events: + - http: true + x-azure-settings: + methods: + - POST + authLevel: anonymous + - cosmosDB: + x-azure-settings: + 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 + +```json +{ + "name": "John Henry", + "employeeId": "123456", + "address": "A town nearby" +} +``` + +## Handler + +```javascript +// 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'); +}; +``` diff --git a/docs/providers/azure/events/other.md b/docs/providers/azure/events/other.md index 9da75deae..46c697007 100644 --- a/docs/providers/azure/events/other.md +++ b/docs/providers/azure/events/other.md @@ -1,7 +1,7 @@ @@ -21,42 +21,3 @@ These work by setting the direction explicitly. The properties go under the You can learn about all the bindings Azure has to offer here on the [official documentation](https://docs.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings). - -## Example - -This is an example of outputting data to Document DB. - -```yml -# serverless.yml - -functions: - example: - handler: handler.hello - events: - - queue: hello - x-azure-settings: - name: item #, default - "myQueueItem", specifies which name is available on `context.bindings` - connection: AzureWebJobsStorage #, default - "AzureWebJobsStorage", environment variable which contains Storage Account Connection String - - documentDB: - x-azure-settings: - name: record # Name of input parameter in function signature>", - databaseName: myDocs # "", - collectionName: todo # "", - createIfNotExists: true - connection: docDBAppSetting # "", - direction: out -``` - -```javascript -// handler.js - -'use strict'; - -module.exports.hello = function(context, item) { - context.log('Received item: ${item}'); - context.bindings.record = { - hello: 'world', - }; - context.done(); -}; -```