From c749766dc6f02d8459708a38f56e74bb5017ffda Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Mon, 19 Feb 2024 17:19:12 +0800 Subject: [PATCH] Allows configuration of MongoDB entity indexes --- .../MongoDB/IHasCreateCollectionOptions.cs | 8 ++++++ .../MongoDB/IHasMongoIndexManagerAction.cs | 10 ++++++++ .../Abp/MongoDB/IMongoEntityModelBuilder.cs | 10 ++++++++ .../Abp/MongoDB/MongoEntityModelBuilder.cs | 19 ++++++++++++++ .../Volo/Abp/MongoDB/MongoModelBuilder.cs | 25 ++++++++++++++----- 5 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IHasCreateCollectionOptions.cs create mode 100644 framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IHasMongoIndexManagerAction.cs diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IHasCreateCollectionOptions.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IHasCreateCollectionOptions.cs new file mode 100644 index 0000000000..ce4a08e9cb --- /dev/null +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IHasCreateCollectionOptions.cs @@ -0,0 +1,8 @@ +using MongoDB.Driver; + +namespace Volo.Abp.MongoDB; + +public interface IHasCreateCollectionOptions +{ + CreateCollectionOptions GetCreateCollectionOptions(); +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IHasMongoIndexManagerAction.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IHasMongoIndexManagerAction.cs new file mode 100644 index 0000000000..54c2970b93 --- /dev/null +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IHasMongoIndexManagerAction.cs @@ -0,0 +1,10 @@ +using System; +using MongoDB.Bson; +using MongoDB.Driver; + +namespace Volo.Abp.MongoDB; + +public interface IHasMongoIndexManagerAction +{ + Action>? IndexesAction { get; set; } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoEntityModelBuilder.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoEntityModelBuilder.cs index 673f1f4288..923cd0aaef 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoEntityModelBuilder.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/IMongoEntityModelBuilder.cs @@ -1,5 +1,7 @@ using System; +using MongoDB.Bson; using MongoDB.Bson.Serialization; +using MongoDB.Driver; namespace Volo.Abp.MongoDB; @@ -10,6 +12,10 @@ public interface IMongoEntityModelBuilder string CollectionName { get; set; } BsonClassMap BsonMap { get; } + + CreateCollectionOptions CreateCollectionOptions { get; } + + Action>? IndexesAction { get; set; } } public interface IMongoEntityModelBuilder @@ -19,4 +25,8 @@ public interface IMongoEntityModelBuilder string CollectionName { get; set; } BsonClassMap BsonMap { get; } + + CreateCollectionOptions CreateCollectionOptions { get; } + + Action>? IndexesAction { get; set; } } diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoEntityModelBuilder.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoEntityModelBuilder.cs index e012e89072..6d367703f5 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoEntityModelBuilder.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoEntityModelBuilder.cs @@ -1,27 +1,41 @@ using MongoDB.Bson.Serialization; using System; +using MongoDB.Bson; +using MongoDB.Driver; namespace Volo.Abp.MongoDB; public class MongoEntityModelBuilder : IMongoEntityModel, IHasBsonClassMap, + IHasCreateCollectionOptions, + IHasMongoIndexManagerAction, IMongoEntityModelBuilder, IMongoEntityModelBuilder { public Type EntityType { get; } public string CollectionName { get; set; } = default!; + + public Action>? IndexesAction { get; set; } BsonClassMap IMongoEntityModelBuilder.BsonMap => _bsonClassMap; + BsonClassMap IMongoEntityModelBuilder.BsonMap => _bsonClassMap; private readonly BsonClassMap _bsonClassMap; + + CreateCollectionOptions IMongoEntityModelBuilder.CreateCollectionOptions => _createCollectionOptions; + + CreateCollectionOptions IMongoEntityModelBuilder.CreateCollectionOptions => _createCollectionOptions; + + private readonly CreateCollectionOptions _createCollectionOptions; public MongoEntityModelBuilder() { EntityType = typeof(TEntity); _bsonClassMap = new BsonClassMap(); + _createCollectionOptions = new CreateCollectionOptions(); _bsonClassMap.ConfigureAbpConventions(); } @@ -29,4 +43,9 @@ public class MongoEntityModelBuilder : { return _bsonClassMap; } + + public CreateCollectionOptions GetCreateCollectionOptions() + { + return _createCollectionOptions; + } } diff --git a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelBuilder.cs b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelBuilder.cs index b7de3043ac..5814cd2b26 100644 --- a/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelBuilder.cs +++ b/framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/MongoModelBuilder.cs @@ -74,7 +74,10 @@ public class MongoModelBuilder : IMongoModelBuilder baseClasses.AddRange(entityModel.EntityType.GetBaseClasses(includeObject: false)); - CreateCollectionIfNotExists(dbContext, entityModel.CollectionName); + var createCollectionOptions = entityModel.As().GetCreateCollectionOptions(); + var indexesAction = entityModel.As().IndexesAction; + CreateCollectionIfNotExists(dbContext, entityModel.CollectionName, createCollectionOptions); + CreateCollectionIndexes(dbContext, entityModel.CollectionName, indexesAction); } baseClasses = baseClasses.Distinct().ToList(); @@ -141,7 +144,7 @@ public class MongoModelBuilder : IMongoModelBuilder typeof(MongoEntityModelBuilder<>).MakeGenericType(entityType) )! ); - + buildAction?.Invoke(model); } @@ -150,14 +153,24 @@ public class MongoModelBuilder : IMongoModelBuilder return _entityModelBuilders.Values.Cast().ToImmutableList(); } - protected virtual void CreateCollectionIfNotExists(AbpMongoDbContext dbContext, string collectionName) + protected virtual void CreateCollectionIfNotExists(AbpMongoDbContext dbContext, string collectionName, CreateCollectionOptions createCollectionOptions) { var filter = new BsonDocument("name", collectionName); - var options = new ListCollectionNamesOptions { Filter = filter }; + var options = new ListCollectionsOptions { Filter = filter }; + + if (!dbContext.Database.ListCollections(options).Any()) + { + dbContext.Database.CreateCollection(collectionName, createCollectionOptions); + } + } - if (!dbContext.Database.ListCollectionNames(options).Any()) + protected virtual void CreateCollectionIndexes(AbpMongoDbContext dbContext, string collectionName, Action>? indexesAction = null) + { + var collection = dbContext.Database.GetCollection(collectionName); + + if (collection != null) { - dbContext.Database.CreateCollection(collectionName); + indexesAction?.Invoke(collection.Indexes); } } }