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");
}
}
You have to use two separate bindings, one for in (your query), one for out. The complete list is on the official docs for the Bindings.
[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
[CosmosDB(
databaseName: "customersDB",
collectionName: "customers",
ConnectionStringSetting = "CosmosDBConnection"]
out dynamic customersDocumentToDB, // out binding
ILogger log)
If you want to store more than 1 document, you can use the IAsyncCollector
:
[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
[CosmosDB(
databaseName: "customersDB",
collectionName: "customers",
ConnectionStringSetting = "CosmosDBConnection"]
IAsyncCollector<dynamic> customersDocumentToDB, // out binding
ILogger log)
And when you want to save a document call await customersDocumentToDB.AddAsync(newDocument)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With