-
Notifications
You must be signed in to change notification settings - Fork 210
Cosmos DB: Migration to V3 SDK #717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,7 @@ | |
| using System.Net; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Azure.Documents; | ||
| using Microsoft.Azure.Documents.Client; | ||
| using Microsoft.Azure.Cosmos; | ||
| using Microsoft.Azure.WebJobs.Host.Bindings; | ||
| using Newtonsoft.Json; | ||
| using Newtonsoft.Json.Linq; | ||
|
|
@@ -41,48 +40,35 @@ public async Task SetValueAsync(object value, CancellationToken cancellationToke | |
|
|
||
| public async Task<object> GetValueAsync() | ||
| { | ||
| Uri documentUri = UriFactory.CreateDocumentUri(_context.ResolvedAttribute.DatabaseName, _context.ResolvedAttribute.CollectionName, _context.ResolvedAttribute.Id); | ||
| RequestOptions options = null; | ||
| T document = default(T); | ||
|
|
||
| if (!string.IsNullOrEmpty(_context.ResolvedAttribute.PartitionKey)) | ||
| { | ||
| options = new RequestOptions | ||
| { | ||
| PartitionKey = new PartitionKey(_context.ResolvedAttribute.PartitionKey) | ||
| }; | ||
| } | ||
|
|
||
| Document document = null; | ||
|
|
||
| try | ||
| { | ||
| document = await _context.Service.ReadDocumentAsync(documentUri, options); | ||
| } | ||
| catch (DocumentClientException ex) when (ex.StatusCode == HttpStatusCode.NotFound) | ||
| { | ||
| // ignore not found; we'll return null below | ||
| } | ||
|
|
||
| if (document == null) | ||
| { | ||
| return document; | ||
| } | ||
|
|
||
| T item = null; | ||
| PartitionKey partitionKey = _context.ResolvedAttribute.PartitionKey == null ? PartitionKey.None : new PartitionKey(_context.ResolvedAttribute.PartitionKey); | ||
|
|
||
| // Strings need to be handled differently. | ||
| if (typeof(T) == typeof(string)) | ||
| if (typeof(T) != typeof(string)) | ||
| { | ||
| _originalItem = JObject.FromObject(document); | ||
| item = _originalItem.ToString(Formatting.None) as T; | ||
| try | ||
| { | ||
| document = await _context.Service.GetContainer(_context.ResolvedAttribute.DatabaseName, _context.ResolvedAttribute.CollectionName) | ||
| .ReadItemAsync<T>(_context.ResolvedAttribute.Id, partitionKey); | ||
|
|
||
| _originalItem = JObject.FromObject(document); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if we want to continue supporting this "update-in-place" behavior. It's never worked for out-of-proc languages and has resulted in a decent amount of confusion as it seems all of the bindings handle it differently. @fabiocav -- thoughts? |
||
| } | ||
| catch (CosmosException ex) when (ex.StatusCode == HttpStatusCode.NotFound) | ||
| { | ||
| // ignore not found; we'll return null below | ||
| } | ||
| } | ||
| else | ||
| { | ||
| item = (T)(dynamic)document; | ||
| _originalItem = JObject.FromObject(item); | ||
| JObject jObject = await _context.Service.GetContainer(_context.ResolvedAttribute.DatabaseName, _context.ResolvedAttribute.CollectionName) | ||
| .ReadItemAsync<JObject>(_context.ResolvedAttribute.Id, partitionKey); | ||
| _originalItem = jObject; | ||
|
|
||
| document = _originalItem.ToString(Formatting.None) as T; | ||
| } | ||
|
|
||
| return item; | ||
| return document; | ||
| } | ||
|
|
||
| public string ToInvokeString() | ||
|
|
@@ -111,7 +97,7 @@ internal static async Task SetValueInternalAsync(JObject originalItem, T newItem | |
| // make sure it's not the Id that has changed | ||
| if (!string.Equals(originalId, currentId, StringComparison.Ordinal)) | ||
| { | ||
| throw new InvalidOperationException("Cannot update the 'Id' property."); | ||
| throw new InvalidOperationException("Cannot update the 'id' property."); | ||
| } | ||
| } | ||
| else | ||
|
|
@@ -121,8 +107,8 @@ internal static async Task SetValueInternalAsync(JObject originalItem, T newItem | |
| throw new InvalidOperationException(string.Format("The document must have an 'id' property.")); | ||
| } | ||
|
|
||
| Uri documentUri = UriFactory.CreateDocumentUri(context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.CollectionName, originalId); | ||
| await context.Service.ReplaceDocumentAsync(documentUri, newItem); | ||
| Container container = context.Service.GetContainer(context.ResolvedAttribute.DatabaseName, context.ResolvedAttribute.CollectionName); | ||
| await container.ReplaceItemAsync<T>(newItem, originalId); | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use System.Text.Json here? Would that allow you to remove a ref to Newtonsoft?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind... I forgot we require Newtonsoft.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The SDK could take System.Text.Json using a custom serializer if that is the approach we want to take.