Skip to content

Commit

Permalink
Provide a way to override YesSql configuration (#11713)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Jul 9, 2022
1 parent b51608a commit 8beac86
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using YesSql;
using YesSql.Indexes;

namespace OrchardCore.Data
{
/// <summary>
/// Represents a contract that used to denote an <see cref="IIndexProvider"/> that needs to be resolved by the DI and registered
/// at the <see cref="YesSql.ISession"/> level.
/// at the <see cref="ISession"/> level.
/// </summary>
public interface IScopedIndexProvider : IIndexProvider
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ShellSettings>();
// 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<ArrayPool<char>>()),
};
var storeConfiguration = GetStoreConfiguration(sp);
switch (shellSettings["DatabaseProvider"])
{
Expand Down Expand Up @@ -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"] + "_");
}
Expand Down Expand Up @@ -161,5 +158,44 @@ public static OrchardCoreBuilder AddDataAccess(this OrchardCoreBuilder builder)

return builder;
}

private static IConfiguration GetStoreConfiguration(IServiceProvider sp)
{
var yesSqlOptions = sp.GetService<IOptions<YesSqlOptions>>().Value;

var storeConfiguration = new YesSql.Configuration
{
CommandsPageSize = yesSqlOptions.CommandsPageSize,
QueryGatingEnabled = yesSqlOptions.QueryGatingEnabled,
ContentSerializer = new PoolingJsonContentSerializer(sp.GetService<ArrayPool<char>>()),
};

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;
}
}
}

0 comments on commit 8beac86

Please sign in to comment.