From 8beac86204ac77dc70054d9daf7dd88e147b4609 Mon Sep 17 00:00:00 2001 From: CrestApps Date: Sat, 9 Jul 2022 09:35:51 -0700 Subject: [PATCH] Provide a way to override YesSql configuration (#11713) --- .../IScopedIndexProvider.cs | 3 +- .../YesSqlOptions.cs | 20 ++++++++ .../OrchardCoreBuilderExtensions.cs | 50 ++++++++++++++++--- 3 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 src/OrchardCore/OrchardCore.Data.YesSql.Abstractions/YesSqlOptions.cs diff --git a/src/OrchardCore/OrchardCore.Data.YesSql.Abstractions/IScopedIndexProvider.cs b/src/OrchardCore/OrchardCore.Data.YesSql.Abstractions/IScopedIndexProvider.cs index dfe3e731d61..2bf3e62ed2d 100644 --- a/src/OrchardCore/OrchardCore.Data.YesSql.Abstractions/IScopedIndexProvider.cs +++ b/src/OrchardCore/OrchardCore.Data.YesSql.Abstractions/IScopedIndexProvider.cs @@ -1,10 +1,11 @@ +using YesSql; using YesSql.Indexes; namespace OrchardCore.Data { /// /// Represents a contract that used to denote an that needs to be resolved by the DI and registered - /// at the level. + /// at the level. /// public interface IScopedIndexProvider : IIndexProvider { diff --git a/src/OrchardCore/OrchardCore.Data.YesSql.Abstractions/YesSqlOptions.cs b/src/OrchardCore/OrchardCore.Data.YesSql.Abstractions/YesSqlOptions.cs new file mode 100644 index 00000000000..3a6872e5299 --- /dev/null +++ b/src/OrchardCore/OrchardCore.Data.YesSql.Abstractions/YesSqlOptions.cs @@ -0,0 +1,20 @@ +using YesSql; + +namespace OrchardCore.Data.YesSql.Abstractions; + +public class YesSqlOptions +{ + public int CommandsPageSize { get; set; } = 500; + + public bool QueryGatingEnabled { get; set; } = true; + + public IIdGenerator IdGenerator { get; set; } + + public ITableNameConvention TableNameConvention { get; set; } + + public IAccessorFactory IdentifierAccessorFactory { get; set; } + + public IAccessorFactory VersionAccessorFactory { get; set; } + + public IContentSerializer ContentSerializer { get; set; } +} diff --git a/src/OrchardCore/OrchardCore.Data.YesSql/OrchardCoreBuilderExtensions.cs b/src/OrchardCore/OrchardCore.Data.YesSql/OrchardCoreBuilderExtensions.cs index 8129048a72d..e8895b8979c 100644 --- a/src/OrchardCore/OrchardCore.Data.YesSql/OrchardCoreBuilderExtensions.cs +++ b/src/OrchardCore/OrchardCore.Data.YesSql/OrchardCoreBuilderExtensions.cs @@ -8,6 +8,7 @@ using OrchardCore.Data; using OrchardCore.Data.Documents; using OrchardCore.Data.Migration; +using OrchardCore.Data.YesSql.Abstractions; using OrchardCore.Environment.Shell; using OrchardCore.Environment.Shell.Models; using OrchardCore.Environment.Shell.Scope; @@ -47,21 +48,17 @@ public static OrchardCoreBuilder AddDataAccess(this OrchardCoreBuilder builder) services.TryAddDataProvider(name: "Postgres", value: "Postgres", hasConnectionString: true, sampleConnectionString: "Server=localhost;Port=5432;Database=Orchard;User Id=username;Password=password", hasTablePrefix: true, isDefault: false); // Configuring data access - services.AddSingleton(sp => { var shellSettings = sp.GetService(); - // Before the setup a 'DatabaseProvider' may be configured without a required 'ConnectionString'. + // Before the setup, a 'DatabaseProvider' may be configured without a required 'ConnectionString'. if (shellSettings.State == TenantState.Uninitialized || shellSettings["DatabaseProvider"] == null) { return null; } - IConfiguration storeConfiguration = new YesSql.Configuration - { - ContentSerializer = new PoolingJsonContentSerializer(sp.GetService>()), - }; + var storeConfiguration = GetStoreConfiguration(sp); switch (shellSettings["DatabaseProvider"]) { @@ -102,7 +99,7 @@ public static OrchardCoreBuilder AddDataAccess(this OrchardCoreBuilder builder) throw new ArgumentException("Unknown database provider: " + shellSettings["DatabaseProvider"]); } - if (!string.IsNullOrWhiteSpace(shellSettings["TablePrefix"])) + if (!String.IsNullOrWhiteSpace(shellSettings["TablePrefix"])) { storeConfiguration = storeConfiguration.SetTablePrefix(shellSettings["TablePrefix"] + "_"); } @@ -161,5 +158,44 @@ public static OrchardCoreBuilder AddDataAccess(this OrchardCoreBuilder builder) return builder; } + + private static IConfiguration GetStoreConfiguration(IServiceProvider sp) + { + var yesSqlOptions = sp.GetService>().Value; + + var storeConfiguration = new YesSql.Configuration + { + CommandsPageSize = yesSqlOptions.CommandsPageSize, + QueryGatingEnabled = yesSqlOptions.QueryGatingEnabled, + ContentSerializer = new PoolingJsonContentSerializer(sp.GetService>()), + }; + + if (yesSqlOptions.IdGenerator != null) + { + storeConfiguration.IdGenerator = yesSqlOptions.IdGenerator; + } + + if (yesSqlOptions.TableNameConvention != null) + { + storeConfiguration.TableNameConvention = yesSqlOptions.TableNameConvention; + } + + if (yesSqlOptions.IdentifierAccessorFactory != null) + { + storeConfiguration.IdentifierAccessorFactory = yesSqlOptions.IdentifierAccessorFactory; + } + + if (yesSqlOptions.VersionAccessorFactory != null) + { + storeConfiguration.VersionAccessorFactory = yesSqlOptions.VersionAccessorFactory; + } + + if (yesSqlOptions.ContentSerializer != null) + { + storeConfiguration.ContentSerializer = yesSqlOptions.ContentSerializer; + } + + return storeConfiguration; + } } }