Skip to content

Commit 80e2320

Browse files
authoredFeb 14, 2025
Merge pull request #117 from CityStructure/cosmosdb-datawritemode-delete
introduce DataWriteMode.Delete to undo inserts to the wrong container
2 parents b3171df + 249c53d commit 80e2320

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed
 

‎Extensions/Cosmos/Cosmos.DataTransfer.CosmosExtension/CosmosDataSinkExtension.cs

+15-4
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private static Task<ItemResult> AddItemAsync(Container container, ExpandoObject
162162
return t.Result;
163163
}
164164

165-
return new ItemResult(null, HttpStatusCode.InternalServerError);
165+
return new ItemResult(null, mode, HttpStatusCode.InternalServerError);
166166
}, cancellationToken);
167167
return task;
168168
}
@@ -190,14 +190,24 @@ private static async Task<ItemResult> PopulateItem(Container container, ExpandoO
190190
var upsertResponse = await container.UpsertItemAsync(item, cancellationToken: cancellationToken);
191191
statusCode = upsertResponse.StatusCode;
192192
break;
193+
case DataWriteMode.DeleteStream:
194+
ArgumentNullException.ThrowIfNull(partitionKeyPath, nameof(partitionKeyPath));
195+
var deleteMessage = await container.DeleteItemStreamAsync(itemId, new PartitionKey(GetPropertyValue(item, partitionKeyPath.TrimStart('/'))), cancellationToken: cancellationToken);
196+
statusCode = deleteMessage.StatusCode;
197+
break;
198+
case DataWriteMode.Delete:
199+
ArgumentNullException.ThrowIfNull(partitionKeyPath, nameof(partitionKeyPath));
200+
var deleteResponse = await container.DeleteItemAsync<ExpandoObject>(itemId, new PartitionKey(GetPropertyValue(item, partitionKeyPath.TrimStart('/'))), cancellationToken: cancellationToken);
201+
statusCode = deleteResponse.StatusCode;
202+
break;
193203
}
194204

195205
if (statusCode == null)
196206
{
197207
throw new ArgumentOutOfRangeException(nameof(mode), $"Invalid data write mode specified: {mode}");
198208
}
199209

200-
return new ItemResult(itemId, statusCode.Value);
210+
return new ItemResult(itemId, mode, statusCode.Value);
201211
}
202212

203213
private static MemoryStream CreateItemStream(ExpandoObject item)
@@ -216,9 +226,10 @@ public IEnumerable<IDataExtensionSettings> GetSettings()
216226
yield return new CosmosSinkSettings();
217227
}
218228

219-
public record ItemResult(string? Id, HttpStatusCode StatusCode)
229+
public record ItemResult(string? Id, DataWriteMode DataWriteMode, HttpStatusCode StatusCode)
220230
{
221-
public bool IsSuccess => StatusCode is HttpStatusCode.OK or HttpStatusCode.Created;
231+
public bool IsSuccess => StatusCode is HttpStatusCode.OK or HttpStatusCode.Created ||
232+
(StatusCode is HttpStatusCode.NoContent or HttpStatusCode.NotFound && DataWriteMode is DataWriteMode.Delete or DataWriteMode.DeleteStream);
222233
public int ItemCount => IsSuccess ? 1 : 0;
223234
}
224235
}

‎Extensions/Cosmos/Cosmos.DataTransfer.CosmosExtension/DataWriteMode.cs

+2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ public enum DataWriteMode
66
Insert,
77
UpsertStream,
88
Upsert,
9+
DeleteStream,
10+
Delete
911
}
1012
}

0 commit comments

Comments
 (0)