Skip to content

Commit

Permalink
adjusts
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Sep 18, 2024
1 parent 6be13ca commit f4689f5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
18 changes: 4 additions & 14 deletions generators/base-application/support/prepare-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -283,29 +283,19 @@ 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, {
entity: entityWithConfig,
});
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!);
}
Expand Down
4 changes: 3 additions & 1 deletion generators/base-application/support/prepare-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
11 changes: 11 additions & 0 deletions generators/bootstrap-application-base/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions generators/server/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -333,17 +333,13 @@ 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 => {
// Migration from JodaTime to Java Time
if (field.fieldType === 'DateTime' || field.fieldType === 'Date') {
field.fieldType = INSTANT;
}
if (field.fieldType === BYTES && databaseType === CASSANDRA) {
field.fieldType = BYTE_BUFFER;
}

this._validateField(entityName, field);

Expand Down
39 changes: 24 additions & 15 deletions lib/application/field-types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { SetFieldType, SetRequired } from 'type-fest';
import type { Field } from '../types/application/field.js';

const blobFieldTypes = {
Expand Down Expand Up @@ -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<Field, 'fieldType', FieldBlobType> => isBlobType(field.fieldType);

export const isFieldBinaryType = (field: Field): field is SetFieldType<Field, 'fieldType', FieldBinaryType> =>
isBlobType(field.fieldType) || field.fieldType === 'byte[]';

export const isFieldEnumType = (field: Field): field is SetRequired<Field, 'enumFileName' | 'enumValues'> => Boolean(field.fieldValues);

export const isFieldNotEnumType = (field: Field): field is SetFieldType<Field, 'fieldType', FieldType> => !field.fieldValues;

0 comments on commit f4689f5

Please sign in to comment.