generators/entity/prompts.js
function askForRelationship(done) {
    const context = this.context;
    const name = context.name;
    this.log(chalk.green('\nGenerating relationships to other entities\n'));
    const fieldNamesUnderscored = context.fieldNamesUnderscored;
    const prompts = [
        {
            type: 'confirm',
            name: 'relationshipAdd',
            message: 'Do you want to add a relationship to another entity?',
            default: true
        },
        {
            when: response => response.relationshipAdd === true,
            type: 'input',
            name: 'otherEntityName',
            validate: input => {
                if (!/^([a-zA-Z0-9_]*)$/.test(input)) {
                    return 'Your other entity name cannot contain special characters';
                }
                if (input === '') {
                    return 'Your other entity name cannot be empty';
                }
                if (jhiCore.isReservedKeyword(input, 'JAVA')) {
                    return 'Your other entity name cannot contain a Java reserved keyword';
                }
                if (input.toLowerCase() === 'user' && context.applicationType === 'microservice') {
                    return "Your entity cannot have a relationship with User because it's a gateway entity";
                }
                return true;
            },
            message: 'What is the name of the other entity?'
        },
        {
            when: response => response.relationshipAdd === true,
            type: 'input',
            name: 'relationshipName',
            validate: input => {
                if (!/^([a-zA-Z0-9_]*)$/.test(input)) {
                    return 'Your relationship cannot contain special characters';
                }
                if (input === '') {
                    return 'Your relationship cannot be empty';
                }
                if (input.charAt(0) === input.charAt(0).toUpperCase()) {
                    return 'Your relationship cannot start with an upper case letter';
                }
                if (input === 'id' || fieldNamesUnderscored.includes(_.snakeCase(input))) {
                    return 'Your relationship cannot use an already existing field name';
                }
                if (jhiCore.isReservedKeyword(input, 'JAVA')) {
                    return 'Your relationship cannot contain a Java reserved keyword';
                }
                return true;
            },
            message: 'What is the name of the relationship?',
            default: response => _.lowerFirst(response.otherEntityName)
        },
        {
            when: response => response.relationshipAdd === true,
            type: 'list',
            name: 'relationshipType',
            message: 'What is the type of the relationship?',
            choices: response => {
                const opts = [
                    {
                        value: 'many-to-one',
                        name: 'many-to-one'
                    },
                    {
                        value: 'many-to-many',
                        name: 'many-to-many'
                    },
                    {
                        value: 'one-to-one',
                        name: 'one-to-one'
                    }
                ];
                if (response.otherEntityName.toLowerCase() !== 'user') {
                    opts.unshift({
                        value: 'one-to-many',
                        name: 'one-to-many'
                    });
                }
                return opts;
            },
            default: 0
        },
        {
            when: response =>
                response.relationshipAdd === true &&
                response.otherEntityName.toLowerCase() !== 'user' &&
                (response.relationshipType === 'many-to-many' || response.relationshipType === 'one-to-one'),
            type: 'confirm',
            name: 'ownerSide',
            message: 'Is this entity the owner of the relationship?',
            default: false
        },
        {
            when: response =>
                context.databaseType === 'sql' &&
                response.relationshipAdd === true &&
                response.relationshipType === 'one-to-one' &&
                (response.ownerSide === true || response.otherEntityName.toLowerCase() === 'user'),
            type: 'confirm',
            name: 'useJPADerivedIdentifier',
            message: 'Do you want to use JPA Derived Identifier - @MapsId?',
            default: false
        },
        {
            when: response =>
                response.relationshipAdd === true &&
                (response.relationshipType === 'one-to-many' ||
                    ((response.relationshipType === 'many-to-many' || response.relationshipType === 'one-to-one') &&
                        response.otherEntityName.toLowerCase() !== 'user')),
            type: 'input',
            name: 'otherEntityRelationshipName',
            message: 'What is the name of this relationship in the other entity?',
            default: response => _.lowerFirst(name)
        },
        {
            when: response =>
                response.relationshipAdd === true &&
                (response.relationshipType === 'many-to-one' ||
                    (response.relationshipType === 'many-to-many' && response.ownerSide === true) ||
                    (response.relationshipType === 'one-to-one' && response.ownerSide === true)),
            type: 'input',
            name: 'otherEntityField',
            message: response =>
                `When you display this relationship on client-side, which field from '${
                    response.otherEntityName
                }' do you want to use? This field will be displayed as a String, so it cannot be a Blob`,
            default: 'id'
        },
        {
            when: response =>
                response.relationshipAdd === true &&
                response.otherEntityName.toLowerCase() !== context.name.toLowerCase() &&
                (response.relationshipType === 'many-to-one' ||
                    (response.relationshipType === 'many-to-many' &&
                        (response.ownerSide === true || response.otherEntityName.toLowerCase() === 'user')) ||
                    (response.relationshipType === 'one-to-one' &&
                        (response.ownerSide === true || response.otherEntityName.toLowerCase() === 'user'))),
            type: 'confirm',
            name: 'relationshipValidate',
            message: 'Do you want to add any validation rules to this relationship?',
            default: false
        },
        {
            when: response => response.relationshipValidate === true,
            type: 'checkbox',
            name: 'relationshipValidateRules',
            message: 'Which validation rules do you want to add?',
            choices: [
                {
                    name: 'Required',
                    value: 'required'
                }
            ],
            default: 0
        }
    ];
    this.prompt(prompts).then(props => {
        if (props.relationshipAdd) {
            const relationship = {
                relationshipName: props.relationshipName,
                otherEntityName: _.lowerFirst(props.otherEntityName),
                relationshipType: props.relationshipType,
                relationshipValidateRules: props.relationshipValidateRules,
                otherEntityField: props.otherEntityField,
                ownerSide: props.ownerSide,
                useJPADerivedIdentifier: props.useJPADerivedIdentifier,
                otherEntityRelationshipName: props.otherEntityRelationshipName
            };

            if (props.otherEntityName.toLowerCase() === 'user') {
                relationship.ownerSide = true;
                relationship.otherEntityField = 'login';
                relationship.otherEntityRelationshipName = _.lowerFirst(name);
            }

            fieldNamesUnderscored.push(_.snakeCase(props.relationshipName));
            context.relationships.push(relationship);
        }
        logFieldsAndRelationships.call(this);
        if (props.relationshipAdd) {
            askForRelationship.call(this, done);
        } else {
            this.log('\n');
            done();
        }
    });
}
function askForRelationship(done) {
    const context = this.context;
    const name = context.name;
    this.log(chalk.green('\nGenerating relationships to other entities\n'));
    const fieldNamesUnderscored = context.fieldNamesUnderscored;
    const prompts = [
        {
            type: 'confirm',
            name: 'relationshipAdd',
            message: 'Do you want to add a relationship to another entity?',
            default: true
        },
        {
            when: response => response.relationshipAdd === true,
            type: 'input',
            name: 'otherEntityName',
            validate: input => {
                if (!/^([a-zA-Z0-9_]*)$/.test(input)) {
                    return 'Your other entity name cannot contain special characters';
                }
                if (input === '') {
                    return 'Your other entity name cannot be empty';
                }
                if (jhiCore.isReservedKeyword(input, 'JAVA')) {
                    return 'Your other entity name cannot contain a Java reserved keyword';
                }
                if (input.toLowerCase() === 'user' && context.applicationType === 'microservice') {
                    return "Your entity cannot have a relationship with User because it's a gateway entity";
                }
                return true;
            },
            message: 'What is the name of the other entity?'
        },
        {
            when: response => response.relationshipAdd === true,
            type: 'input',
            name: 'relationshipName',
            validate: input => {
                if (!/^([a-zA-Z0-9_]*)$/.test(input)) {
                    return 'Your relationship cannot contain special characters';
                }
                if (input === '') {
                    return 'Your relationship cannot be empty';
                }
                if (input.charAt(0) === input.charAt(0).toUpperCase()) {
                    return 'Your relationship cannot start with an upper case letter';
                }
                if (input === 'id' || fieldNamesUnderscored.includes(_.snakeCase(input))) {
                    return 'Your relationship cannot use an already existing field name';
                }
                if (jhiCore.isReservedKeyword(input, 'JAVA')) {
                    return 'Your relationship cannot contain a Java reserved keyword';
                }
                return true;
            },
            message: 'What is the name of the relationship?',
            default: response => _.lowerFirst(response.otherEntityName)
        },
        {
            when: response => response.relationshipAdd === true,
            type: 'list',
            name: 'relationshipType',
            message: 'What is the type of the relationship?',
            choices: response => {
                const opts = [
                    {
                        value: 'many-to-one',
                        name: 'many-to-one'
                    },
                    {
                        value: 'many-to-many',
                        name: 'many-to-many'
                    },
                    {
                        value: 'one-to-one',
                        name: 'one-to-one'
                    }
                ];
                if (response.otherEntityName.toLowerCase() !== 'user') {
                    opts.unshift({
                        value: 'one-to-many',
                        name: 'one-to-many'
                    });
                }
                return opts;
            },
            default: 0
        },
        {
            when: response =>
                response.relationshipAdd === true &&
                response.otherEntityName.toLowerCase() !== 'user' &&
                (response.relationshipType === 'many-to-many' || response.relationshipType === 'one-to-one'),
            type: 'confirm',
            name: 'ownerSide',
            message: 'Is this entity the owner of the relationship?',
            default: false
        },
        {
            when: response =>
                context.databaseType === 'sql' &&
                response.relationshipAdd === true &&
                response.relationshipType === 'one-to-one' &&
                (response.ownerSide === true || response.otherEntityName.toLowerCase() === 'user'),
            type: 'confirm',
            name: 'useJPADerivedIdentifier',
            message: 'Do you want to use JPA Derived Identifier - @MapsId?',
            default: false
        },
        {
            when: response =>
                response.relationshipAdd === true &&
                (response.relationshipType === 'one-to-many' ||
                    ((response.relationshipType === 'many-to-many' || response.relationshipType === 'one-to-one') &&
                        response.otherEntityName.toLowerCase() !== 'user')),
            type: 'input',
            name: 'otherEntityRelationshipName',
            message: 'What is the name of this relationship in the other entity?',
            default: response => _.lowerFirst(name)
        },
        {
            when: response =>
                response.relationshipAdd === true &&
                (response.relationshipType === 'many-to-one' ||
                    (response.relationshipType === 'many-to-many' && response.ownerSide === true) ||
                    (response.relationshipType === 'one-to-one' && response.ownerSide === true)),
            type: 'input',
            name: 'otherEntityField',
            message: response =>
                `When you display this relationship on client-side, which field from '${
                    response.otherEntityName
                }' do you want to use? This field will be displayed as a String, so it cannot be a Blob`,
            default: 'id'
        },
        {
            when: response =>
                response.relationshipAdd === true &&
                response.otherEntityName.toLowerCase() !== context.name.toLowerCase() &&
                (response.relationshipType === 'many-to-one' ||
                    (response.relationshipType === 'many-to-many' &&
                        (response.ownerSide === true || response.otherEntityName.toLowerCase() === 'user')) ||
                    (response.relationshipType === 'one-to-one' &&
                        (response.ownerSide === true || response.otherEntityName.toLowerCase() === 'user'))),
            type: 'confirm',
            name: 'relationshipValidate',
            message: 'Do you want to add any validation rules to this relationship?',
            default: false
        },
        {
            when: response => response.relationshipValidate === true,
            type: 'checkbox',
            name: 'relationshipValidateRules',
            message: 'Which validation rules do you want to add?',
            choices: [
                {
                    name: 'Required',
                    value: 'required'
                }
            ],
            default: 0
        }
    ];
    this.prompt(prompts).then(props => {
        if (props.relationshipAdd) {
            const relationship = {
                relationshipName: props.relationshipName,
                otherEntityName: _.lowerFirst(props.otherEntityName),
                relationshipType: props.relationshipType,
                relationshipValidateRules: props.relationshipValidateRules,
                otherEntityField: props.otherEntityField,
                ownerSide: props.ownerSide,
                useJPADerivedIdentifier: props.useJPADerivedIdentifier,
                otherEntityRelationshipName: props.otherEntityRelationshipName
            };

            if (props.otherEntityName.toLowerCase() === 'user') {
                relationship.ownerSide = true;
                relationship.otherEntityField = 'login';
                relationship.otherEntityRelationshipName = _.lowerFirst(name);
            }

            fieldNamesUnderscored.push(_.snakeCase(props.relationshipName));
            context.relationships.push(relationship);
        }
        logFieldsAndRelationships.call(this);
        if (props.relationshipAdd) {
            askForRelationship.call(this, done);
        } else {
            this.log('\n');
            done();
        }
    });
}
function askForRelationship ( done ) { const context = this . context ; const name = context . name ; this . log ( chalk . green ( '\nGenerating relationships to other entities\n' ) ) ; const fieldNamesUnderscored = context . fieldNamesUnderscored ; const prompts = [ { type : 'confirm' , name : 'relationshipAdd' , message : 'Do you want to add a relationship to another entity?' , default : true } , { when : response => response . relationshipAdd === true , type : 'input' , name : 'otherEntityName' , validate : input => { if ( ! / ^([a-zA-Z0-9_]*)$ / . test ( input ) ) { return 'Your other entity name cannot contain special characters' ; } if ( input === '' ) { return 'Your other entity name cannot be empty' ; } if ( jhiCore . isReservedKeyword ( input , 'JAVA' ) ) { return 'Your other entity name cannot contain a Java reserved keyword' ; } if ( input . toLowerCase ( ) === 'user' && context . applicationType === 'microservice' ) { return "Your entity cannot have a relationship with User because it's a gateway entity" ; } return true ; } , message : 'What is the name of the other entity?' } , { when : response => response . relationshipAdd === true , type : 'input' , name : 'relationshipName' , validate : input => { if ( ! / ^([a-zA-Z0-9_]*)$ / . test ( input ) ) { return 'Your relationship cannot contain special characters' ; } if ( input === '' ) { return 'Your relationship cannot be empty' ; } if ( input . charAt ( 0 ) === input . charAt ( 0 ) . toUpperCase ( ) ) { return 'Your relationship cannot start with an upper case letter' ; } if ( input === 'id' || fieldNamesUnderscored . includes ( _ . snakeCase ( input ) ) ) { return 'Your relationship cannot use an already existing field name' ; } if ( jhiCore . isReservedKeyword ( input , 'JAVA' ) ) { return 'Your relationship cannot contain a Java reserved keyword' ; } return true ; } , message : 'What is the name of the relationship?' , default : response => _ . lowerFirst ( response . otherEntityName ) } , { when : response => response . relationshipAdd === true , type : 'list' , name : 'relationshipType' , message : 'What is the type of the relationship?' , choices : response => { const opts = [ { value : 'many-to-one' , name : 'many-to-one' } , { value : 'many-to-many' , name : 'many-to-many' } , { value : 'one-to-one' , name : 'one-to-one' } ] ; if ( response . otherEntityName . toLowerCase ( ) !== 'user' ) { opts . unshift ( { value : 'one-to-many' , name : 'one-to-many' } ) ; } return opts ; } , default : 0 } , { when : response => response . relationshipAdd === true && response . otherEntityName . toLowerCase ( ) !== 'user' && ( response . relationshipType === 'many-to-many' || response . relationshipType === 'one-to-one' ) , type : 'confirm' , name : 'ownerSide' , message : 'Is this entity the owner of the relationship?' , default : false } , { when : response => context . databaseType === 'sql' && response . relationshipAdd === true && response . relationshipType === 'one-to-one' && ( response . ownerSide === true || response . otherEntityName . toLowerCase ( ) === 'user' ) , type : 'confirm' , name : 'useJPADerivedIdentifier' , message : 'Do you want to use JPA Derived Identifier - @MapsId?' , default : false } , { when : response => response . relationshipAdd === true && ( response . relationshipType === 'one-to-many' || ( ( response . relationshipType === 'many-to-many' || response . relationshipType === 'one-to-one' ) && response . otherEntityName . toLowerCase ( ) !== 'user' ) ) , type : 'input' , name : 'otherEntityRelationshipName' , message : 'What is the name of this relationship in the other entity?' , default : response => _ . lowerFirst ( name ) } , { when : response => response . relationshipAdd === true && ( response . relationshipType === 'many-to-one' || ( response . relationshipType === 'many-to-many' && response . ownerSide === true ) || ( response . relationshipType === 'one-to-one' && response . ownerSide === true ) ) , type : 'input' , name : 'otherEntityField' , message : response => ` ${ response . otherEntityName } ` , default : 'id' } , { when : response => response . relationshipAdd === true && response . otherEntityName . toLowerCase ( ) !== context . name . toLowerCase ( ) && ( response . relationshipType === 'many-to-one' || ( response . relationshipType === 'many-to-many' && ( response . ownerSide === true || response . otherEntityName . toLowerCase ( ) === 'user' ) ) || ( response . relationshipType === 'one-to-one' && ( response . ownerSide === true || response . otherEntityName . toLowerCase ( ) === 'user' ) ) ) , type : 'confirm' , name : 'relationshipValidate' , message : 'Do you want to add any validation rules to this relationship?' , default : false } , { when : response => response . relationshipValidate === true , type : 'checkbox' , name : 'relationshipValidateRules' , message : 'Which validation rules do you want to add?' , choices : [ { name : 'Required' , value : 'required' } ] , default : 0 } ] ; this . prompt ( prompts ) . then ( props => { if ( props . relationshipAdd ) { const relationship = { relationshipName : props . relationshipName , otherEntityName : _ . lowerFirst ( props . otherEntityName ) , relationshipType : props . relationshipType , relationshipValidateRules : props . relationshipValidateRules , otherEntityField : props . otherEntityField , ownerSide : props . ownerSide , useJPADerivedIdentifier : props . useJPADerivedIdentifier , otherEntityRelationshipName : props . otherEntityRelationshipName } ; if ( props . otherEntityName . toLowerCase ( ) === 'user' ) { relationship . ownerSide = true ; relationship . otherEntityField = 'login' ; relationship . otherEntityRelationshipName = _ . lowerFirst ( name ) ; } fieldNamesUnderscored . push ( _ . snakeCase ( props . relationshipName ) ) ; context . relationships . push ( relationship ) ; } logFieldsAndRelationships . call ( this ) ; if ( props . relationshipAdd ) { askForRelationship . call ( this , done ) ; } else { this . log ( '\n' ) ; done ( ) ; } } ) ; }
ask question for a relationship creation
ask question for a relationship creation