Tasks

Create database and API for simple ToDo list application:

  1. Create a new collection
  2. Replicate the database to three different regions
  3. Change Default consistency to Eventual
  4. Turn on automatic failover and set up your order
  5. Create a function that allows adding new ToDo items to the database.
  6. Create a function that will list all ToDo items assigned to list.
  7. Try to test your solution
  8. Remove description from indexing

 

ToDo item structure:

  • name
  • description
  • due date
  • status – to do or done
  • assigned list name

Materials

Documentation

Azure Cosmos DB bindings for Azure Functions 2.x

Code samples

Trigger

[FunctionName("CollectionChangeMonitor")]
public static void Run([CosmosDBTrigger(
  databaseName: "workshop",
  collectionName: "family_100k",
  ConnectionStringSetting = "CosmosDBConnection",
  LeaseCollectionName = "leases",
  CreateLeaseCollectionIfNotExists = true)]IReadOnlyList<Document> documents, ILogger log)
{
  if (documents != null && documents.Count > 0)
  {
    log.LogInformation("Documents modified " + documents.Count);
    foreach (var document in documents)
    {
      log.LogInformation("Document Id " + document.Id);
      log.LogInformation("Family name " + document.GetPropertyValue<string>("lastName"));
      log.LogInformation(Environment.NewLine);
    }
  }
}

Read access

[FunctionName("SearchById")]
public static IActionResult Run(
  [HttpTrigger(AuthorizationLevel.Function, "get", Route = "family/{lastName}/{id}")]HttpRequestMessage req,
  [CosmosDB(
    databaseName: "workshop",
    collectionName: "family_100k",
    ConnectionStringSetting = "CosmosDBConnection",
    PartitionKey = "{lastName}",
    Id = "{id}")] Family family, ILogger log)
{
  log.LogInformation("Found: " + family.lastName);
  return new OkObjectResult(family);
}


[FunctionName("SearchByQuery")]
public static IActionResult Run(
  [HttpTrigger(AuthorizationLevel.Function, "get", Route = "families/mother/{motherName}")]HttpRequestMessage req,
  [CosmosDB(
    databaseName: "workshop",
    collectionName: "family_100k",
    ConnectionStringSetting = "CosmosDBConnection",
    SqlQuery = "SELECT * FROM c Where c.parents[0].firstName = {motherName}")] IEnumerable<Family> families, ILogger log)
{
  log.LogInformation("Found: " + families.Count());
  return new OkObjectResult(families);
}


[FunctionName("SearchByDocumentClient")]
public static async Task<IActionResult> Run(
  [HttpTrigger(AuthorizationLevel.Function, "get", Route = "families/mother2/{motherName}")]HttpRequestMessage req,
  [CosmosDB(
    databaseName: "workshop",
    collectionName: "family_100k",
    ConnectionStringSetting = "CosmosDBConnection")] DocumentClient client, string motherName, ILogger log)
{
  Uri collectionUri = UriFactory.CreateDocumentCollectionUri("demo", "family_100k");
         
  IDocumentQuery<Family> families = 
    client
      .CreateDocumentQuery<Family>(collectionUri, new FeedOptions
        { EnableCrossPartitionQuery = true})
      .Where(p => p.parents[0].firstName == motherName)
      .AsDocumentQuery();
 
  var result = new List<Family>();
  while (families.HasMoreResults)
  {
    result.AddRange(await families.ExecuteNextAsync<Family>());
  }
 
  log.LogInformation("Found: " + result.Count);
  return new OkObjectResult(result);
}

Write access

[FunctionName("AddOneFamily")]
public static IActionResult Run(
  [HttpTrigger(AuthorizationLevel.Function, "post", Route = "families")]Family family,
  [CosmosDB(
    databaseName: "workshop",
    collectionName: "family_100k",
    ConnectionStringSetting = "CosmosDBConnection")] out dynamic documentDb,
  ILogger log)
{
  documentDb = family;
  return new OkResult();
}



[FunctionName("AddManyFamilies")]
public static async Task<IActionResult> Run(
  [HttpTrigger(AuthorizationLevel.Function, "post", Route = "families/batch")]Family[] families,
  [CosmosDB(
    databaseName: "workshop",
    collectionName: "family_100k",
    ConnectionStringSetting = "CosmosDBConnection")] IAsyncCollector<Family> documentDb,
  ILogger log)
{
  foreach (var family in families)
  {
    await documentDb.AddAsync(family);
  }
 
  return new OkResult();
}


[FunctionName("UpdateOneFamily")]
public static IActionResult Run(
  [HttpTrigger(AuthorizationLevel.Function, "put", Route = "family/{lastName}/{id}")]Family family,
  [CosmosDB(
    databaseName: "workshop",
    collectionName: "family_100k",
    ConnectionStringSetting = "CosmosDBConnection",
    PartitionKey = "{lastName}",
    Id = "{id}")] out dynamic document, string id, string lastName, ILogger log)
{
  document = new
    {
      id = id,
      lastName = lastName,
      parents = family.parents,
      children = family.children,
      address = family.address,
    };
  return new OkResult();
}