Files
mixtape/zero.Raven/Operations/RavenOperationsExtensions.cs
T

50 lines
1.7 KiB
C#
Raw Normal View History

2022-12-07 14:05:11 +01:00
using Raven.Client.Documents.Linq;
2023-07-18 10:59:06 +02:00
using System.Linq.Expressions;
2022-12-07 14:05:11 +01:00
2022-12-07 15:20:53 +01:00
namespace zero.Raven;
2022-12-07 14:05:11 +01:00
2022-12-07 14:29:07 +01:00
public static class RavenOperationsExtensions
2022-12-07 14:05:11 +01:00
{
/// <summary>
/// Stream the collection
/// </summary>
2022-12-07 14:29:07 +01:00
public static IAsyncEnumerable<T> Stream<T>(this IRavenOperations ops) where T : ZeroIdEntity, new() => ops.Stream<T>(null);
2022-12-07 14:05:11 +01:00
/// <summary>
/// Deletes an entity by Id
/// </summary>
2022-12-07 14:29:07 +01:00
public static async Task<Result<T>> Delete<T>(this IRavenOperations ops, string id) where T : ZeroIdEntity, new() => await ops.Delete(await ops.Load<T>(id));
2022-12-07 14:05:11 +01:00
2023-07-18 10:59:06 +02:00
/// <summary>
/// Deletes entities by selector
/// </summary>
public static async Task<int> Delete<T>(this IRavenOperations ops, Expression<Func<T, bool>> predicate) where T : ZeroIdEntity, new() => await ops.Delete(await ops.Load<T>(predicate));
2022-12-07 14:05:11 +01:00
/// <summary>
/// Deletes entities by Id
/// </summary>
2022-12-07 14:29:07 +01:00
public static async Task<int> Delete<T>(this IRavenOperations ops, IEnumerable<string> ids) where T : ZeroIdEntity, new() => await ops.Delete((await ops.Load<T>(ids)).Select(x => x.Value));
2022-12-07 14:05:11 +01:00
/// <summary>
/// Deletes entities
/// </summary>
2022-12-07 14:29:07 +01:00
public static async Task<int> Delete<T>(this IRavenOperations ops, IEnumerable<T> models) where T : ZeroIdEntity, new()
2022-12-07 14:05:11 +01:00
{
int successCount = 0;
foreach (T model in models)
{
Result<T> result = await ops.Delete(model);
successCount += result.IsSuccess ? 1 : 0;
}
return successCount;
}
/// <summary>
/// Deletes an entity by Id with all descendents
/// </summary>
2022-12-07 14:29:07 +01:00
public static async Task<Result<string[]>> DeleteWithDescendants<T>(this IRavenOperations ops, string id) where T : ZeroIdEntity, ISupportsTrees, new() => await ops.DeleteWithDescendants(await ops.Load<T>(id));
2022-12-07 14:05:11 +01:00
}