I'm having trouble figuring out how to access CosmosDB with in and out binding at the same time in at Azure Function 2.0.
I can from one HttpTrigger function get a json object from my cosmosDB collection and from another HttpTrigger function, write the json object to the collection
What I can't figure out is how to first read the json object from the cosmosDB collection, make some changes to it and write it back again, from within the same Function.
The code below should outline my question
[FunctionName("WebrootConnector")]
public static void Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
[CosmosDB(
databaseName: "customersDB",
collectionName: "customers",
ConnectionStringSetting = "CosmosDBConnection",
CreateIfNotExists = true,
Id = "999",
PartitionKey = "/id")]
Customers customersObject, // in binding
out dynamic customersDocumentToDB, // out binding
ILogger log)
{
// Chect if a customersObject is recieved from cosmosDB
if (customersObject == null)
{
// Create a new Customers object
customersObject = new Customers();
// Set the id of the database document (should always be the same)
customersObject.Id = 999;
// Create a new empty customer list on the customers object
customersObject.customers = new List<Customer>();
// Add some customers to the list
}
else
{
// if a object is received from the database
// do something with it.
}
if (customersObject.customers != null)
{
// Write the object back to the cosmosDB collection
customersDocumentToDB = customersObject;
log.LogInformation($"Data written to customerDB");
}
else
{
customersDocumentToDB = null;
log.LogInformation($"Nothing to write to database");
}
}