mirror of
https://github.com/serverless/serverless.git
synced 2026-02-01 16:07:28 +00:00
Merge pull request #6794 from mydiemho/myho/addCosmosDBEvent
doc: add cosmosdb events doc
This commit is contained in:
commit
9299738de8
90
docs/providers/azure/events/cosmosdb.md
Normal file
90
docs/providers/azure/events/cosmosdb.md
Normal file
@ -0,0 +1,90 @@
|
||||
<!--
|
||||
title: Serverless Framework - Azure Functions Events - Cosmos DB
|
||||
menuText: Cosmos DB
|
||||
menuOrder: 7
|
||||
description: Setting up Cosmos DB Events with Azure Functions via the Serverless Framework
|
||||
layout: Doc
|
||||
-->
|
||||
|
||||
<!-- DOCS-SITE-LINK:START automatically generated -->
|
||||
|
||||
### [Read this on the main serverless docs site](https://www.serverless.com/framework/docs/providers/azure/events/cosmosdb)
|
||||
|
||||
<!-- DOCS-SITE-LINK:END -->
|
||||
|
||||
# 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');
|
||||
};
|
||||
```
|
||||
@ -1,7 +1,7 @@
|
||||
<!--
|
||||
title: Serverless Framework - Azure Functions Events - Other Bindings
|
||||
menuText: Other Bindings
|
||||
menuOrder: 7
|
||||
menuOrder: 8
|
||||
description: Setting up Other Bindings Events with Azure Functions via the Serverless Framework
|
||||
layout: Doc
|
||||
-->
|
||||
@ -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 #<string>, default - "myQueueItem", specifies which name is available on `context.bindings`
|
||||
connection: AzureWebJobsStorage #<string>, 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 # "<Name of the DocumentDB database>",
|
||||
collectionName: todo # "<Name of the DocumentDB collection>",
|
||||
createIfNotExists: true
|
||||
connection: docDBAppSetting # "<Name of app setting with connection string - see below>",
|
||||
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();
|
||||
};
|
||||
```
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user