diff --git a/packages/cli/generators/model/index.js b/packages/cli/generators/model/index.js index db2f5e1a291a..6619910119e1 100644 --- a/packages/cli/generators/model/index.js +++ b/packages/cli/generators/model/index.js @@ -14,6 +14,8 @@ const utils = require('../../lib/utils'); const chalk = require('chalk'); const path = require('path'); +const {createPropertyTemplateData} = require('./property-definition'); + const PROMPT_BASE_MODEL_CLASS = 'Please select the model base class'; const ERROR_NO_MODELS_FOUND = 'Model was not found in'; @@ -476,56 +478,11 @@ module.exports = class ModelGenerator extends ArtifactGenerator { this.artifactInfo.modelBaseClass, ); - // Set up types for Templating - const TS_TYPES = ['string', 'number', 'object', 'boolean', 'any']; - const NON_TS_TYPES = ['geopoint', 'date']; - Object.values(this.artifactInfo.properties).forEach(val => { - // Default tsType is the type property - val.tsType = val.type; - - // Override tsType based on certain type values - if (val.type === 'array') { - if (TS_TYPES.includes(val.itemType)) { - val.tsType = `${val.itemType}[]`; - } else if (val.type === 'buffer') { - val.tsType = 'Buffer[]'; - } else { - val.tsType = 'string[]'; - } - } else if (val.type === 'buffer') { - val.tsType = 'Buffer'; - } - - if (NON_TS_TYPES.includes(val.tsType)) { - val.tsType = 'string'; - } - - if ( - val.defaultValue && - NON_TS_TYPES.concat(['string', 'any']).includes(val.type) - ) { - val.defaultValue = `'${val.defaultValue}'`; - } - - // Convert Type to include '' for template - val.type = `'${val.type}'`; - if (val.itemType) { - val.itemType = `'${val.itemType}'`; - } - - // If required is false, we can delete it as that's the default assumption - // for this field if not present. This helps to avoid polluting the - // decorator with redundant properties. - if (!val.required) { - delete val.required; - } - - // We only care about marking the `id` field as `id` and not fields that - // are not the id so if this is false we delete it similar to `required`. - if (!val.id) { - delete val.id; - } - }); + const propDefs = this.artifactInfo.properties; + this.artifactInfo.properties = {}; + for (const key in propDefs) { + this.artifactInfo.properties = createPropertyTemplateData(propDefs[key]); + } if (this.artifactInfo.modelSettings) { this.artifactInfo.modelSettings = utils.stringifyModelSettings( diff --git a/packages/cli/generators/model/property-definition.js b/packages/cli/generators/model/property-definition.js new file mode 100644 index 000000000000..971caee28100 --- /dev/null +++ b/packages/cli/generators/model/property-definition.js @@ -0,0 +1,73 @@ +// Copyright IBM Corp. 2018,2019. All Rights Reserved. +// Node module: @loopback/cli +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +'use strict'; + +const TS_TYPES = ['string', 'number', 'object', 'boolean', 'any']; +const NON_TS_TYPES = ['geopoint', 'date']; +const BUILTIN_TYPES = [...TS_TYPES, ...NON_TS_TYPES]; + +module.exports = { + createPropertyTemplateData, + BUILTIN_TYPES, +}; + +/** + * Convert property definition in LB4 style to data needed by model template + * @param {object} val The property definition + * @returns {object} Data for model-property template + */ +function createPropertyTemplateData(val) { + // shallow clone the object - don't modify original data! + val = {...val}; + + // Default tsType is the type property + val.tsType = val.type; + + // Override tsType based on certain type values + if (val.type === 'array') { + if (TS_TYPES.includes(val.itemType)) { + val.tsType = `${val.itemType}[]`; + } else if (val.type === 'buffer') { + val.tsType = 'Buffer[]'; + } else { + val.tsType = 'string[]'; + } + } else if (val.type === 'buffer') { + val.tsType = 'Buffer'; + } + + if (NON_TS_TYPES.includes(val.tsType)) { + val.tsType = 'string'; + } + + if ( + val.defaultValue && + NON_TS_TYPES.concat(['string', 'any']).includes(val.type) + ) { + val.defaultValue = `'${val.defaultValue}'`; + } + + // Convert Type to include '' for template + val.type = `'${val.type}'`; + if (val.itemType) { + val.itemType = `'${val.itemType}'`; + } + + // If required is false, we can delete it as that's the default assumption + // for this field if not present. This helps to avoid polluting the + // decorator with redundant properties. + if (!val.required) { + delete val.required; + } + + // We only care about marking the `id` field as `id` and not fields that + // are not the id so if this is false we delete it similar to `required`. + if (!val.id) { + delete val.id; + } + + return val; +}