Skip to content

Commit

Permalink
Merge pull request #73 from Etherna/feature/MODM-167-atomic-upsert
Browse files Browse the repository at this point in the history
add upsert overload with expression filter
  • Loading branch information
tmm360 authored Mar 22, 2024
2 parents 24c0473 + eb29731 commit 2f64d4e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/MongODM.Core/Repositories/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ Task ReplaceAsync(
Expression<Func<TModel, bool>> predicate,
CancellationToken cancellationToken = default);

/// <summary>
/// Find one and modify atomically with an upsert "add to set" operation.
/// Create a new document if doesn't exists, add the element to the set if not present, or do nothing if element is already present
/// </summary>
/// <param name="filter">The document find filter</param>
/// <param name="setField">The set where add the item</param>
/// <param name="itemValue">The item to add</param>
/// <param name="onInsertModel">A new model, in case of insert</param>
/// <param name="cancellationToken">The cancellation token</param>
/// <typeparam name="TItem">Item type</typeparam>
/// <returns>The model as result from find before update</returns>
Task<TModel> UpsertAddToSetAsync<TItem>(
Expression<Func<TModel, bool>> filter,
Expression<Func<TModel, IEnumerable<TItem>>> setField,
TItem itemValue,
TModel onInsertModel,
CancellationToken cancellationToken = default);

/// <summary>
/// Find one and modify atomically with an upsert "add to set" operation.
/// Create a new document if doesn't exists, add the element to the set if not present, or do nothing if element is already present
Expand Down
13 changes: 13 additions & 0 deletions src/MongODM.Core/Repositories/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,19 @@ public virtual Task ReplaceAsync(
}
}

public Task<TModel> UpsertAddToSetAsync<TItem>(
Expression<Func<TModel, bool>> filter,
Expression<Func<TModel, IEnumerable<TItem>>> setField,
TItem itemValue,
TModel onInsertModel,
CancellationToken cancellationToken = default) =>
UpsertAddToSetAsync(
new ExpressionFilterDefinition<TModel>(filter),
setField,
itemValue,
onInsertModel,
cancellationToken);

public Task<TModel> UpsertAddToSetAsync<TItem>(
FilterDefinition<TModel> filter,
Expression<Func<TModel, IEnumerable<TItem>>> setField,
Expand Down

0 comments on commit 2f64d4e

Please sign in to comment.