How update collections using Azure functions - azure

I want to create the azure Function which is bind to cosmos DB.
Whenever some insertion happens in "A" collection I want to update "B" collection.
"B" collection has stored procedure which I want to call after the insertion in collection "A".
I am new to Azure and cosmos-DB.
Suggest me what need to be done to accomplish the requirement.
So far I have created Azure Function
Also Updated the function.json with below code.
{
"bindings": [
{
"type": "cosmosDBTrigger",
"name": "input",
"direction": "in",
"leaseCollectionName": "leases",
"connectionStringSetting": "cdb-swm-dev-001_DOCUMENTDB",
"databaseName": "admin",
"collectionName": "aomsorders",
"createLeaseCollectionIfNotExists": true
},
{
"type": "documentDB",
"name": "inputDocument",
"databaseName": "admin",
"collectionName": "aomsorders",
"connection": "cdb-swm-dev-001_DOCUMENTDB",
"direction": "in"
},
{
"type": "documentDB",
"name": "outputDocument",
"databaseName": "admin",
"collectionName": "test",
"createIfNotExists": true,
"connection": "cdb-swm-dev-001_DOCUMENTDB",
"direction": "out"
}
],
"disabled": false
}
Also Updated the Integrate Part as below
Any Suggestion will be appreciable.

I find your question not very specific, but broadly you have at least two options:
Insert to both collections from the same Azure Function
Insert to the collection 1 from the first Azure Function, then have a second Azure Function with Cosmos DB trigger listening to the changes of collection 1 and updating collection 2
I'm sure there are other options too.

Here is an example of an Azure Function that gets triggered by a CosmosDBTrigger and then it uses a DocumentDB Output Binding to write to a second collection:
function.json
{
"bindings": [
{
"type": "cosmosDBTrigger",
"name": "input",
"direction": "in",
"databaseName": "your-database",
"collectionName": "your-collection1",
"connectionStringSetting": "name-of-connectionstring-setting-for-collection1",
"leaseCollectionName": "your-lease-collection"
},
{
"type": "documentDB",
"direction": "out",
"name": "docsToSave",
"databaseName": "your-database2",
"collectionName": "your-collection2",
"connection": "name-of-connectionstring-setting-for-collection2",
"createIfNotExists": false
}
]
}
run.csx (C#)
#r "Microsoft.Azure.Documents.Client"
using Microsoft.Azure.Documents;
using System.Collections.Generic;
using System;
public static async Task Run(IReadOnlyList<Document> input, IAsyncCollector<Document> docsToSave)
{
foreach(var doc in input){
// Do something here, process the document or do your compute
// Here I am saving the same document to the second collection but you could send a new document created within the processing logic or send the same document modified by some logic
await docsToSave.AddAsync(doc);
}
}
index.js (NodeJS)
module.exports = function(context, input) {
if(!!input && input.length > 0){
context.bindings.docsToSave = [];
for(var i = 0, len=input.length; i<len;i++){
var doc = input[i];
// Do something here with the doc or create a new one
context.bindings.docsToSave.push(doc);
}
}
context.done();
}

Related

How can i query data from my azure cosmos db?

I have azure functions written in nodejs. I can't find a way how to get data for example from my created azure cosmos db. I know that there is azure cosmos SDK, but i don't want to use that way.I want to learn to do it through the azure functions because it is possible with them also.
i try do to this:
function.json
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "cosmosDB",
"name": "inputDocument",
"databaseName": "dbtodos",
"collectionName": "items",
"connectionStringSetting": "todos_DOCUMENTDB",
"partitionKey": "/all",
"direction": "in"
}
],
"disabled": false
}
index
module.exports = async function (context, req) {
context.res = {
// status: 200, /* Defaults to 200 */
body: context.bindings.inputDocument
};
};
after my deploy when i visit the automatically generated url - i can't even open the link.There is not requests coming back.
If i do some basic example where i don't try to pull data from the db then my url is working after deploy.
How can i get the data ?
My data in the local.settings.json was wrong. I had azure storage for other table not for the one that i wanted to query... The code works perfectly fine

How to insert data in cosmos DB with azure JavaScript function app?

