Browse Source

Update documentdb compatibility.

pull/1269/head
Sebastian Stehle 10 months ago
parent
commit
d7045a5e5c
  1. 23
      backend/src/Squidex.Data.MongoDb/Domain/Apps/Entities/Contents/Text/DocumentDbTextIndex.cs
  2. 32
      backend/src/Squidex.Data.MongoDb/ServiceExtensions.cs
  3. 2
      backend/src/Squidex.Data.MongoDb/Squidex.Data.MongoDb.csproj
  4. 9
      backend/src/Squidex/appsettings.json
  5. 5
      backend/tests/Squidex.Data.Tests/MongoDb/TestHelpers/DocumentDbFixture.cs

23
backend/src/Squidex.Data.MongoDb/Domain/Apps/Entities/Contents/Text/DocumentDbTextIndex.cs

@ -10,7 +10,6 @@ using MongoDB.Driver.GeoJsonObjectModel;
using Squidex.Domain.Apps.Core.Apps; using Squidex.Domain.Apps.Core.Apps;
using Squidex.Infrastructure; using Squidex.Infrastructure;
using Squidex.Infrastructure.ObjectPool; using Squidex.Infrastructure.ObjectPool;
using Squidex.Infrastructure.Translations;
namespace Squidex.Domain.Apps.Entities.Contents.Text; namespace Squidex.Domain.Apps.Entities.Contents.Text;
@ -38,21 +37,19 @@ public sealed class DocumentDbTextIndex(IMongoDatabase database, string shardKey
Index.Text(x => x.Texts)), Index.Text(x => x.Texts)),
cancellationToken: ct); cancellationToken: ct);
await collection.Indexes.CreateManyAsync( await collection.Indexes.CreateManyAsync(
[ [
new CreateIndexModel<MongoTextIndexEntity<string>>( new CreateIndexModel<MongoTextIndexEntity<string>>(
Index Index
.Ascending(x => x.AppId) .Ascending(x => x.AppId)
.Ascending(x => x.ContentId)), .Ascending(x => x.ContentId)),
new CreateIndexModel<MongoTextIndexEntity<string>>(
new CreateIndexModel<MongoTextIndexEntity<string>>( Index
Index .Ascending(x => x.AppId)
.Ascending(x => x.AppId) .Ascending(x => x.SchemaId)
.Ascending(x => x.SchemaId) .Ascending(x => x.GeoField)
.Ascending(x => x.GeoField) .Geo2DSphere(x => x.GeoObject)),
.Geo2DSphere(x => x.GeoObject)), ], ct);
], ct);
} }
public override async Task<List<DomainId>?> SearchAsync(App app, GeoQuery query, SearchScope scope, public override async Task<List<DomainId>?> SearchAsync(App app, GeoQuery query, SearchScope scope,

32
backend/src/Squidex.Data.MongoDb/ServiceExtensions.cs

@ -6,6 +6,7 @@
// ========================================================================== // ==========================================================================
using System.Net; using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json; using System.Text.Json;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Caching.Distributed;
@ -63,14 +64,13 @@ public static class ServiceExtensions
{ {
public static void AddSquidexMongoEventStore(this IServiceCollection services, IConfiguration config) public static void AddSquidexMongoEventStore(this IServiceCollection services, IConfiguration config)
{ {
var mongoConfiguration = config.GetRequiredValue("eventStore:mongoDb:configuration");
var mongoDatabaseName = config.GetRequiredValue("eventStore:mongoDb:database"); var mongoDatabaseName = config.GetRequiredValue("eventStore:mongoDb:database");
services.AddMongoEventStore(config); services.AddMongoEventStore(config);
services.AddSingletonAs(c => services.AddSingletonAs(c =>
{ {
var options = c.GetRequiredService<IOptions<MongoEventStoreOptions>>(); var options = c.GetRequiredService<IOptions<MongoEventStoreOptions>>();
var mongoClient = GetMongoClient(mongoConfiguration); var mongoClient = GetMongoClient(config, "eventStore:mongoDb");
var mongoDatabase = mongoClient.GetDatabase(mongoDatabaseName); var mongoDatabase = mongoClient.GetDatabase(mongoDatabaseName);
return new MongoEventStore(mongoDatabase, options); return new MongoEventStore(mongoDatabase, options);
@ -80,13 +80,12 @@ public static class ServiceExtensions
public static void AddSquidexMongoAssetStore(this IServiceCollection services, IConfiguration config) public static void AddSquidexMongoAssetStore(this IServiceCollection services, IConfiguration config)
{ {
var mongoConfiguration = config.GetRequiredValue("assetStore:mongoDb:configuration");
var mongoDatabaseName = config.GetRequiredValue("assetStore:mongoDb:database"); var mongoDatabaseName = config.GetRequiredValue("assetStore:mongoDb:database");
var mongoGridFsBucketName = config.GetRequiredValue("assetStore:mongoDb:bucket"); var mongoGridFsBucketName = config.GetRequiredValue("assetStore:mongoDb:bucket");
services.AddMongoAssetStore(c => services.AddMongoAssetStore(c =>
{ {
var mongoClient = GetMongoClient(mongoConfiguration); var mongoClient = GetMongoClient(config, "assetStore:mongoDb");
var mongoDatabase = mongoClient.GetDatabase(mongoDatabaseName); var mongoDatabase = mongoClient.GetDatabase(mongoDatabaseName);
return new GridFSBucket<string>(mongoDatabase, new GridFSBucketOptions return new GridFSBucket<string>(mongoDatabase, new GridFSBucketOptions
@ -98,7 +97,6 @@ public static class ServiceExtensions
public static void AddSquidexMongoStore(this IServiceCollection services, IConfiguration config) public static void AddSquidexMongoStore(this IServiceCollection services, IConfiguration config)
{ {
var mongoConfiguration = config.GetRequiredValue("store:mongoDb:configuration")!;
var mongoDatabaseName = config.GetRequiredValue("store:mongoDb:database")!; var mongoDatabaseName = config.GetRequiredValue("store:mongoDb:database")!;
var mongoContentDatabaseName = config.GetOptionalValue("store:mongoDb:contentDatabase", mongoDatabaseName)!; var mongoContentDatabaseName = config.GetOptionalValue("store:mongoDb:contentDatabase", mongoDatabaseName)!;
@ -107,7 +105,7 @@ public static class ServiceExtensions
return GetDatabase(c, mongoDatabaseName); return GetDatabase(c, mongoDatabaseName);
}); });
services.AddSingletonAs(c => GetMongoClient(mongoConfiguration)) services.AddSingletonAs(c => GetMongoClient(config, "store:mongoDb"))
.As<IMongoClient>(); .As<IMongoClient>();
services.AddSingletonAs(c => GetDatabase(c, mongoDatabaseName)) services.AddSingletonAs(c => GetDatabase(c, mongoDatabaseName))
@ -289,12 +287,28 @@ public static class ServiceExtensions
.As<IMigration>(); .As<IMigration>();
} }
private static IMongoClient GetMongoClient(string configuration) private static IMongoClient GetMongoClient(IConfiguration config, string prefix)
{ {
return Singletons<IMongoClient>.GetOrAdd(configuration, connectionString => var mongoConfiguration = config.GetRequiredValue($"{prefix}:configuration")!;
var mongoCertificate = config.GetValue<string>($"{prefix}:certificate");
var cacheKey = $"{mongoConfiguration}_{mongoCertificate}";
return Singletons<IMongoClient>.GetOrAdd(cacheKey, _ =>
{ {
return MongoClientFactory.Create(connectionString, settings => return MongoClientFactory.Create(mongoConfiguration, settings =>
{ {
if (!string.IsNullOrWhiteSpace(mongoCertificate))
{
var certFile = new X509Certificate2(mongoCertificate);
settings.SslSettings = new SslSettings
{
ClientCertificates = [certFile],
CheckCertificateRevocation = false,
ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true,
};
}
settings.ClusterConfigurator = builder => settings.ClusterConfigurator = builder =>
{ {
builder.Subscribe(new DiagnosticsActivityEventSubscriber()); builder.Subscribe(new DiagnosticsActivityEventSubscriber());

2
backend/src/Squidex.Data.MongoDb/Squidex.Data.MongoDb.csproj

@ -31,7 +31,7 @@
<PackageReference Include="Squidex.Flows.Mongo" Version="7.33.0" /> <PackageReference Include="Squidex.Flows.Mongo" Version="7.33.0" />
<PackageReference Include="Squidex.Hosting" Version="7.33.0" /> <PackageReference Include="Squidex.Hosting" Version="7.33.0" />
<PackageReference Include="Squidex.Messaging.Mongo" Version="7.33.0" /> <PackageReference Include="Squidex.Messaging.Mongo" Version="7.33.0" />
<PackageReference Include="Squidex.OpenIddict.MongoDb" Version="5.8.4" /> <PackageReference Include="Squidex.OpenIddict.MongoDb" Version="5.8.5" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="all" /> <PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="all" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" /> <PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="YDotNet.Server.MongoDB" Version="0.4.3" /> <PackageReference Include="YDotNet.Server.MongoDB" Version="0.4.3" />

9
backend/src/Squidex/appsettings.json

@ -538,6 +538,9 @@
// Read More: https://docs.mongodb.com/manual/reference/connection-string/ // Read More: https://docs.mongodb.com/manual/reference/connection-string/
"configuration": "mongodb://localhost", "configuration": "mongodb://localhost",
// The path to the certificate.
"certificate": "",
// The name of the event store database. // The name of the event store database.
"database": "SquidexAssets", "database": "SquidexAssets",
@ -582,6 +585,9 @@
// Read More: https://docs.mongodb.com/manual/reference/connection-string/ // Read More: https://docs.mongodb.com/manual/reference/connection-string/
"configuration": "mongodb://localhost", "configuration": "mongodb://localhost",
// The path to the certificate.
"certificate": "",
// The name of the event store database. // The name of the event store database.
"database": "Squidex" "database": "Squidex"
}, },
@ -627,6 +633,9 @@
// Read More: https://docs.mongodb.com/manual/reference/connection-string/ // Read More: https://docs.mongodb.com/manual/reference/connection-string/
"configuration": "mongodb://localhost", "configuration": "mongodb://localhost",
// The path to the certificate.
"certificate": "",
// The database for all your content collections (one collection per app). // The database for all your content collections (one collection per app).
"contentDatabase": "SquidexContent", "contentDatabase": "SquidexContent",

5
backend/tests/Squidex.Data.Tests/MongoDb/TestHelpers/DocumentDbFixture.cs

@ -34,13 +34,14 @@ public class DocumentDbFixture
TestConfig.Configuration.GetValue<string>("documentDb:configuration") TestConfig.Configuration.GetValue<string>("documentDb:configuration")
); );
var cert = new X509Certificate2(TestConfig.Configuration.GetValue<string>("documentDb:keyFile")!); var certPath = TestConfig.Configuration.GetValue<string>("documentDb:keyFile")!;
var certFile = new X509Certificate2(certPath);
settings.RetryWrites = false; settings.RetryWrites = false;
settings.RetryReads = false; settings.RetryReads = false;
settings.SslSettings = new SslSettings settings.SslSettings = new SslSettings
{ {
ClientCertificates = [cert], ClientCertificates = [certFile],
CheckCertificateRevocation = false, CheckCertificateRevocation = false,
ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true, ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true,
}; };

Loading…
Cancel
Save