Skip to content

Commit

Permalink
Merge pull request #25127 from mshima/loading-entities
Browse files Browse the repository at this point in the history
pass entity object at LOADING_ENTITIES
  • Loading branch information
DanielFran authored Feb 8, 2024
2 parents 2192f0e + ed0ef18 commit 3639bc6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 31 deletions.
34 changes: 33 additions & 1 deletion generators/base-application/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
});
}

/**
Expand Down
2 changes: 2 additions & 0 deletions generators/base-application/tasks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type LoadingEntitiesTaskParam = {
entityStorage: Storage;
/** Proxy object for the entitystorage */
entityConfig: Record<string, any>;
/** Initial entity object */
entityBootstrap: Record<string, any>;
}[];
};

Expand Down
60 changes: 30 additions & 30 deletions generators/bootstrap-application-base/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 3639bc6

Please sign in to comment.