Skip to content

Commit

Permalink
Add the usage code with CosmosClient
Browse files Browse the repository at this point in the history
  • Loading branch information
ewdlop committed Mar 24, 2024
1 parent 93071c0 commit a5532bd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
56 changes: 54 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
using Microsoft.Azure.Cosmos;
using Newtonsoft.Json.Linq;
using System.Collections;
using System.Net;
using System.Reflection;

//https://learn.microsoft.com/en-us/azure/cosmos-db/partial-document-update-getting-started?tabs=dotnet

//place holders
string connectionString = "AccountEndpoint=https://mycosmosaccount.documents.azure.com:443/;AccountKey=mykey==;";
string databaseId = "myCosmosDBId";

// sample codes
// still need more testing
PatchOperationList patchOperationList = new()
{
PatchOperation.Add("age", 33),
Expand Down Expand Up @@ -39,7 +45,7 @@
IList<PatchOperation> patchOperations2 = new PatchOperationList();
IReadOnlyList<PatchOperation> patchOperations3 = patchOperationList;

PatchOperationList builder = new PatchOperationListBuilder()
PatchOperationList patchOperationList8 = new PatchOperationListBuilder()
.With(PatchOperation.Move("from", "to"))
.WithAdd("age", 70)
.WithAdd("address", new JObject
Expand All @@ -65,6 +71,52 @@

patchOperationList.ForEach(Console.WriteLine);

//usage of PatchOperationList
try
{
CosmosClient client = new(connectionString);
Database database = client.GetDatabase(databaseId);
string partitionKeyPath = "/myPartitionKey";
ContainerResponse response = await database.CreateContainerIfNotExistsAsync("myContainer", partitionKeyPath);
if (response.StatusCode == HttpStatusCode.Created)
{
Container container = response.Container;
dynamic item = new { id = "myId", name = "myName" };
string myPartitionKey = $"{partitionKeyPath}/1";
PartitionKey partitionKey = new PartitionKey(myPartitionKey);
ItemResponse<dynamic> itemResponse = await container.CreateItemAsync(item, new PartitionKey(myPartitionKey));
Console.WriteLine($"Created item in database with id: {itemResponse.Resource.id}");

//Partial update
ItemResponse<dynamic> patchResponse = await container.PatchItemAsync<dynamic>(item.id, partitionKey, patchOperationList);
if(patchResponse.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine($"Patched item in database with id: {patchResponse.Resource.id}");
}
else
{
Console.WriteLine($"Failed to patch item in database with id: {patchResponse.Resource.id}");
}
}
else if (response.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("Failed to create container or container already exists");
}
}
catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound ||
ex.StatusCode == HttpStatusCode.BadRequest)
{
Console.WriteLine($"CosmosException: {ex}");
}
catch (CosmosException ex)
{
Console.WriteLine($"Other CosmosException: {ex}");
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex}");
}

//Not done yet
namespace CosmosDBPartialUpdateTypeConverter
{
Expand All @@ -74,7 +126,7 @@ public class PatchOperationList : IList<PatchOperation>, IReadOnlyList<PatchOper

public static implicit operator PatchOperationList(List<PatchOperation> patchOperations) => new(patchOperations);
public static implicit operator PatchOperationList(PatchOperation[] patchOperations) => new([.. patchOperations]);
public static implicit operator PatchOperationList(PatchOperation patchOperation) => new([patchOperation]);
public static implicit operator PatchOperationList(PatchOperation patchOperation) => new(patchOperation);
public static implicit operator List<PatchOperation>(PatchOperationList patchOperation) => patchOperation._patchOperations;

public PatchOperationList() : this([]) { }
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# CosmosDBPartialUpdateTypeConverter
# CosmosDBPartialUpdateTypeConverter

Requires Dotnet 8.0 SDK
https://dotnet.microsoft.com/en-us/download/dotnet/8.0

0 comments on commit a5532bd

Please sign in to comment.