generator-begcode
Version:
Spring Boot + Angular/React/Vue in one handy generator
38 lines (37 loc) • 1.42 kB
JavaScript
import { fieldTypes, databaseTypes } from '../../../../jdl/jhipster/index.js';
const dbTypes = fieldTypes;
const { STRING, UUID, LONG, INTEGER } = dbTypes.CommonDBTypes;
const { SQL } = databaseTypes;
export const getJavaValueGeneratorForType = (type) => {
if (type === STRING) {
return 'UUID.randomUUID().toString()';
}
if (type === UUID) {
return 'UUID.randomUUID()';
}
if (type === INTEGER) {
return 'intCount.incrementAndGet()';
}
if (type === LONG) {
return 'longCount.incrementAndGet()';
}
throw new Error(`Java type ${type} does not have a random generator implemented`);
};
export const getPrimaryKeyValue = (primaryKey, databaseType, defaultValue = 1) => {
if (typeof primaryKey === 'object' && primaryKey.composite) {
return `new ${primaryKey.type}(${primaryKey.references
.map(ref => getPrimaryKeyValue(ref.type, databaseType, defaultValue))
.join(', ')})`;
}
const primaryKeyType = typeof primaryKey === 'string' ? primaryKey : primaryKey.type;
if (primaryKeyType === STRING) {
if (databaseType === SQL && defaultValue === 0) {
return getJavaValueGeneratorForType(primaryKeyType);
}
return `"id${defaultValue}"`;
}
if (primaryKeyType === UUID) {
return getJavaValueGeneratorForType(primaryKeyType);
}
return `${defaultValue}L`;
};