Multi-Platform Package Manager for Stable Diffusion
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.7 KiB

using System.Collections.Concurrent;
using System.Linq.Expressions;
1 year ago
using System.Reflection;
using LiteDB;
using LiteDB.Async;
namespace StabilityMatrix.Core.Extensions;
1 year ago
// ReSharper disable once InconsistentNaming
public static class LiteDBExtensions
{
private static readonly ConcurrentDictionary<Type, (Type PropertyType, string MemberName, bool IsList)> Mapper =
new();
1 year ago
public static void Register<T, TU>(Expression<Func<T, List<TU>?>> exp, string? collection = null)
{
var member = (exp.Body is MemberExpression body ? body.Member : null) as PropertyInfo;
if (member == null)
throw new ArgumentException("Expecting Member Expression");
BsonMapper.Global.Entity<T>().DbRef(exp, collection);
Mapper.TryAdd(typeof(T), (typeof(TU), member.Name, true));
1 year ago
}
public static void Register<T, TU>(Expression<Func<T, TU?>> exp, string? collection = null)
{
var member = (exp.Body is MemberExpression body ? body.Member : null) as PropertyInfo;
if (member == null)
throw new ArgumentException("Expecting Member Expression");
BsonMapper.Global.Entity<T>().DbRef(exp, collection);
Mapper.TryAdd(typeof(T), (typeof(TU), member.Name, false));
1 year ago
}
public static ILiteCollection<T>? IncludeAll<T>(this ILiteCollection<T> col)
{
if (!Mapper.ContainsKey(typeof(T)))
return null;
1 year ago
var stringList = new List<string>();
var key = typeof(T);
var values = new List<string>();
var flag = true;
while (Mapper.TryGetValue(key, out var tuple))
{
var str = tuple.MemberName + (tuple.IsList ? "[*]" : "");
values.Add(flag ? "$." + str : str);
stringList.Add(string.Join(".", values));
key = tuple.PropertyType;
flag = false;
}
return stringList.Aggregate(col, (current, keySelector) => current.Include((BsonExpression)keySelector));
1 year ago
}
1 year ago
public static ILiteCollectionAsync<T> IncludeAll<T>(this ILiteCollectionAsync<T> col)
{
if (!Mapper.ContainsKey(typeof(T)))
return col;
1 year ago
var stringList = new List<string>();
var key = typeof(T);
var values = new List<string>();
var flag = true;
while (Mapper.TryGetValue(key, out var tuple))
{
var str = tuple.MemberName + (tuple.IsList ? "[*]" : "");
values.Add(flag ? "$." + str : str);
stringList.Add(string.Join(".", values));
key = tuple.PropertyType;
flag = false;
}
return stringList.Aggregate(col, (current, keySelector) => current.Include((BsonExpression)keySelector));
1 year ago
}
}