Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass entity object at LOADING_ENTITIES #25127

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading