2022-12-10 20:17:22 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2022-12-07 14:05:11 +01:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Raven.Client.Documents;
|
|
|
|
|
using Raven.Client.Documents.Indexes;
|
|
|
|
|
using Raven.Client.Http;
|
2026-04-07 14:23:29 +02:00
|
|
|
using Finch.Identity;
|
|
|
|
|
using Finch.Media;
|
|
|
|
|
using Finch.Numbers;
|
2022-12-07 14:05:11 +01:00
|
|
|
|
2026-04-07 14:23:29 +02:00
|
|
|
namespace Finch.Raven;
|
2022-12-07 14:05:11 +01:00
|
|
|
|
2026-04-07 14:23:29 +02:00
|
|
|
public static class FinchBuilderExtensions
|
2022-12-09 10:42:39 +01:00
|
|
|
{
|
2026-04-07 14:23:29 +02:00
|
|
|
public static FinchBuilder AddRavenDb(this FinchBuilder builder)
|
2022-12-09 10:42:39 +01:00
|
|
|
{
|
2026-04-07 14:23:29 +02:00
|
|
|
builder.AddModule<FinchRavenModule>();
|
2022-12-09 10:42:39 +01:00
|
|
|
return builder;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 14:23:29 +02:00
|
|
|
internal class FinchRavenModule : FinchModule
|
2022-12-07 14:05:11 +01:00
|
|
|
{
|
|
|
|
|
public override void ConfigureServices(IServiceCollection services, IConfiguration configuration)
|
|
|
|
|
{
|
2022-12-09 10:42:39 +01:00
|
|
|
services.AddSingleton<IRavenDocumentConventionsBuilder, RavenDocumentConventionsBuilder>();
|
2025-02-25 11:15:39 +01:00
|
|
|
services.AddSingleton<IDocumentStore>(CreateRavenStore);
|
2026-04-07 14:23:29 +02:00
|
|
|
services.AddScoped<IFinchStore, FinchStore>();
|
|
|
|
|
services.AddScoped<IFinchTokenProvider, FinchTokenProvider>();
|
2022-12-07 14:29:07 +01:00
|
|
|
services.AddScoped<StoreContext>();
|
|
|
|
|
services.AddTransient<IRavenOperations, RavenOperations>();
|
2022-12-07 15:20:53 +01:00
|
|
|
services.AddScoped<IInterceptors, Interceptors>();
|
2022-12-07 14:05:11 +01:00
|
|
|
|
2026-04-07 14:23:29 +02:00
|
|
|
services.Replace<IFinchIdentityStoreDbProvider, RavenIdentityStoreDbProvider>(ServiceLifetime.Scoped);
|
|
|
|
|
services.Replace<IFinchMediaStoreDbProvider, RavenMediaStoreDbProvider>(ServiceLifetime.Scoped);
|
|
|
|
|
services.Replace<IFinchNumberStoreDbProvider, RavenNumberStoreDbProvider>(ServiceLifetime.Scoped);
|
2022-12-10 20:17:22 +01:00
|
|
|
|
2022-12-07 14:29:07 +01:00
|
|
|
services.AddOptions<FlavorOptions>();
|
2026-04-07 14:23:29 +02:00
|
|
|
services.AddOptions<RavenOptions>().Bind(configuration.GetSection("Finch:Raven"));
|
2022-12-07 14:29:07 +01:00
|
|
|
services.ConfigureOptions<ConfigureFlavorJsonOptions>();
|
2022-12-07 14:05:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates and configures the raven store
|
|
|
|
|
/// </summary>
|
2025-02-25 11:15:39 +01:00
|
|
|
protected IDocumentStore CreateRavenStore(IServiceProvider services)
|
2022-12-07 14:05:11 +01:00
|
|
|
{
|
2026-04-07 14:23:29 +02:00
|
|
|
IFinchOptions options = services.GetService<IFinchOptions>();
|
2022-12-07 14:05:11 +01:00
|
|
|
RavenOptions ravenOptions = options.For<RavenOptions>();
|
2022-12-09 10:42:39 +01:00
|
|
|
IRavenDocumentConventionsBuilder conventionsBuilder = services.GetService<IRavenDocumentConventionsBuilder>();
|
2022-12-07 14:05:11 +01:00
|
|
|
|
2025-02-25 11:15:39 +01:00
|
|
|
IDocumentStore store = new DocumentStore()
|
2022-12-07 14:05:11 +01:00
|
|
|
{
|
2022-12-07 14:29:07 +01:00
|
|
|
Database = ravenOptions.Database,
|
2022-12-07 14:05:11 +01:00
|
|
|
Urls = new string[1] { ravenOptions.Url },
|
|
|
|
|
Conventions =
|
|
|
|
|
{
|
|
|
|
|
AggressiveCache =
|
|
|
|
|
{
|
2022-12-07 14:29:07 +01:00
|
|
|
Duration = TimeSpan.FromMinutes(ravenOptions.CacheInMinutes),
|
2022-12-07 14:05:11 +01:00
|
|
|
Mode = AggressiveCacheMode.TrackChanges
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
conventionsBuilder.Run(store.Conventions);
|
|
|
|
|
|
|
|
|
|
IDocumentStore raven = store.Initialize();
|
|
|
|
|
|
|
|
|
|
// create all indexes
|
2026-04-07 14:23:29 +02:00
|
|
|
IEnumerable<IFinchIndexDefinition> indexes = ravenOptions.Indexes.BuildAll(options, store);
|
2022-12-07 15:20:53 +01:00
|
|
|
IndexCreation.CreateIndexes(indexes, store, database: ravenOptions.Database);
|
2022-12-07 14:05:11 +01:00
|
|
|
|
2022-12-07 15:20:53 +01:00
|
|
|
|
2025-02-25 11:15:39 +01:00
|
|
|
return raven;
|
2022-12-07 14:05:11 +01:00
|
|
|
}
|
|
|
|
|
}
|