From d19889c589452cf187b1ce452331ab8e6724c930 Mon Sep 17 00:00:00 2001 From: Charlie Mordant Date: Wed, 8 May 2024 21:39:31 +0200 Subject: [PATCH 1/5] proof of concept regarding relationships constants --- .../support/prepare-relationship.js | 24 ++++++--- generators/entity/support/index.ts | 1 + generators/entity/support/relationships.ts | 49 +++++++++++++++++++ 3 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 generators/entity/support/relationships.ts diff --git a/generators/base-application/support/prepare-relationship.js b/generators/base-application/support/prepare-relationship.js index d57f4b82647c..af2c695322c4 100644 --- a/generators/base-application/support/prepare-relationship.js +++ b/generators/base-application/support/prepare-relationship.js @@ -21,6 +21,16 @@ import pluralize from 'pluralize'; import { databaseTypes, entityOptions, validations, checkAndReturnRelationshipOnValue } from '../../../jdl/jhipster/index.js'; import { getJoinTableName, hibernateSnakeCase } from '../../server/support/index.js'; +import { + ONE_TO_ONE, + ONE_TO_MANY, + MANY_TO_ONE, + MANY_TO_MANY, + RelationshipTypes, + RelationshipDirections, + LEFT, + RIGHT, +} from '../../entity/support/index.js'; import { stringifyApplicationData } from './debug.js'; import { mutateData } from '../../base/support/config.js'; import { prepareProperty } from './prepare-property.js'; @@ -54,13 +64,13 @@ export default function prepareRelationship(entityWithConfig, relationship, gene // Prepare basic relationship data Object.assign(relationship, { - relationshipLeftSide: relationshipSide === 'left', - relationshipRightSide: relationshipSide === 'right', - collection: relationshipType === 'one-to-many' || relationshipType === 'many-to-many', - relationshipOneToOne: relationshipType === 'one-to-one', - relationshipOneToMany: relationshipType === 'one-to-many', - relationshipManyToOne: relationshipType === 'many-to-one', - relationshipManyToMany: relationshipType === 'many-to-many', + relationshipLeftSide: relationshipSide === RelationshipDirections[LEFT], + relationshipRightSide: relationshipSide === RelationshipDirections[RIGHT], + collection: relationshipType === RelationshipTypes[ONE_TO_MANY] || relationshipType === RelationshipTypes[MANY_TO_MANY], + relationshipOneToOne: relationshipType === RelationshipTypes[ONE_TO_ONE], + relationshipOneToMany: relationshipType === RelationshipTypes[ONE_TO_MANY], + relationshipManyToOne: relationshipType === RelationshipTypes[MANY_TO_ONE], + relationshipManyToMany: relationshipType === RelationshipTypes[MANY_TO_MANY], otherEntityUser: otherEntityName === 'user', }); diff --git a/generators/entity/support/index.ts b/generators/entity/support/index.ts index 7fb102603143..1ebad24917ad 100644 --- a/generators/entity/support/index.ts +++ b/generators/entity/support/index.ts @@ -22,3 +22,4 @@ export { isSignedNumber as inputIsSignedNumber, isSignedDecimalNumber as inputIsSignedDecimalNumber, } from './asserts.js'; +export * from './relationships.js'; diff --git a/generators/entity/support/relationships.ts b/generators/entity/support/relationships.ts new file mode 100644 index 000000000000..49b6dd26d109 --- /dev/null +++ b/generators/entity/support/relationships.ts @@ -0,0 +1,49 @@ +/** + * Copyright 2013-2024 the original author or authors from the JHipster project. + * + * This file is part of the JHipster project, see https://www.jhipster.tech/ + * for more information. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +const RELATIONSHIP_VALUE_ONE_TO_ONE = 'one-to-one'; +const RELATIONSHIP_VALUE_ONE_TO_MANY = 'one-to-many'; +const RELATIONSHIP_VALUE_MANY_TO_ONE = 'many-to-one'; +const RELATIONSHIP_VALUE_MANY_TO_MANY = 'many-to-many'; + +export const ONE_TO_ONE = 'ONE_TO_ONE'; +export const ONE_TO_MANY = 'ONE_TO_MANY'; +export const MANY_TO_ONE = 'MANY_TO_ONE'; +export const MANY_TO_MANY = 'MANY_TO_MANY'; + +type JhipsterOptionRelationshipTypeValue = 'one-to-one' | 'one-to-many' | 'many-to-one' | 'many-to-many'; + +export const RelationshipTypes: Record<'ONE_TO_ONE' | 'ONE_TO_MANY' | 'MANY_TO_ONE' | 'MANY_TO_MANY', JhipsterOptionRelationshipTypeValue> = + { + ONE_TO_ONE: RELATIONSHIP_VALUE_ONE_TO_ONE, + ONE_TO_MANY: RELATIONSHIP_VALUE_ONE_TO_MANY, + MANY_TO_ONE: RELATIONSHIP_VALUE_MANY_TO_ONE, + MANY_TO_MANY: RELATIONSHIP_VALUE_MANY_TO_MANY, + }; + +const RELATIONSHIP_SIDE_VALUE_LEFT = 'left'; +const RELATIONSHIP_SIDE_VALUE_RIGHT = 'right'; +export const LEFT = 'LEFT'; +export const RIGHT = 'RIGHT'; + +type JhipsterOptionRelationshipSideValue = 'left' | 'right'; + +export const RelationshipDirections: Record<'LEFT' | 'RIGHT', JhipsterOptionRelationshipSideValue> = { + LEFT: RELATIONSHIP_SIDE_VALUE_LEFT, + RIGHT: RELATIONSHIP_SIDE_VALUE_RIGHT, +}; From 27f7ed1a3b6cea99f60074ff81e45fd0e15b0f5b Mon Sep 17 00:00:00 2001 From: Charlie Mordant Date: Wed, 8 May 2024 21:55:45 +0200 Subject: [PATCH 2/5] rename relationships types --- generators/entity/prompts.js | 18 ++++++++++-------- generators/entity/support/relationships.ts | 8 ++++---- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/generators/entity/prompts.js b/generators/entity/prompts.js index 97a50f2874f9..f6d1fe19d37a 100644 --- a/generators/entity/prompts.js +++ b/generators/entity/prompts.js @@ -28,6 +28,7 @@ import { validations, clientFrameworkTypes, } from '../../jdl/jhipster/index.js'; +import { ONE_TO_MANY, MANY_TO_MANY, ONE_TO_ONE, MANY_TO_ONE, RelationshipTypes, RelationshipDirections } from '../entity/support/index.js'; import { inputIsNumber, inputIsSignedDecimalNumber, inputIsSignedNumber } from './support/index.js'; const { isReservedPaginationWords, isReservedFieldName, isReservedTableName } = reservedKeywords; @@ -791,10 +792,10 @@ async function askForRelationship(...args) { name: 'relationshipType', message: 'What is the type of the relationship?', choices: response => [ - 'many-to-one', - 'many-to-many', - 'one-to-one', - ...(this.isBuiltInUser(response.otherEntityName) ? [] : ['one-to-many']), + RelationshipTypes[MANY_TO_ONE], + RelationshipTypes[MANY_TO_MANY], + RelationshipTypes[ONE_TO_ONE], + ...(this.isBuiltInUser(response.otherEntityName) ? [] : [RelationshipTypes[ONE_TO_MANY]]), ], default: 0, }, @@ -841,9 +842,10 @@ async function askForRelationship(...args) { }, { when: response => - (response.otherEntityName.toLowerCase() !== context.name.toLowerCase() && response.relationshipType === 'many-to-one') || - response.relationshipType === 'many-to-many' || - response.relationshipType === 'one-to-one', + (response.otherEntityName.toLowerCase() !== context.name.toLowerCase() && + response.relationshipType === RelationshipTypes[MANY_TO_ONE]) || + response.relationshipType === RelationshipTypes[MANY_TO_MANY] || + response.relationshipType === RelationshipTypes[ONE_TO_ONE], type: 'confirm', name: 'relationshipValidate', message: 'Do you want to add any validation rules to this relationship?', @@ -865,7 +867,7 @@ async function askForRelationship(...args) { ]); const relationship = { - relationshipSide: 'left', + relationshipSide: RelationshipDirections[LEFT], relationshipName: answers.relationshipName, otherEntityName: lowerFirst(answers.otherEntityName), relationshipType: answers.relationshipType, diff --git a/generators/entity/support/relationships.ts b/generators/entity/support/relationships.ts index 49b6dd26d109..cf78d0ec9811 100644 --- a/generators/entity/support/relationships.ts +++ b/generators/entity/support/relationships.ts @@ -26,9 +26,9 @@ export const ONE_TO_MANY = 'ONE_TO_MANY'; export const MANY_TO_ONE = 'MANY_TO_ONE'; export const MANY_TO_MANY = 'MANY_TO_MANY'; -type JhipsterOptionRelationshipTypeValue = 'one-to-one' | 'one-to-many' | 'many-to-one' | 'many-to-many'; +type JHipsterOptionRelationshipTypeValue = 'one-to-one' | 'one-to-many' | 'many-to-one' | 'many-to-many'; -export const RelationshipTypes: Record<'ONE_TO_ONE' | 'ONE_TO_MANY' | 'MANY_TO_ONE' | 'MANY_TO_MANY', JhipsterOptionRelationshipTypeValue> = +export const RelationshipTypes: Record<'ONE_TO_ONE' | 'ONE_TO_MANY' | 'MANY_TO_ONE' | 'MANY_TO_MANY', JHipsterOptionRelationshipTypeValue> = { ONE_TO_ONE: RELATIONSHIP_VALUE_ONE_TO_ONE, ONE_TO_MANY: RELATIONSHIP_VALUE_ONE_TO_MANY, @@ -41,9 +41,9 @@ const RELATIONSHIP_SIDE_VALUE_RIGHT = 'right'; export const LEFT = 'LEFT'; export const RIGHT = 'RIGHT'; -type JhipsterOptionRelationshipSideValue = 'left' | 'right'; +type JHipsterOptionRelationshipSideValue = 'left' | 'right'; -export const RelationshipDirections: Record<'LEFT' | 'RIGHT', JhipsterOptionRelationshipSideValue> = { +export const RelationshipDirections: Record<'LEFT' | 'RIGHT', JHipsterOptionRelationshipSideValue> = { LEFT: RELATIONSHIP_SIDE_VALUE_LEFT, RIGHT: RELATIONSHIP_SIDE_VALUE_RIGHT, }; From f255d5bc4828a032008bbfaaeb356f2f083d5312 Mon Sep 17 00:00:00 2001 From: Charlie Mordant Date: Wed, 8 May 2024 22:51:43 +0200 Subject: [PATCH 3/5] fully replaced relationship strings with constants --- .../base-application/support/prepare-entity.ts | 12 ++++++++---- generators/bootstrap-application-base/generator.ts | 6 ++++-- generators/bootstrap-application-base/utils.js | 3 ++- generators/client/support/entity-definition.js | 3 ++- generators/entity/prompts.js | 14 +++++++++++--- generators/entity/support/relationships.ts | 2 +- generators/liquibase/support/relationship.ts | 4 +++- generators/server/generator.js | 3 ++- generators/server/support/relationship.ts | 9 ++++++--- generators/spring-data-relational/generator.ts | 3 ++- jdl/basic-types/relationships.ts | 11 ----------- jdl/jhipster/relationship-types.spec.ts | 8 +++++++- jdl/jhipster/relationship-types.ts | 6 ++++-- 13 files changed, 52 insertions(+), 32 deletions(-) diff --git a/generators/base-application/support/prepare-entity.ts b/generators/base-application/support/prepare-entity.ts index cdbf571e9736..d50ff8af91e5 100644 --- a/generators/base-application/support/prepare-entity.ts +++ b/generators/base-application/support/prepare-entity.ts @@ -18,7 +18,7 @@ */ import { camelCase, kebabCase, startCase, upperFirst, sortedUniq, intersection, lowerFirst, uniq } from 'lodash-es'; import pluralize from 'pluralize'; - +import { RelationshipTypes, ONE_TO_ONE, ONE_TO_MANY, MANY_TO_MANY, MANY_TO_ONE } from '../../entity/support/index.js'; import type BaseGenerator from '../../base-core/index.js'; import { getDatabaseTypeData, hibernateSnakeCase } from '../../server/support/index.js'; import { @@ -572,7 +572,7 @@ export function preparePostEntityCommonDerivedProperties(entity: Entity) { function preparePostEntityCommonDerivedPropertiesNotTyped(entity: any) { const { relationships, fields } = entity; - const oneToOneRelationships = relationships.filter(({ relationshipType }) => relationshipType === 'one-to-one'); + const oneToOneRelationships = relationships.filter(({ relationshipType }) => relationshipType === RelationshipTypes[ONE_TO_ONE]); entity.fieldsContainNoOwnerOneToOne = oneToOneRelationships.some(({ ownerSide }) => !ownerSide); entity.anyPropertyHasValidation = @@ -618,7 +618,9 @@ function preparePostEntityCommonDerivedPropertiesNotTyped(entity: any) { }); entity.relationships.forEach(relationship => { - relationship.relationshipCollection = ['one-to-many', 'many-to-many'].includes(relationship.relationshipType); + relationship.relationshipCollection = [RelationshipTypes[ONE_TO_MANY], RelationshipTypes[MANY_TO_MANY]].includes( + relationship.relationshipType, + ); relationship.relationshipReferenceField = relationship.relationshipCollection ? relationship.relationshipFieldNamePlural : relationship.relationshipFieldName; @@ -666,7 +668,9 @@ function preparePostEntityCommonDerivedPropertiesNotTyped(entity: any) { entity.regularEagerRelations = entity.eagerRelations.filter(rel => rel.id !== true); entity.reactiveEagerRelations = entity.relationships.filter( - rel => rel.relationshipType === 'many-to-one' || (rel.relationshipType === 'one-to-one' && rel.ownerSide === true), + rel => + rel.relationshipType === RelationshipTypes[MANY_TO_ONE] || + (rel.relationshipType === RelationshipTypes[ONE_TO_ONE] && rel.ownerSide === true), ); entity.reactiveRegularEagerRelations = entity.reactiveEagerRelations.filter(rel => rel.id !== true); } diff --git a/generators/bootstrap-application-base/generator.ts b/generators/bootstrap-application-base/generator.ts index 7730f06313c6..8c82a981fdf3 100644 --- a/generators/bootstrap-application-base/generator.ts +++ b/generators/bootstrap-application-base/generator.ts @@ -41,6 +41,7 @@ import { loadLanguagesConfig } from '../languages/support/index.js'; import { loadAppConfig, loadDerivedAppConfig, loadStoredAppOptions } from '../app/support/index.js'; import { exportJDLTransform, importJDLTransform } from './support/index.js'; import command from './command.js'; +import { LEFT, MANY_TO_ONE, ONE_TO_MANY, RelationshipDirections, RelationshipTypes } from '../entity/support/index.js'; const isWin32 = os.platform() === 'win32'; @@ -333,8 +334,9 @@ export default class BootstrapApplicationBase extends BaseApplicationGenerator { if (relationship.ownerSide === undefined) { // ownerSide backward compatibility relationship.ownerSide = - relationship.relationshipType === 'many-to-one' || - (relationship.relationshipType !== 'one-to-many' && relationship.relationshipSide === 'left'); + relationship.relationshipType === RelationshipTypes[MANY_TO_ONE] || + (relationship.relationshipType !== RelationshipTypes[ONE_TO_MANY] && + relationship.relationshipSide === RelationshipDirections[LEFT]); } } } diff --git a/generators/bootstrap-application-base/utils.js b/generators/bootstrap-application-base/utils.js index 51c0287b4a90..f30e177fe609 100644 --- a/generators/bootstrap-application-base/utils.js +++ b/generators/bootstrap-application-base/utils.js @@ -22,6 +22,7 @@ import { loadRequiredConfigIntoEntity } from '../base-application/support/index. import { PaginationTypes } from '../../jdl/jhipster/entity-options.js'; import { LOGIN_REGEX, LOGIN_REGEX_JS } from '../generator-constants.js'; import { getDatabaseTypeData } from '../server/support/database.js'; +import { MANY_TO_MANY, RelationshipTypes } from '../entity/support/index.js'; const { CASSANDRA } = databaseTypes; const { OAUTH2 } = authenticationTypes; @@ -227,7 +228,7 @@ export function createUserManagementEntity(customUserManagementData = {}, applic { otherEntityName: 'Authority', relationshipName: 'authority', - relationshipType: 'many-to-many', + relationshipType: RelationshipTypes[MANY_TO_MANY], relationshipIgnoreBackReference: true, }, ]); diff --git a/generators/client/support/entity-definition.js b/generators/client/support/entity-definition.js index 2988032940b4..b204e8039fe5 100644 --- a/generators/client/support/entity-definition.js +++ b/generators/client/support/entity-definition.js @@ -21,6 +21,7 @@ import getTypescriptKeyType from './types-utils.js'; import { fieldTypes, validations, clientFrameworkTypes } from '../../../jdl/jhipster/index.js'; import { filterRelevantRelationships } from './template-utils.js'; +import { MANY_TO_MANY, ONE_TO_MANY, RelationshipTypes } from '../../entity/support/index.js'; const dbTypes = fieldTypes; const { @@ -107,7 +108,7 @@ const generateEntityClientFields = ( let fieldName; const nullable = !relationship.relationshipValidateRules || !relationship.relationshipValidateRules.includes(REQUIRED); const relationshipType = relationship.relationshipType; - if (relationshipType === 'one-to-many' || relationshipType === 'many-to-many') { + if (relationshipType === RelationshipTypes[ONE_TO_MANY] || relationshipType === RelationshipTypes[MANY_TO_MANY]) { fieldType = `I${relationship.otherEntityAngularName}[]`; fieldName = relationship.relationshipFieldNamePlural; } else { diff --git a/generators/entity/prompts.js b/generators/entity/prompts.js index f6d1fe19d37a..70a47c082e12 100644 --- a/generators/entity/prompts.js +++ b/generators/entity/prompts.js @@ -28,7 +28,15 @@ import { validations, clientFrameworkTypes, } from '../../jdl/jhipster/index.js'; -import { ONE_TO_MANY, MANY_TO_MANY, ONE_TO_ONE, MANY_TO_ONE, RelationshipTypes, RelationshipDirections } from '../entity/support/index.js'; +import { + ONE_TO_MANY, + MANY_TO_MANY, + ONE_TO_ONE, + MANY_TO_ONE, + RelationshipTypes, + RelationshipDirections, + LEFT, +} from '../entity/support/index.js'; import { inputIsNumber, inputIsSignedDecimalNumber, inputIsSignedNumber } from './support/index.js'; const { isReservedPaginationWords, isReservedFieldName, isReservedTableName } = reservedKeywords; @@ -800,7 +808,7 @@ async function askForRelationship(...args) { default: 0, }, { - when: response => application.databaseType === SQL && response.relationshipType === 'one-to-one', + when: response => application.databaseType === SQL && response.relationshipType === RelationshipTypes[ONE_TO_ONE], type: 'confirm', name: 'id', message: 'Do you want to use JPA Derived Identifier - @MapsId?', @@ -813,7 +821,7 @@ async function askForRelationship(...args) { return false; } - if (!application.databaseTypeNeo4j && answers.relationshipType !== 'many-to-one') { + if (!application.databaseTypeNeo4j && answers.relationshipType !== RelationshipTypes[MANY_TO_ONE]) { // Relationships requires bidirectional. answers.bidirectional = true; return false; diff --git a/generators/entity/support/relationships.ts b/generators/entity/support/relationships.ts index cf78d0ec9811..e5527baef11d 100644 --- a/generators/entity/support/relationships.ts +++ b/generators/entity/support/relationships.ts @@ -26,7 +26,7 @@ export const ONE_TO_MANY = 'ONE_TO_MANY'; export const MANY_TO_ONE = 'MANY_TO_ONE'; export const MANY_TO_MANY = 'MANY_TO_MANY'; -type JHipsterOptionRelationshipTypeValue = 'one-to-one' | 'one-to-many' | 'many-to-one' | 'many-to-many'; +export type JHipsterOptionRelationshipTypeValue = 'one-to-one' | 'one-to-many' | 'many-to-one' | 'many-to-many'; export const RelationshipTypes: Record<'ONE_TO_ONE' | 'ONE_TO_MANY' | 'MANY_TO_ONE' | 'MANY_TO_MANY', JHipsterOptionRelationshipTypeValue> = { diff --git a/generators/liquibase/support/relationship.ts b/generators/liquibase/support/relationship.ts index ef16b2195aa3..85a5051719c9 100644 --- a/generators/liquibase/support/relationship.ts +++ b/generators/liquibase/support/relationship.ts @@ -18,6 +18,7 @@ */ import { getFKConstraintName } from '../../server/support/index.js'; import { mutateData } from '../../base/support/index.js'; +import { RelationshipTypes, ONE_TO_ONE, MANY_TO_ONE } from '../../entity/support/index.js'; function relationshipBaseDataEquals(relationshipA, relationshipB) { return ( @@ -61,7 +62,8 @@ export function relationshipNeedsForeignKeyRecreationOnly(relationshipA, relatio export function prepareRelationshipForLiquibase(entity, relationship) { relationship.shouldWriteRelationship = - relationship.relationshipType === 'many-to-one' || (relationship.relationshipType === 'one-to-one' && relationship.ownerSide === true); + relationship.relationshipType === RelationshipTypes[MANY_TO_ONE] || + (relationship.relationshipType === RelationshipTypes[ONE_TO_ONE] && relationship.ownerSide === true); if (relationship.shouldWriteJoinTable) { const joinTableName = relationship.joinTable.name; diff --git a/generators/server/generator.js b/generators/server/generator.js index 16a28e8cc3ad..510b0655e163 100644 --- a/generators/server/generator.js +++ b/generators/server/generator.js @@ -49,6 +49,7 @@ import { JAVA_COMPATIBLE_VERSIONS, JHIPSTER_DEPENDENCIES_VERSION, } from '../generator-constants.js'; +import { RelationshipTypes, ONE_TO_ONE, MANY_TO_MANY } from '../entity/support/index.js'; import { applicationTypes, @@ -767,7 +768,7 @@ export default class JHipsterServerGenerator extends BaseApplicationGenerator { if ( relationship.relationshipSide === undefined && - (relationship.relationshipType === 'one-to-one' || relationship.relationshipType === 'many-to-many') + (relationship.relationshipType === RelationshipTypes[ONE_TO_ONE] || relationship.relationshipType === RelationshipTypes[MANY_TO_MANY]) ) { throw new Error( `relationshipSide is missing in .jhipster/${entityName}.json for relationship ${stringifyApplicationData(relationship)}`, diff --git a/generators/server/support/relationship.ts b/generators/server/support/relationship.ts index 788cfbeb298d..00d14ab78ce3 100644 --- a/generators/server/support/relationship.ts +++ b/generators/server/support/relationship.ts @@ -21,6 +21,7 @@ import { Entity } from '../../../jdl/converters/types.js'; import { addOtherRelationship } from '../../base-application/support/index.js'; import { ValidationResult } from '../../base/api.js'; import { databaseTypes } from '../../../jdl/index.js'; +import { MANY_TO_MANY, ONE_TO_MANY, ONE_TO_ONE, RelationshipTypes } from '../../entity/support/index.js'; const { NO: NO_DATABASE, SQL, NEO4J } = databaseTypes; @@ -32,10 +33,12 @@ export const addEntitiesOtherRelationships = (entities: Entity[]): ValidationRes if ( !relationship.otherRelationship && (relationship.otherEntityRelationshipName || - relationship.relationshipType === 'many-to-many' || + relationship.relationshipType === RelationshipTypes[MANY_TO_MANY] || // OneToOne back reference is required due to filtering - (relationship.relationshipType === 'one-to-one' && entity.databaseType === SQL) || - (relationship.relationshipType === 'one-to-many' && entity.databaseType !== NEO4J && entity.databaseType !== NO_DATABASE)) + (relationship.relationshipType === RelationshipTypes[ONE_TO_ONE] && entity.databaseType === SQL) || + (relationship.relationshipType === RelationshipTypes[ONE_TO_MANY] && + entity.databaseType !== NEO4J && + entity.databaseType !== NO_DATABASE)) ) { if (relationship.otherEntity.builtIn) { result.warning.push( diff --git a/generators/spring-data-relational/generator.ts b/generators/spring-data-relational/generator.ts index e36bd0f57c4b..650b88b1e7ca 100644 --- a/generators/spring-data-relational/generator.ts +++ b/generators/spring-data-relational/generator.ts @@ -27,6 +27,7 @@ import { databaseTypes } from '../../jdl/jhipster/index.js'; import { GeneratorDefinition as SpringBootGeneratorDefinition } from '../server/index.js'; import { getDBCExtraOption, getJdbcUrl, getR2dbcUrl } from './support/index.js'; import { getDatabaseDriverForDatabase, getDatabaseTypeMavenDefinition, getH2MavenDefinition } from './internal/dependencies.js'; +import { MANY_TO_MANY, RelationshipTypes } from '../entity/support/index.js'; const { SQL } = databaseTypes; @@ -85,7 +86,7 @@ export default class SqlGenerator extends BaseApplicationGenerator { - if (relationship.persistableRelationship === undefined && relationship.relationshipType === 'many-to-many') { + if (relationship.persistableRelationship === undefined && relationship.relationshipType === RelationshipTypes[MANY_TO_MANY]) { relationship.persistableRelationship = true; } }); diff --git a/jdl/basic-types/relationships.ts b/jdl/basic-types/relationships.ts index 543a0d37a05a..891dcc258479 100644 --- a/jdl/basic-types/relationships.ts +++ b/jdl/basic-types/relationships.ts @@ -3,17 +3,6 @@ export const JDL_RELATIONSHIP_ONE_TO_MANY = 'OneToMany'; export const JDL_RELATIONSHIP_MANY_TO_ONE = 'ManyToOne'; export const JDL_RELATIONSHIP_MANY_TO_MANY = 'ManyToMany'; -export const RELATIONSHIP_ONE_TO_ONE = 'one-to-one'; -export const RELATIONSHIP_ONE_TO_MANY = 'one-to-many'; -export const RELATIONSHIP_MANY_TO_ONE = 'many-to-one'; -export const RELATIONSHIP_MANY_TO_MANY = 'many-to-many'; - -export type RelationshipType = - | typeof RELATIONSHIP_ONE_TO_ONE - | typeof RELATIONSHIP_ONE_TO_MANY - | typeof RELATIONSHIP_MANY_TO_ONE - | typeof RELATIONSHIP_MANY_TO_MANY; - export type JDLRelationshipType = | typeof JDL_RELATIONSHIP_ONE_TO_ONE | typeof JDL_RELATIONSHIP_ONE_TO_MANY diff --git a/jdl/jhipster/relationship-types.spec.ts b/jdl/jhipster/relationship-types.spec.ts index e34cbe13921c..792d1e6b0bea 100644 --- a/jdl/jhipster/relationship-types.spec.ts +++ b/jdl/jhipster/relationship-types.spec.ts @@ -21,7 +21,8 @@ import { it, describe } from 'esmocha'; import { expect } from 'chai'; import { relationshipTypes } from '../jhipster/index.js'; -import { relationshipTypeExists } from './relationship-types.js'; +import { asJdlRelationshipType, relationshipTypeExists } from './relationship-types.js'; +import { RelationshipTypes, MANY_TO_MANY } from '../../generators/entity/support/index.js'; describe('jdl - RelationshipTypes', () => { describe('exists', () => { @@ -36,4 +37,9 @@ describe('jdl - RelationshipTypes', () => { }); }); }); + describe('asJdlRelationshipType', () => { + it('should convert', () => { + expect(asJdlRelationshipType(RelationshipTypes[MANY_TO_MANY])).to.be.eq(relationshipTypes.MANY_TO_MANY); + }); + }); }); diff --git a/jdl/jhipster/relationship-types.ts b/jdl/jhipster/relationship-types.ts index 68352428f5ac..f1e9c457f45b 100644 --- a/jdl/jhipster/relationship-types.ts +++ b/jdl/jhipster/relationship-types.ts @@ -17,9 +17,11 @@ * limitations under the License. */ import { camelCase, upperFirst } from 'lodash-es'; -import { JDLRelationshipType, RelationshipType, relationshipTypes } from '../basic-types/relationships.js'; +import { JDLRelationshipType, relationshipTypes } from '../basic-types/relationships.js'; +import { JHipsterOptionRelationshipTypeValue } from '../../generators/entity/support/index.js'; -export const asJdlRelationshipType = (type: RelationshipType): JDLRelationshipType => upperFirst(camelCase(type)) as JDLRelationshipType; +export const asJdlRelationshipType = (type: JHipsterOptionRelationshipTypeValue): JDLRelationshipType => + upperFirst(camelCase(type)) as JDLRelationshipType; export const relationshipTypeExists = (relationship: JDLRelationshipType) => Object.values(relationshipTypes).includes(relationship); From 6036a489779fcc09e1a4b4d387fa9c23053a891e Mon Sep 17 00:00:00 2001 From: Charlie Mordant Date: Wed, 8 May 2024 23:04:19 +0200 Subject: [PATCH 4/5] fix missing type --- generators/entity/support/relationships.ts | 8 ++++---- jdl/basic-types/relationships.ts | 13 +++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/generators/entity/support/relationships.ts b/generators/entity/support/relationships.ts index e5527baef11d..c62b3fa0b69a 100644 --- a/generators/entity/support/relationships.ts +++ b/generators/entity/support/relationships.ts @@ -16,10 +16,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -const RELATIONSHIP_VALUE_ONE_TO_ONE = 'one-to-one'; -const RELATIONSHIP_VALUE_ONE_TO_MANY = 'one-to-many'; -const RELATIONSHIP_VALUE_MANY_TO_ONE = 'many-to-one'; -const RELATIONSHIP_VALUE_MANY_TO_MANY = 'many-to-many'; +export const RELATIONSHIP_VALUE_ONE_TO_ONE = 'one-to-one'; +export const RELATIONSHIP_VALUE_ONE_TO_MANY = 'one-to-many'; +export const RELATIONSHIP_VALUE_MANY_TO_ONE = 'many-to-one'; +export const RELATIONSHIP_VALUE_MANY_TO_MANY = 'many-to-many'; export const ONE_TO_ONE = 'ONE_TO_ONE'; export const ONE_TO_MANY = 'ONE_TO_MANY'; diff --git a/jdl/basic-types/relationships.ts b/jdl/basic-types/relationships.ts index 891dcc258479..55ba6cb9074c 100644 --- a/jdl/basic-types/relationships.ts +++ b/jdl/basic-types/relationships.ts @@ -1,8 +1,21 @@ +import { + RELATIONSHIP_VALUE_ONE_TO_ONE, + RELATIONSHIP_VALUE_ONE_TO_MANY, + RELATIONSHIP_VALUE_MANY_TO_ONE, + RELATIONSHIP_VALUE_MANY_TO_MANY, +} from '../../generators/entity/support/index.js'; + export const JDL_RELATIONSHIP_ONE_TO_ONE = 'OneToOne'; export const JDL_RELATIONSHIP_ONE_TO_MANY = 'OneToMany'; export const JDL_RELATIONSHIP_MANY_TO_ONE = 'ManyToOne'; export const JDL_RELATIONSHIP_MANY_TO_MANY = 'ManyToMany'; +export type RelationshipType = + | typeof RELATIONSHIP_VALUE_ONE_TO_ONE + | typeof RELATIONSHIP_VALUE_ONE_TO_MANY + | typeof RELATIONSHIP_VALUE_MANY_TO_ONE + | typeof RELATIONSHIP_VALUE_MANY_TO_MANY; + export type JDLRelationshipType = | typeof JDL_RELATIONSHIP_ONE_TO_ONE | typeof JDL_RELATIONSHIP_ONE_TO_MANY From ee0b9c3c15f89e9d0d930ef606718db61187988a Mon Sep 17 00:00:00 2001 From: Charlie Mordant Date: Wed, 8 May 2024 23:30:48 +0200 Subject: [PATCH 5/5] more constants --- .../bootstrap-application-base/generator.ts | 8 +++++--- .../liquibase/changelog/added_entity.xml.ejs | 4 ++-- .../changelog/added_entity_constraints.xml.ejs | 15 +++++++-------- .../changelog/updated_entity_constraints.xml.ejs | 10 +++++----- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/generators/bootstrap-application-base/generator.ts b/generators/bootstrap-application-base/generator.ts index 8c82a981fdf3..61d89715f53c 100644 --- a/generators/bootstrap-application-base/generator.ts +++ b/generators/bootstrap-application-base/generator.ts @@ -41,7 +41,7 @@ import { loadLanguagesConfig } from '../languages/support/index.js'; import { loadAppConfig, loadDerivedAppConfig, loadStoredAppOptions } from '../app/support/index.js'; import { exportJDLTransform, importJDLTransform } from './support/index.js'; import command from './command.js'; -import { LEFT, MANY_TO_ONE, ONE_TO_MANY, RelationshipDirections, RelationshipTypes } from '../entity/support/index.js'; +import { LEFT, MANY_TO_ONE, ONE_TO_MANY, RelationshipDirections, RelationshipTypes, RIGHT } from '../entity/support/index.js'; const isWin32 = os.platform() === 'win32'; @@ -239,8 +239,10 @@ export default class BootstrapApplicationBase extends BaseApplicationGenerator { } else { // Missing ownerSide (one-to-many/many-to-one relationships) depends on the otherSide existence. const unidirectionalRelationship = !relationship.otherEntityRelationshipName; - const bidirectionalOneToManySide = !unidirectionalRelationship && relationship.relationshipType === 'one-to-many'; - relationship.relationshipSide = unidirectionalRelationship || bidirectionalOneToManySide ? 'left' : 'right'; + const bidirectionalOneToManySide = + !unidirectionalRelationship && relationship.relationshipType === RelationshipTypes[ONE_TO_MANY]; + relationship.relationshipSide = + unidirectionalRelationship || bidirectionalOneToManySide ? RelationshipDirections[LEFT] : RelationshipDirections[RIGHT]; } } diff --git a/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/added_entity.xml.ejs b/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/added_entity.xml.ejs index fb221aff0b6e..c90db3405d2c 100644 --- a/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/added_entity.xml.ejs +++ b/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/added_entity.xml.ejs @@ -46,10 +46,10 @@ <%_ } _%> <%_ } _%> <%_ for (relationship of relationships) { - if ((relationship.relationshipType === 'many-to-one' || (relationship.relationshipType === 'one-to-one' && relationship.ownerSide === true)) + if ((relationship.relationshipManyToOne || (relationship.relationshipOneToOne && relationship.ownerSide === true)) && !relationship.id) { relationship.otherEntity.primaryKey.fields.forEach(idField => { - const uniqueConstraintName = relationship.relationshipType === 'one-to-one' ? this.getUXConstraintName(entity.entityTableName, relationship.columnName + '_' + idField.columnName, entity.prodDatabaseType) : null; + const uniqueConstraintName = relationship.relationshipOneToOne ? this.getUXConstraintName(entity.entityTableName, relationship.columnName + '_' + idField.columnName, entity.prodDatabaseType) : null; _%> unique="true" uniqueConstraintName="<%= uniqueConstraintName %>"<% } %> /> diff --git a/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/added_entity_constraints.xml.ejs b/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/added_entity_constraints.xml.ejs index d6840075d11b..668bcbf8715b 100644 --- a/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/added_entity_constraints.xml.ejs +++ b/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/added_entity_constraints.xml.ejs @@ -22,17 +22,16 @@ --> <% for (relationship of relationships) { - const relationshipType = relationship.relationshipType, relationshipName = relationship.relationshipName, ownerSide = relationship.ownerSide, otherEntityTableName = relationship.otherEntityTableName, - onDelete = relationship.onDelete, + onDelete = relationship.onDelete, onUpdate = relationship.onUpdate; - if (relationshipType === 'many-to-one' || (relationshipType === 'one-to-one' && ownerSide)) { + if (relationship.relationshipManyToOne || (relationship.relationshipOneToOne && ownerSide)) { const constraintName = this.getFKConstraintName(entity.entityTableName, relationshipName, prodDatabaseType); let baseColumnNames; let referencedColumnNames; - if (relationshipType === 'one-to-one' && ownerSide && relationship.id === true) { + if (relationship.relationshipOneToOne && ownerSide && relationship.id === true) { baseColumnNames = relationship.otherEntity.primaryKey.fields.map(field => field.columnName).join(','); referencedColumnNames = relationship.otherEntity.primaryKey.fields.map(field => field.columnName).join(','); } else if (relationship.otherEntity) { @@ -44,7 +43,7 @@ constraintName="<%= constraintName %>" referencedColumnNames="<%= referencedColumnNames %>" referencedTableName="<%= otherEntityTableName %>" - <%_ if (onDelete) { _%> + <%_ if (onDelete) { _%> onDelete="<%= onDelete %>" <%_ } _%> <%_ if (onUpdate) { _%> @@ -58,7 +57,7 @@ constraintName="<%= relationship.joinTable.constraintName %>" referencedColumnNames="<%= entity.primaryKey.fields.map(field => field.columnName).join(', ') %>" referencedTableName="<%= entity.entityTableName %>" - <%_ if (onDelete) { _%> + <%_ if (onDelete) { _%> onDelete="<%= onDelete %>" <%_ } _%> <%_ if (onUpdate) { _%> @@ -71,10 +70,10 @@ constraintName="<%= relationship.joinTable.otherConstraintName %>" referencedColumnNames="<%= relationship.otherEntity.primaryKey.fields.map(field => field.columnName).join(', ') %>" referencedTableName="<%= relationship.otherEntity.entityTableName %>" - <%_ if (relationship.otherRelationship) { + <%_ if (relationship.otherRelationship) { // User object is not supporting this right now _%> - <%_ if (relationship.otherRelationship.onDelete) { _%> + <%_ if (relationship.otherRelationship.onDelete) { _%> onDelete="<%= relationship.otherRelationship.onDelete %>" <%_ } _%> <%_ if (relationship.otherRelationship.onUpdate) { _%> diff --git a/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/updated_entity_constraints.xml.ejs b/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/updated_entity_constraints.xml.ejs index 8936a0c18643..e163c67ee849 100644 --- a/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/updated_entity_constraints.xml.ejs +++ b/generators/liquibase/templates/src/main/resources/config/liquibase/changelog/updated_entity_constraints.xml.ejs @@ -84,7 +84,7 @@ if (hasFieldConstraint) { _%> const relationshipData = addedRelationships[idx]; if (relationshipData.unique) { const idField = relationship.otherEntity.primaryKey.ownFields[0]; - const uniqueConstraintName = relationshipData.relationshipType === 'one-to-one' ? this.getUXConstraintName(entity.entityTableName, relationshipData.columnName + '_' + idField.columnName, entity.prodDatabaseType) : null; + const uniqueConstraintName = relationshipData.relationshipOneToOne ? this.getUXConstraintName(entity.entityTableName, relationshipData.columnName + '_' + idField.columnName, entity.prodDatabaseType) : null; _%> constraintName="<%= constraintName %>" referencedColumnNames="id" referencedTableName="<%= relationship.otherEntityTableName %>" - <%_ if (onDelete) { _%> + <%_ if (onDelete) { _%> onDelete="<%= onDelete %>" <%_ } _%> <%_ if (onUpdate) { _%> @@ -139,7 +139,7 @@ if (hasFieldConstraint) { _%> constraintName="<%= constraintName %>" referencedColumnNames="<%= entity.primaryKey.fields.map(field => field.columnName).join(', ') %>" referencedTableName="<%= entityTableName %>" - <%_ if (onDelete) { _%> + <%_ if (onDelete) { _%> onDelete="<%= onDelete %>" <%_ } _%> <%_ if (onUpdate) { _%> @@ -152,10 +152,10 @@ if (hasFieldConstraint) { _%> constraintName="<%= otherEntityConstraintName %>" referencedColumnNames="<%= relationship.otherEntity.primaryKey.fields.map(field => field.columnName).join(', ') %>" referencedTableName="<%= relationship.otherEntityTableName %>" - <%_ if (relationship.otherRelationship) { + <%_ if (relationship.otherRelationship) { // User object is not supporting this right now _%> - <%_ if (relationship.otherRelationship.onDelete) { _%> + <%_ if (relationship.otherRelationship.onDelete) { _%> onDelete="<%= relationship.otherRelationship.onDelete %>" <%_ } _%> <%_ if (relationship.otherRelationship.onUpdate) { _%>