From ed0ef18a9bb742f82da71f3da7b1df0f2eb0f5e5 Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Wed, 7 Feb 2024 16:45:27 -0300 Subject: [PATCH] pass entity object at LOADING_ENTITIES --- generators/base-application/generator.ts | 34 ++++++++++- generators/base-application/tasks.d.ts | 2 + .../bootstrap-application-base/generator.ts | 60 +++++++++---------- 3 files changed, 65 insertions(+), 31 deletions(-) diff --git a/generators/base-application/generator.ts b/generators/base-application/generator.ts index 8a78095812c2..7fcfaa61e1a3 100644 --- a/generators/base-application/generator.ts +++ b/generators/base-application/generator.ts @@ -443,7 +443,39 @@ export default class BaseApplicationGenerator< * @returns {string[]} */ getEntitiesDataToLoad() { - return this.getExistingEntityNames().map(entityName => ({ entityName, entityStorage: this.getEntityConfig(entityName, true) })); + const application = this.sharedData.getApplication(); + const builtInEntities: string[] = []; + if (application.generateBuiltInUserEntity) { + // Reorder User entity to be the first one to be loaded + builtInEntities.push('User'); + } + if (application.generateBuiltInUserEntity && application.generateUserManagement) { + // Reorder User entity to be the first one to be loaded + builtInEntities.push('UserManagement'); + } + if (application.generateBuiltInAuthorityEntity) { + // Reorder User entity to be the first one to be loaded + builtInEntities.push('Authority'); + } + const entitiesToLoad = [...new Set([...builtInEntities, ...this.getExistingEntityNames()])]; + return entitiesToLoad.map(entityName => { + // eslint-disable-next-line @typescript-eslint/no-this-alias + const generator = this; + if (!this.sharedData.hasEntity(entityName)) { + this.sharedData.setEntity(entityName, { name: entityName }); + } + const entityBootstrap = this.sharedData.getEntity(entityName); + return { + entityName, + get entityStorage() { + return generator.getEntityConfig(entityName, true); + }, + get entityConfig() { + return generator.getEntityConfig(entityName, true)!.createProxy(); + }, + entityBootstrap, + }; + }); } /** diff --git a/generators/base-application/tasks.d.ts b/generators/base-application/tasks.d.ts index 4c52080b25f1..6b1552c07287 100644 --- a/generators/base-application/tasks.d.ts +++ b/generators/base-application/tasks.d.ts @@ -25,6 +25,8 @@ type LoadingEntitiesTaskParam = { entityStorage: Storage; /** Proxy object for the entitystorage */ entityConfig: Record; + /** Initial entity object */ + entityBootstrap: Record; }[]; }; diff --git a/generators/bootstrap-application-base/generator.ts b/generators/bootstrap-application-base/generator.ts index 78b1fc4ad7b5..334418965b18 100644 --- a/generators/bootstrap-application-base/generator.ts +++ b/generators/bootstrap-application-base/generator.ts @@ -222,61 +222,61 @@ export default class BootstrapApplicationBase extends BaseApplicationGenerator { return this.asLoadingEntitiesTaskGroup({ loadUser({ application, entitiesToLoad }) { if (application.generateBuiltInUserEntity) { - if (this.sharedData.hasEntity('User')) { - throw new Error("Fail to bootstrap 'User', already exists."); + const User = 'User'; + + const customUser = entitiesToLoad.find(entityToLoad => entityToLoad.entityName === User); + const bootstrap = customUser?.entityBootstrap; + if (!bootstrap) { + throw new Error('User entity should already be passed.'); } - const customUser = entitiesToLoad.find(entityToLoad => entityToLoad.entityName === 'User'); const customUserData: any = customUser?.entityStorage.getAll() ?? {}; - const user = createUserEntity.call(this, { ...customUserData, ...customUserData.annotations }, application); - this.sharedData.setEntity('User', user); - application.user = user; + Object.assign(bootstrap!, createUserEntity.call(this, { ...customUserData, ...customUserData.annotations }, application)); + application.user = bootstrap; } }, loadUserManagement({ application, entitiesToLoad }) { if (application.generateBuiltInUserEntity && application.generateUserManagement) { - if (this.sharedData.hasEntity('UserManagement')) { - throw new Error("Fail to bootstrap 'User', already exists."); + const UserManagement = 'UserManagement'; + const customUserManagement = entitiesToLoad.find(entityToLoad => entityToLoad.entityName === UserManagement); + const bootstrap = customUserManagement?.entityBootstrap; + if (!bootstrap) { + throw new Error('UserManagement entity should already be passed.'); } - const customUserManagement = entitiesToLoad.find(entityToLoad => entityToLoad.entityName === 'UserManagement'); const customUserManagementData: any = customUserManagement?.entityStorage.getAll() ?? {}; - - const userManagement = createUserManagementEntity.call( - this, - { ...customUserManagementData, ...customUserManagementData.annotations }, - application, + Object.assign( + bootstrap!, + createUserManagementEntity.call(this, { ...customUserManagementData, ...customUserManagementData.annotations }, application), ); - this.sharedData.setEntity('UserManagement', userManagement); - application.userManagement = userManagement; + application.userManagement = bootstrap; } }, loadAuthority({ application, entitiesToLoad }) { if (application.generateBuiltInAuthorityEntity) { const authority = 'Authority'; - if (this.sharedData.hasEntity(authority)) { - throw new Error(`Fail to bootstrap '${authority}', already exists.`); - } const customEntity = entitiesToLoad.find(entityToLoad => entityToLoad.entityName === authority); + const bootstrap = customEntity?.entityBootstrap; + if (!bootstrap) { + throw new Error('Authority entity should already be passed.'); + } + const customEntityData: any = customEntity?.entityStorage.getAll() ?? {}; - const authorityEntity = createAuthorityEntity.call(this, { ...customEntityData, ...customEntityData.annotations }, application); - this.sharedData.setEntity(authority, authorityEntity); - application.authority = authorityEntity; + Object.assign( + bootstrap!, + createAuthorityEntity.call(this, { ...customEntityData, ...customEntityData.annotations }, application), + ); + application.authority = bootstrap; } }, loadingEntities({ application, entitiesToLoad }) { - for (const { entityName, entityStorage } of entitiesToLoad) { - if (this.sharedData.hasEntity(entityName)) { - const existingEntity = this.sharedData.getEntity(entityName); - if (!existingEntity.builtIn) { - throw new Error(`Fail to bootstrap '${entityName}', already exists.`); - } - } else { + for (const { entityName, entityBootstrap, entityStorage } of entitiesToLoad) { + if (!entityBootstrap.builtIn) { let entity = entityStorage.getAll() as any; entity.name = entity.name ?? entityName; entity = { ...entity, ...entity.annotations }; - this.sharedData.setEntity(entityName, entity); + Object.assign(entityBootstrap, entity); } }