@@ -14,8 +14,9 @@ const path = require('path');
1414/**
1515 * Model Generator
1616 *
17- * Prompts for a Model name, Model Base Class (currently defaults to 'Entity').
18- * Creates the Model Class -- currently a one time process only.
17+ * Prompts for a Model name and model properties and creates the model class.
18+ * Currently properties can only be added once to each model using the CLI (at
19+ * creation).
1920 *
2021 * Will prompt for properties to add to the Model till a blank property name is
2122 * entered. Will also ask if a property is required, the default value for the
@@ -64,18 +65,23 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
6465 return super . checkLoopBackProject ( ) ;
6566 }
6667
68+ // Prompt a user for Model Name
6769 async promptArtifactName ( ) {
6870 await super . promptArtifactName ( ) ;
6971 this . artifactInfo . className = utils . toClassName ( this . artifactInfo . name ) ;
72+ this . log ( ) ;
7073 this . log (
7174 `Let's add a property to ${ chalk . yellow ( this . artifactInfo . className ) } ` ,
7275 ) ;
7376 }
7477
75- // Prompt for a property name
78+ // Prompt for a Property Name
7679 async promptPropertyName ( ) {
7780 this . log ( `Enter an empty property name when done` ) ;
81+ this . log ( ) ;
7882
83+ // This function can be called repeatedly so this deletes the previous
84+ // property name if one was set.
7985 delete this . propName ;
8086
8187 const prompts = [
@@ -154,15 +160,21 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
154160
155161 const answers = await this . prompt ( prompts ) ;
156162 debug ( `propertyInfo => ${ JSON . stringify ( answers ) } ` ) ;
163+
164+ // Yeoman sets the default to `''` so we remove it unless the user entered
165+ // a different value
157166 if ( answers . default === '' ) {
158167 delete answers . default ;
159168 }
160169
161170 Object . assign ( this . artifactInfo . properties [ this . propName ] , answers ) ;
171+
172+ // We prompt for `id` only once per model using idFieldSet flag.
162173 if ( answers . id ) {
163174 this . idFieldSet = true ;
164175 }
165176
177+ this . log ( ) ;
166178 this . log (
167179 `Let's add another property to ${ chalk . yellow (
168180 this . artifactInfo . className ,
@@ -223,10 +235,15 @@ module.exports = class ModelGenerator extends ArtifactGenerator {
223235 // Convert Type to include '' for template
224236 val . type = `'${ val . type } '` ;
225237
238+ // If required is false, we can delete it as that's the default assumption
239+ // for this field if not present. This helps to avoid polluting the
240+ // decorator with redundant properties.
226241 if ( ! val . required ) {
227242 delete val . required ;
228243 }
229244
245+ // We only care about marking the `id` field as `id` and not fields that
246+ // are not the id so if this is false we delete it similar to `required`.
230247 if ( ! val . id ) {
231248 delete val . id ;
232249 }
0 commit comments