From f4689f54685956720203852a10ba27718fcd56dd Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Wed, 18 Sep 2024 12:09:15 -0300 Subject: [PATCH] adjusts --- .../base-application/support/prepare-field.ts | 18 ++------- .../support/prepare-property.ts | 4 +- .../bootstrap-application-base/generator.ts | 11 ++++++ generators/server/generator.ts | 8 +--- lib/application/field-types.ts | 39 ++++++++++++------- 5 files changed, 44 insertions(+), 36 deletions(-) diff --git a/generators/base-application/support/prepare-field.ts b/generators/base-application/support/prepare-field.ts index 80d91329ae3a..82005f32c9a7 100644 --- a/generators/base-application/support/prepare-field.ts +++ b/generators/base-application/support/prepare-field.ts @@ -24,7 +24,7 @@ import { mutateData } from '../../../lib/utils/object.js'; import type CoreGenerator from '../../base-core/generator.js'; import type { Field } from '../../../lib/types/application/field.js'; import type { Entity } from '../../../lib/types/application/entity.js'; -import { fieldTypeValues, isBlobType, isFieldEnum } from '../../../lib/application/field-types.js'; +import { fieldTypeValues, isFieldEnumType } from '../../../lib/application/field-types.js'; import { prepareProperty } from './prepare-property.js'; const { BlobTypes, CommonDBTypes, RelationalOnlyDBTypes } = fieldTypes; @@ -283,17 +283,6 @@ function prepareCommonFieldForTemplates(entityWithConfig: Entity, field: Field, tsType: ({ fieldType }) => getTypescriptType(fieldType), }); - if (isBlobType(field.fieldType)) { - field.fieldType = 'byte[]'; - if (field.fieldType === 'ImageBlob') { - field.fieldTypeBlobContent = 'image'; - } else if (field.fieldType === 'TextBlob') { - field.fieldTypeBlobContent = 'text'; - } else if (field.fieldType === 'AnyBlob') { - field.fieldTypeBlobContent = 'any'; - } - } - prepareProperty(field); defaults(field, { @@ -301,11 +290,12 @@ function prepareCommonFieldForTemplates(entityWithConfig: Entity, field: Field, }); const fieldType = field.fieldType; - if (isFieldEnum(field)) { + const fieldIsEnum = isFieldEnumType(field); + field.fieldIsEnum = fieldIsEnum; + if (fieldIsEnum) { if (fieldTypeValues.includes(fieldType)) { throw new Error(`Field type '${fieldType}' is a reserved keyword and can't be used as an enum name.`); } - field.fieldIsEnum = true; field.enumFileName = kebabCase(field.fieldType); field.enumValues = getEnumValuesWithCustomValues(field.fieldValues!); } diff --git a/generators/base-application/support/prepare-property.ts b/generators/base-application/support/prepare-property.ts index be50b15cc864..bd156f3b5135 100644 --- a/generators/base-application/support/prepare-property.ts +++ b/generators/base-application/support/prepare-property.ts @@ -18,8 +18,10 @@ */ import { snakeCase, upperFirst } from 'lodash-es'; import { mutateData } from '../../../lib/utils/object.js'; +import type { Field } from '../../../lib/types/application/field.js'; +import type { Relationship } from '../../../lib/types/application/relationship.js'; -export const prepareProperty = (property: any) => { +export const prepareProperty = (property: Field | Relationship) => { mutateData(property, { __override__: false, propertyNameCapitalized: ({ propertyName }) => upperFirst(propertyName), diff --git a/generators/bootstrap-application-base/generator.ts b/generators/bootstrap-application-base/generator.ts index 376490ce2608..558307b636f8 100644 --- a/generators/bootstrap-application-base/generator.ts +++ b/generators/bootstrap-application-base/generator.ts @@ -43,6 +43,7 @@ import { lookupCommandsConfigs } from '../../lib/command/lookup-commands-configs import { loadCommandConfigsIntoApplication, loadCommandConfigsKeysIntoTemplatesContext } from '../../lib/command/load.js'; import { getConfigWithDefaults } from '../../lib/jhipster/default-application-options.js'; import { removeFieldsWithNullishValues } from '../base/support/index.js'; +import { getBlobContentType, isFieldBinaryType, isFieldBlobType } from '../../lib/application/field-types.js'; import { createAuthorityEntity, createUserEntity, createUserManagementEntity } from './utils.js'; import { exportJDLTransform, importJDLTransform } from './support/index.js'; @@ -360,6 +361,16 @@ export default class BootstrapApplicationBase extends BaseApplicationGenerator { this.validateResult(loadEntitiesOtherSide(entities, { application })); for (const entity of entities) { + for (const field of entity.fields) { + if (isFieldBinaryType(field)) { + field.fieldTypeBlobContent ??= getBlobContentType(field.fieldType); + if (application.databaseTypeCassandra || entity.databaseType === 'cassandra') { + field.fieldType = 'ByteBuffer'; + } else if (isFieldBlobType(field)) { + field.fieldType = 'byte[]' as any; + } + } + } for (const relationship of entity.relationships) { if (relationship.ownerSide === undefined) { // ownerSide backward compatibility diff --git a/generators/server/generator.ts b/generators/server/generator.ts index e45daf6ac75f..aee4f688499f 100644 --- a/generators/server/generator.ts +++ b/generators/server/generator.ts @@ -82,13 +82,13 @@ const { SUPPORTED_VALIDATION_RULES } = validations; const { isReservedTableName } = reservedKeywords; const { ANGULAR, REACT, VUE } = clientFrameworkTypes; const { GRADLE, MAVEN } = buildToolTypes; -const { CASSANDRA, SQL, NO: NO_DATABASE } = databaseTypes; +const { SQL, NO: NO_DATABASE } = databaseTypes; const { GATEWAY } = applicationTypes; const { NO: NO_SEARCH_ENGINE } = searchEngineTypes; const { CommonDBTypes, RelationalOnlyDBTypes } = fieldTypes; const { INSTANT } = CommonDBTypes; -const { BYTES, BYTE_BUFFER } = RelationalOnlyDBTypes; +const { BYTE_BUFFER } = RelationalOnlyDBTypes; const { PaginationTypes, ServiceTypes } = entityOptions; const { Validations: { MAX, MIN, MAXLENGTH, MINLENGTH, MAXBYTES, MINBYTES, PATTERN }, @@ -333,7 +333,6 @@ export default class JHipsterServerGenerator extends BaseApplicationGenerator { }, configureFields({ application, entityConfig, entityName }) { - const databaseType = entityConfig.databaseType ?? application.databaseType; // Validate entity json field content const fields = entityConfig.fields; fields!.forEach(field => { @@ -341,9 +340,6 @@ export default class JHipsterServerGenerator extends BaseApplicationGenerator { if (field.fieldType === 'DateTime' || field.fieldType === 'Date') { field.fieldType = INSTANT; } - if (field.fieldType === BYTES && databaseType === CASSANDRA) { - field.fieldType = BYTE_BUFFER; - } this._validateField(entityName, field); diff --git a/lib/application/field-types.ts b/lib/application/field-types.ts index b0101e7529e3..519f120d26ba 100644 --- a/lib/application/field-types.ts +++ b/lib/application/field-types.ts @@ -1,3 +1,4 @@ +import type { SetFieldType, SetRequired } from 'type-fest'; import type { Field } from '../types/application/field.js'; const blobFieldTypes = { @@ -34,20 +35,28 @@ export const fieldTypeValues: string[] = Object.values(fieldTypes); export type FieldType = (typeof fieldTypes)[keyof typeof fieldTypes]; -export const isBlobType = (fieldType: string): fieldType is (typeof blobFieldTypes)[keyof typeof blobFieldTypes] => { - return blobFieldTypesValues.includes(fieldType); +type FieldBlobType = (typeof blobFieldTypes)[keyof typeof blobFieldTypes]; + +type FieldBinaryType = (typeof blobFieldTypes)[keyof typeof blobFieldTypes] | 'byte[]'; + +export const isBlobType = (fieldType: string): fieldType is FieldBlobType => blobFieldTypesValues.includes(fieldType); + +export const getBlobContentType = (fieldType: FieldBlobType) => { + if (fieldType === 'AnyBlob') { + return 'any'; + } else if (fieldType === 'ImageBlob') { + return 'image'; + } else if (fieldType === 'TextBlob') { + return 'text'; + } + return undefined; }; -type EnumField = - | { - fieldType: string; - fieldIsEnum: true; - enumValues: { name: string; value: string }[]; - enumFileName: string; - } - | { - fieldIsEnum: false; - fieldType: FieldType; - }; - -export const isFieldEnum = (field: Field): field is Field & EnumField => Boolean(field.fieldValues); +export const isFieldBlobType = (field: Field): field is SetFieldType => isBlobType(field.fieldType); + +export const isFieldBinaryType = (field: Field): field is SetFieldType => + isBlobType(field.fieldType) || field.fieldType === 'byte[]'; + +export const isFieldEnumType = (field: Field): field is SetRequired => Boolean(field.fieldValues); + +export const isFieldNotEnumType = (field: Field): field is SetFieldType => !field.fieldValues;