From 16394c6f07540bb428a1a57ef403ed693d7dc356 Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Thu, 21 May 2020 11:18:05 +0200 Subject: [PATCH] move raven collection prefix into options, so its customizable --- zero.Core/Api/ApplicationsApi.cs | 4 +- zero.Core/Api/BackofficeStore.cs | 2 +- zero.Core/Api/SetupApi.cs | 13 +---- zero.Core/Api/Token.cs | 4 +- zero.Core/Constants.cs | 1 - .../Extensions/DocumentStoreExtensions.cs | 53 +++++++++++++++++++ zero.Core/Options/ZeroOptions.cs | 7 +++ zero.Debug/Controllers/HomeController.cs | 16 +++--- zero.Web.UI/App/navigation.vue | 2 +- zero.Web/Controllers/UsersController.cs | 2 +- zero.Web/ZeroBuilder.cs | 41 ++------------ 11 files changed, 81 insertions(+), 64 deletions(-) diff --git a/zero.Core/Api/ApplicationsApi.cs b/zero.Core/Api/ApplicationsApi.cs index 1173f485..749005a8 100644 --- a/zero.Core/Api/ApplicationsApi.cs +++ b/zero.Core/Api/ApplicationsApi.cs @@ -10,10 +10,10 @@ namespace zero.Core.Api { public class ApplicationsApi : IApplicationsApi { - protected IAppAwareBackofficeStore Backoffice { get; private set; } + protected IBackofficeStore Backoffice { get; private set; } - public ApplicationsApi(IAppAwareBackofficeStore backoffice) + public ApplicationsApi(IBackofficeStore backoffice) { Backoffice = backoffice; } diff --git a/zero.Core/Api/BackofficeStore.cs b/zero.Core/Api/BackofficeStore.cs index 9f6ee1dc..a480ba2e 100644 --- a/zero.Core/Api/BackofficeStore.cs +++ b/zero.Core/Api/BackofficeStore.cs @@ -242,7 +242,7 @@ namespace zero.Core.Api public AppAwareBackofficeStore(IDocumentStore raven, IMediaUpload media, IHttpContextAccessor httpContextAccessor) : base(raven, media) { Claim appIdClaim = httpContextAccessor.HttpContext?.User.Claims.FirstOrDefault(x => x.Type == Constants.Auth.Claims.CurrentAppId); - AppId = appIdClaim.Value; + AppId = appIdClaim?.Value; } } } diff --git a/zero.Core/Api/SetupApi.cs b/zero.Core/Api/SetupApi.cs index 9f238421..5110b002 100644 --- a/zero.Core/Api/SetupApi.cs +++ b/zero.Core/Api/SetupApi.cs @@ -56,18 +56,7 @@ namespace zero.Core.Api Database = model.Database.Name }; - raven.Conventions.IdentityPartsSeparator = "."; - raven.Conventions.FindCollectionName = type => - { - return Constants.Database.CollectionPrefix + DocumentConventions.DefaultGetCollectionName(type); - }; - raven.Conventions.TransformTypeCollectionNameToDocumentIdPrefix = name => - { - return name.Replace(Constants.Database.CollectionPrefix, Constants.Database.CollectionPrefix + raven.Conventions.IdentityPartsSeparator).ToCamelCaseId(); - }; - - - raven.Initialize(); + raven.Setup(Options).Initialize(); // create application Application app = new Application() diff --git a/zero.Core/Api/Token.cs b/zero.Core/Api/Token.cs index 01d6949b..3f11d0db 100644 --- a/zero.Core/Api/Token.cs +++ b/zero.Core/Api/Token.cs @@ -14,7 +14,7 @@ namespace zero.Core.Api protected IZeroOptions Options { get; private set; } - private const string PREFIX = "ChangeTokens."; + private const string PREFIX = "changeTokens"; public Token(IDocumentStore raven, IZeroOptions options) @@ -68,7 +68,7 @@ namespace zero.Core.Api ChangeToken token = new ChangeToken() { - Id = Constants.Database.CollectionPrefix + PREFIX + Guid.NewGuid(), + Id = Options.Raven.CollectionPrefix.EnsureEndsWith(Raven.Conventions.IdentityPartsSeparator) + PREFIX.EnsureEndsWith(Raven.Conventions.IdentityPartsSeparator) + Guid.NewGuid(), ReferenceId = entityId }; diff --git a/zero.Core/Constants.cs b/zero.Core/Constants.cs index 9ac63e5d..7edfab27 100644 --- a/zero.Core/Constants.cs +++ b/zero.Core/Constants.cs @@ -31,7 +31,6 @@ public static class Database { public const string SharedAppId = "shared"; - public const string CollectionPrefix = "zero"; // TODO initially we had a dot (zero.) suffix here, but that did not work out anymore when it comes to index creation public const string ReservationPrefix = "zero."; public const string Expires = Raven.Client.Constants.Documents.Metadata.Expires; } diff --git a/zero.Core/Extensions/DocumentStoreExtensions.cs b/zero.Core/Extensions/DocumentStoreExtensions.cs index f470f283..dd7a8466 100644 --- a/zero.Core/Extensions/DocumentStoreExtensions.cs +++ b/zero.Core/Extensions/DocumentStoreExtensions.cs @@ -1,13 +1,66 @@ using Raven.Client.Documents; +using Raven.Client.Documents.Conventions; using Raven.Client.Documents.Operations.CompareExchange; using System; +using System.Linq; +using System.Reflection; using System.Threading.Tasks; +using zero.Core.Attributes; +using zero.Core.Entities; +using zero.Core.Options; using zero.Core.Utils; namespace zero.Core.Extensions { public static class DocumentStoreExtensions { + /// + /// Setup conventions for the document store + /// + public static IDocumentStore Setup(this IDocumentStore store, IZeroOptions options) + { + Type[] polymorphTypes = new Type[2] { typeof(SpaceContent), typeof(Page) }; + + + store.Conventions.IdentityPartsSeparator = "."; + + store.Conventions.FindCollectionName = type => + { + // do not alter non-internal entities + if (!typeof(IZeroDbConventions).IsAssignableFrom(type)) + { + return DocumentConventions.DefaultGetCollectionName(type); + } + + // use name from attribute if available + CollectionAttribute collection = type.GetCustomAttribute(); + if (collection != null) + { + return collection.Name; + } + + // use base type for polymorphism + Type polymorphBaseType = polymorphTypes.FirstOrDefault(x => type.IsSubclassOf(x)); + Type finalType = polymorphBaseType ?? type; + + return options.Raven.CollectionPrefix + DocumentConventions.DefaultGetCollectionName(finalType); + }; + + + store.Conventions.TransformTypeCollectionNameToDocumentIdPrefix = name => + { + if (!options.Raven.CollectionPrefix.IsNullOrWhiteSpace()) + { + name = options.Raven.CollectionPrefix.EnsureEndsWith(store.Conventions.IdentityPartsSeparator) + name.TrimStart(options.Raven.CollectionPrefix); + } + + return name.ToCamelCaseId(); + }; + + return store; + } + + /// /// Create a new unique Id /// diff --git a/zero.Core/Options/ZeroOptions.cs b/zero.Core/Options/ZeroOptions.cs index 033809c8..fd9dc401 100644 --- a/zero.Core/Options/ZeroOptions.cs +++ b/zero.Core/Options/ZeroOptions.cs @@ -1,4 +1,5 @@ using FluentValidation; +using System; using System.Collections.Generic; using zero.Core.Entities; using zero.Core.Mapper; @@ -15,6 +16,10 @@ namespace zero.Core.Options DefaultLanguage = SupportedLanguages[0]; TokenExpiration = 60; BackofficePath = "/zero"; + Raven = new RavenOptions() + { + CollectionPrefix = String.Empty + }; Sections = new SectionOptions(); Features = new FeatureOptions(); Pages = new PageOptions(); @@ -92,6 +97,8 @@ namespace zero.Core.Options public string Url { get; set; } public string Database { get; set; } + + public string CollectionPrefix { get; set; } } diff --git a/zero.Debug/Controllers/HomeController.cs b/zero.Debug/Controllers/HomeController.cs index fef903be..655948d6 100644 --- a/zero.Debug/Controllers/HomeController.cs +++ b/zero.Debug/Controllers/HomeController.cs @@ -1,8 +1,10 @@ using Microsoft.AspNetCore.Mvc; using Raven.Client.Documents; +using Raven.Client.Documents.Linq; using Raven.Client.Documents.Session; using System; using System.Threading.Tasks; +using zero.Core.Entities; using zero.TestData; namespace zero.Debug.Controllers @@ -20,6 +22,8 @@ namespace zero.Debug.Controllers { using (IAsyncDocumentSession session = raven.OpenAsyncSession()) { + Application app = await session.Query().Where(x => x.IsActive).FirstOrDefaultAsync(); + await session.StoreAsync(new ContentPage() { CreatedDate = DateTimeOffset.Now, @@ -28,7 +32,7 @@ namespace zero.Debug.Controllers IsActive = true, PageTypeAlias = "root", Text = "This is a text", - AppId = "zero.applications.1-A", + AppId = app.Id, Meta = new MetaPagePartial() { TitleOverrideAll = "brothers Klika OG" @@ -47,7 +51,7 @@ namespace zero.Debug.Controllers IsActive = true, PageTypeAlias = "content", Text = "Our products page", - AppId = "zero.applications.1-A" + AppId = app.Id }); ContentPage newsPage = new ContentPage() @@ -58,7 +62,7 @@ namespace zero.Debug.Controllers IsActive = true, PageTypeAlias = "content", Text = "1 out of 100 news are good", - AppId = "zero.applications.1-A" + AppId = app.Id }; await session.StoreAsync(newsPage); @@ -72,7 +76,7 @@ namespace zero.Debug.Controllers PageTypeAlias = "news", Text = "What the fuckk", PublishDate = DateTimeOffset.Now.AddDays(3), - AppId = "zero.applications.1-A", + AppId = app.Id, ParentId = newsPage.Id }); @@ -85,7 +89,7 @@ namespace zero.Debug.Controllers PageTypeAlias = "news", Text = "What the fuckkii", PublishDate = DateTimeOffset.Now.AddDays(-20), - AppId = "zero.applications.1-A", + AppId = app.Id, ParentId = newsPage.Id }); @@ -96,7 +100,7 @@ namespace zero.Debug.Controllers Alias = "xxx", IsActive = true, PageTypeAlias = "redirect", - AppId = "zero.applications.1-A", + AppId = app.Id, ParentId = newsPage.Id, Link = "https://brothers.studio" }); diff --git a/zero.Web.UI/App/navigation.vue b/zero.Web.UI/App/navigation.vue index 48dd6158..d77948df 100644 --- a/zero.Web.UI/App/navigation.vue +++ b/zero.Web.UI/App/navigation.vue @@ -5,7 +5,7 @@ diff --git a/zero.Web/Controllers/UsersController.cs b/zero.Web/Controllers/UsersController.cs index 62934494..a8e2a64b 100644 --- a/zero.Web/Controllers/UsersController.cs +++ b/zero.Web/Controllers/UsersController.cs @@ -51,7 +51,7 @@ namespace zero.Web.Controllers /// public async Task GetAll([FromQuery] ListQuery query) { - return await As(await Api.GetByQuery(query, "zero.applications.1-A")); + return await As(await Api.GetByQuery(query)); // TODO , "zero.applications.1-A" } diff --git a/zero.Web/ZeroBuilder.cs b/zero.Web/ZeroBuilder.cs index c51b596a..55171617 100644 --- a/zero.Web/ZeroBuilder.cs +++ b/zero.Web/ZeroBuilder.cs @@ -11,6 +11,7 @@ using Raven.Client.Documents; using Raven.Client.Documents.Conventions; using Raven.Client.Documents.Indexes; using System; +using System.Linq; using System.Reflection; using System.Threading.Tasks; using zero.Core; @@ -157,45 +158,9 @@ namespace zero.Web Database = options.Raven.Database }; - store.Conventions.IdentityPartsSeparator = "."; - - store.Conventions.FindCollectionName = type => - { - if (!typeof(IZeroDbConventions).IsAssignableFrom(type)) - { - return DocumentConventions.DefaultGetCollectionName(type); - } - - CollectionAttribute collection = type.GetCustomAttribute(); - - if (collection != null) - { - return collection.Name; - } - - Type finalType = type; - - if (type.IsSubclassOf(typeof(SpaceContent))) - { - finalType = typeof(SpaceContent); - } - if (type.IsSubclassOf(typeof(Page))) - { - finalType = typeof(Page); - } - - return Constants.Database.CollectionPrefix + DocumentConventions.DefaultGetCollectionName(finalType); - }; - - store.Conventions.TransformTypeCollectionNameToDocumentIdPrefix = name => - { - return name.Replace(Constants.Database.CollectionPrefix, Constants.Database.CollectionPrefix + store.Conventions.IdentityPartsSeparator).ToCamelCaseId(); - }; - - - - IDocumentStore raven = store.Initialize(); + IDocumentStore raven = store.Setup(options).Initialize(); + // create all indexes IndexCreation.CreateIndexes(Assembly.GetAssembly(typeof(MediaFolder_ByHierarchy)), store); return raven;