I am trying to insert data from azure javascript function app to cosmos DB.
This is the function.json File
{
"bindings": [
{
"type": "cosmosDBTrigger",
"name": "documents",
"direction": "in",
"leaseCollectionName": "leases",
"connectionStringSetting": "CosmosDBConnection",
"databaseName": "roi",
"collectionName": "reports",
"createLeaseCollectionIfNotExists": "true"
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
index.js have a code like this.
module.exports = async function (context, documents) {
if (!!documents && documents.length > 0) {
context.log('Document Id: ', documents[0].id);
}
}
Below images will have the error in the application.
Is there any function or query to insert the data ? the request data which is coming from api will look like this.(its coming in rawBody property of request)
When i am fetching the data its workign fine.
{
"name": "inputDocumentIn",
"type": "cosmosDB",
"databaseName": "roi",
"collectionName": "reports",
"sqlQuery": "SELECT * from reports r",
"connectionStringSetting": "CosmosDBConnection",
"direction": "in"
}
local.setting.json file
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "",
"FUNCTIONS_WORKER_RUNTIME": "node",
"CosmosDBConnection": "AccountEndpoint=.....;"
},
"Host": {
"LocalHttpPort": 7071,
"CORS": "*"
}
}
With this azure value in local.setting.ts file , i m getting following error.
Please check if the Connection String is pointing to the correct account and check if it was caused by the firewall and networks. Go to your cosmos db account on azure portal and click "Firewall and virtual networks". You can select "All networks" or "Selected networks" with your current IP filled in it.
This sounds like some some dependency conflict. Could you do a dotnet clean on the project folder and then verify that all projects (in case you have project dependencies) have the latest version of the Microsoft.Azure.WebJobs.Extensions.CosmosDB package (currently 3.0.5)?
The solution of the above problem is :
I have changed function.json file with this code.
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"methods": [ "post" ],
"name": "req"
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "cosmosDB",
"name": "outputDocument",
"databaseName": "roi",
"collectionName": "reports",
"createIfNotExists": true,
"connectionStringSetting": "CosmosDBConnection",
"direction": "out",
"partitionKey": "/id"
}
],
"disabled": false
}
and the request coming from front end will be manipulate like this.
module.exports = function (context, req) {
if (req.body) {
context.bindings.outputDocument = req.body;
var responseBody = {};
responseBody.message = "Wow! data with id '" + req.body.id + "' was created!";
context.res = {
status: 201,
body:responseBody
};
}
else {
context.res = {
status: 400,
body: { "message" : "Please pass a valid object in the request body"}
};
}
context.done();
}

Azure Function - After Out binding how to trigger a function

I have an Azure Function. I've created an out binding and data is being written to that output CosmosDB.
However I want to ask, once that has done, is it possible to hit another trigger?
Or do i have to manually write code to add to the DB, namely not using the out binding?
Thanks.
Heres the code:
The function.json
{
"bindings": [
{
"type": "cosmosDBTrigger",
"name": "documents",
"direction": "in",
"leaseCollectionName": "leases",
"connectionStringSetting": "COSMOSDB_INPUT_CONNECTION_STRING",
"databaseName": "default",
"collectionName": "metadata",
"createLeaseCollectionIfNotExists": false,
"leaseCollectionPrefix": "IngestMetadata",
"startFromBeginning": true
},
{
"type": "cosmosDB",
"name": "outputdocuments",
"direction": "out",
"connectionStringSetting": "COSMOSDB_CONNECTION_STRING",
"databaseName": "default",
"collectionName": "metadata",
"createIfNotExists": true
}
],
"scriptFile": "../dist/IngestMetadata/index.js"
}
And teh code itself:
const cosmosDBTrigger: AzureFunction = async function (context: Context, documents: any[]): Promise<void> {
if (!!documents && documents.length > 0) {
context.bindings.outputdocuments = documents;
}
context.done();
}
So after the context.done I want to hit another trigger
Yes you can use azure function triggers for that
function.json
{
"type": "cosmosDBTrigger",
"name": "documents",
"direction": "in",
"leaseCollectionName": "leases",
"connectionStringSetting": "<connection-app-setting>",
"databaseName": "Tasks",
"collectionName": "Items",
"createLeaseCollectionIfNotExists": true
}
then js code.
module.exports = function (context, documents) {
context.log('First document Id modified : ', documents[0].id);
context.done();
}
So you could have logic for instance from http trigger you write to cosmos db, and another trigger as soon as anything is written to cosmos db and so on
PS. So to sum up, since I answered your previous question as a result you will have 2 functions, first is from your other question where you write output to cosmos db and second function from this question where it will be triggered as soon as 1 function will finish execution and data available in cosmos db

update and delete documents in cosmos db through azure functions

