Skip to content

Commit

Permalink
can now toggle full update for updating documents (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
runeanielsen authored Sep 9, 2024
1 parent 701fe9c commit acce387
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/Typesense/ITypesenseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,20 @@ public interface ITypesenseClient

/// <summary>
/// Updates documents in a collection using the supplied filter.
/// Partial update by default. Set full update to include everything in the update, also NULL values.
/// </summary>
/// <param name="collection">The collection name.</param>
/// <param name="document">A documents of type T to update the filtered collection list with.</param>
/// <param name="filter">The filter that is used to selected which documents that should be updated.</param>
/// <param name="filter">The filter that is used to selected which documents that should be updated.</param>
/// <param name="fullUpdate">Includes everything in the document, including NULL values.</param>
/// <returns>A response containing a count of the updated documents.</returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="TypesenseApiException"></exception>
/// <exception cref="TypesenseApiBadRequestException"></exception>
/// <exception cref="TypesenseApiNotFoundException"></exception>
/// <exception cref="TypesenseApiServiceUnavailableException"></exception>
Task<FilterUpdateResponse> UpdateDocuments<T>(string collection, T document, string filter);
Task<FilterUpdateResponse> UpdateDocuments<T>(string collection, T document, string filter, bool fullUpdate = false);

/// <summary>
/// Batch import documents.
Expand Down
8 changes: 5 additions & 3 deletions src/Typesense/TypesenseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class TypesenseClient : ITypesenseClient
private static readonly MediaTypeHeaderValue JsonMediaTypeHeaderValue = MediaTypeHeaderValue.Parse($"{MediaTypeNames.Application.Json};charset={Encoding.UTF8.WebName}");
private readonly HttpClient _httpClient;

private readonly JsonSerializerOptions _jsonSerializerDefault = new();

private readonly JsonSerializerOptions _jsonNameCaseInsensitiveTrue = new() { PropertyNameCaseInsensitive = true };

private readonly JsonSerializerOptions _jsonOptionsCamelCaseIgnoreWritingNull = new()
Expand Down Expand Up @@ -301,7 +303,7 @@ public async Task<UpdateCollectionResponse> UpdateCollection(
return await Patch<UpdateCollectionResponse>($"/collections/{name}", jsonContent, _jsonNameCaseInsensitiveTrue).ConfigureAwait(false);
}

public async Task<FilterUpdateResponse> UpdateDocuments<T>(string collection, T document, string filter)
public async Task<FilterUpdateResponse> UpdateDocuments<T>(string collection, T document, string filter, bool fullUpdate = false)
{
if (string.IsNullOrWhiteSpace(collection))
throw new ArgumentException("cannot be null empty or whitespace", nameof(collection));
Expand All @@ -310,7 +312,7 @@ public async Task<FilterUpdateResponse> UpdateDocuments<T>(string collection, T
if (string.IsNullOrWhiteSpace(filter))
throw new ArgumentException("cannot be null empty or whitespace", nameof(filter));

using var jsonContent = JsonContent.Create(document, JsonMediaTypeHeaderValue, _jsonOptionsCamelCaseIgnoreWritingNull);
using var jsonContent = JsonContent.Create(document, JsonMediaTypeHeaderValue, fullUpdate ? _jsonSerializerDefault : _jsonOptionsCamelCaseIgnoreWritingNull);

return await Patch<FilterUpdateResponse>($"collections/{collection}/documents?filter_by={Uri.EscapeDataString(filter)}&action=update", jsonContent, _jsonNameCaseInsensitiveTrue).ConfigureAwait(false);
}
Expand Down Expand Up @@ -738,4 +740,4 @@ private static StringContent GetTextPlainStringContent(string jsonString)
private MultiSearchResult<T> HandleDeserializeMultiSearch<T>(JsonElement jsonElement)
=> jsonElement.Deserialize<MultiSearchResult<T>>(_jsonNameCaseInsensitiveTrue)
?? throw new InvalidOperationException($"Could not deserialize {typeof(T)}, Received following from Typesense: '{jsonElement}'.");
}
}

0 comments on commit acce387

Please sign in to comment.