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

Relationships constants in generators #26075

Closed
wants to merge 5 commits into from
Closed
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
12 changes: 8 additions & 4 deletions generators/base-application/support/prepare-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
24 changes: 17 additions & 7 deletions generators/base-application/support/prepare-relationship.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
});

Expand Down
12 changes: 8 additions & 4 deletions generators/bootstrap-application-base/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, RIGHT } from '../entity/support/index.js';

const isWin32 = os.platform() === 'win32';

Expand Down Expand Up @@ -238,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];
}
}

Expand Down Expand Up @@ -333,8 +336,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]);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion generators/bootstrap-application-base/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -227,7 +228,7 @@ export function createUserManagementEntity(customUserManagementData = {}, applic
{
otherEntityName: 'Authority',
relationshipName: 'authority',
relationshipType: 'many-to-many',
relationshipType: RelationshipTypes[MANY_TO_MANY],
relationshipIgnoreBackReference: true,
},
]);
Expand Down
3 changes: 2 additions & 1 deletion generators/client/support/entity-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
30 changes: 20 additions & 10 deletions generators/entity/prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +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,
LEFT,
} from '../entity/support/index.js';
import { inputIsNumber, inputIsSignedDecimalNumber, inputIsSignedNumber } from './support/index.js';

const { isReservedPaginationWords, isReservedFieldName, isReservedTableName } = reservedKeywords;
Expand Down Expand Up @@ -791,15 +800,15 @@ 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,
},
{
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?',
Expand All @@ -812,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;
Expand Down Expand Up @@ -841,9 +850,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?',
Expand All @@ -865,7 +875,7 @@ async function askForRelationship(...args) {
]);

const relationship = {
relationshipSide: 'left',
relationshipSide: RelationshipDirections[LEFT],
relationshipName: answers.relationshipName,
otherEntityName: lowerFirst(answers.otherEntityName),
relationshipType: answers.relationshipType,
Expand Down
1 change: 1 addition & 0 deletions generators/entity/support/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export {
isSignedNumber as inputIsSignedNumber,
isSignedDecimalNumber as inputIsSignedDecimalNumber,
} from './asserts.js';
export * from './relationships.js';
49 changes: 49 additions & 0 deletions generators/entity/support/relationships.ts
Original file line number Diff line number Diff line change
@@ -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.
*/
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';
export const MANY_TO_ONE = 'MANY_TO_ONE';
export const MANY_TO_MANY = '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> =
{
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,
};
4 changes: 3 additions & 1 deletion generators/liquibase/support/relationship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
_%>
<column name="<%= relationship.columnName %>_<%= idField.columnName %>" type="<%= idField.columnType %>">
<constraints nullable="<%= !relationship.columnRequired %>"<% if (uniqueConstraintName) { %> unique="true" uniqueConstraintName="<%= uniqueConstraintName %>"<% } %> />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@
-->
<changeSet id="<%= changelogDate %>-2" author="jhipster">
<% 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) {
Expand All @@ -44,7 +43,7 @@
constraintName="<%= constraintName %>"
referencedColumnNames="<%= referencedColumnNames %>"
referencedTableName="<%= otherEntityTableName %>"
<%_ if (onDelete) { _%>
<%_ if (onDelete) { _%>
onDelete="<%= onDelete %>"
<%_ } _%>
<%_ if (onUpdate) { _%>
Expand All @@ -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) { _%>
Expand All @@ -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) { _%>
Expand Down
Loading
Loading