I am new to cosmos db as well as azure functions and I'm getting nowhere fast. I've been able to find every tutorial under the sun to create and read documents but not update and delete. No one seems to have a full CRUD tutorial using azure functions.
Can someone show me a typical .csx file in azure functions that takes a document in, updates it, and returns an OK response?
I've tried this already
#load "..\Shared\Classes.csx"
using System.Net;
public static HttpResponseMessage Run(HttpRequestMessage req,
IEnumerable<Business> businessToBeUpdated, out dynamic updatedBusiness,
TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
//compiler requires this assignment
updatedBusiness = null;
// Get request body
Business data = req.Content.ReadAsAsync<Business>().Result;
businessToBeUpdated = businessToBeUpdated.FirstOrDefault<Business>();
log.Info(businessToBeUpdated.Count().ToString());
if(businessToBeUpdated != null && data != null)
{
//update it
businessToBeUpdated = data;
//updatedBusiness.id = data.id;
log.Info(businessToBeUpdated.website);
}
else{
return req.CreateResponse(HttpStatusCode.BadRequest);
}
return req.CreateResponse(HttpStatusCode.OK);
}
Here is the binding associated with it.
{
"bindings": [
{
"authLevel": "anonymous",
"name": "req",
"type": "httpTrigger",
"direction": "in",
"route": "updatebiz/{id}"
},
{
"name": "$return",
"type": "http",
"direction": "out"
},
{
"type": "documentDB",
"name": "businessToBeUpdated",
"databaseName": "dbname",
"collectionName": "Businesses",
"sqlQuery": "Select * FROM c where c.id = {id}",
"connection": "connection",
"direction": "in"
},
{
"type": "documentDB",
"name": "updatedBusiness",
"databaseName": "dbname",
"collectionName": "Businesses",
"createIfNotExists": false,
"connection": "connection",
"direction": "out"
}
],
"disabled": false
}
The simplest example of a function which updates a document looks exactly the same as the function which creates a document: function will do one or the other based on whether the document with specified id already exists.
You don't mention which exact problem you face. Your code doesn't even compile, having enumerable and single objects assigned to each other. On top of that, you never assign updatedBusiness to anything other than null.
I came with a working example which does what I assume you were trying to accomplish.
csx script:
using System.Net;
public class Business
{
public string id { get; set;}
public string name { get; set;}
}
public static HttpResponseMessage Run(HttpRequestMessage req,
Business businessToBeUpdated, out Business updatedBusiness, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
var data = req.Content.ReadAsAsync<Business>().Result;
if(businessToBeUpdated == null || data == null)
{
updatedBusiness = businessToBeUpdated;
return req.CreateResponse(HttpStatusCode.BadRequest);
}
updatedBusiness = data;
// or merge data and businessToBeUpdated in some desired way
return req.CreateResponse(HttpStatusCode.OK);
}
function.json:
{
"bindings": [
{
"authLevel": "anonymous",
"name": "req",
"type": "httpTrigger",
"direction": "in",
"route": "updatebiz/{id}"
},
{
"name": "$return",
"type": "http",
"direction": "out"
},
{
"type": "documentDB",
"name": "businessToBeUpdated",
"databaseName": "dbname",
"collectionName": "Businesses",
"id": "{id}",
"connection": "connection",
"direction": "in"
},
{
"type": "documentDB",
"name": "updatedBusiness",
"databaseName": "dbname",
"collectionName": "Businesses",
"id": "{id}",
"connection": "connection",
"direction": "out"
}
],
"disabled": false
}

Table storage RowKey ,Partition Key

Could someone please tell me whether it is possible to assign same value for both Partitionkey and Rowkey in Azure Functionapp?
Many Thanks in advance
Based on your description, I just created my Http Trigger for Node.js to check this issue.
function.json:
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req"
},
{
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "table",
"name": "outputTable",
"tableName": "emails",
"connection": "AzureWebJobsDashboard",
"direction": "out"
}
],
"disabled": false
}
index.js:
var date=Date.now();
var key=date+'-'+Math.ceil(Math.random()*1000);
context.bindings.outputTable = {
"partitionKey": key,
"rowKey":key,
"GPIOPin":2,
'status':true
};
Leverage Azure Storage Explorer to check my table as follows:
For more details about the output sample for Table storage binding, you could refer to here.
It is possible. The design consequences will be that you will have partitions with a size of one entity. Remember that batch operations and transaction support are limited to entities in the same partition.

Resources