diff --git a/docs/FAQ.md b/docs/FAQ.md index 65e189fa..3fc1ac96 100644 --- a/docs/FAQ.md +++ b/docs/FAQ.md @@ -8,7 +8,7 @@ $ sequelize init ## How can I generate a model? Specify model name with `--name` argument. List of table fields can be passed with `--attributes` option ``` -$ sequelize model:create --name User --attributes name:string,state:boolean,birth:date,card:integer +$ sequelize model:create --name User --attributes name:string,state:boolean,birth:date,card:integer,role:enum:{Admin,Guest} ``` ## How can I create a migration? diff --git a/src/assets/migrations/create-table.js b/src/assets/migrations/create-table.js index cd4f4815..1fd3e92d 100644 --- a/src/assets/migrations/create-table.js +++ b/src/assets/migrations/create-table.js @@ -13,7 +13,7 @@ module.exports = { <% attributes.forEach(function(attribute) { %> <%= attribute.fieldName %>: { - type: Sequelize.<%= attribute.dataFunction ? `${attribute.dataFunction.toUpperCase()}(Sequelize.${attribute.dataType.toUpperCase()})` : attribute.dataType.toUpperCase() %> + type: Sequelize.<%= attribute.dataFunction ? `${attribute.dataFunction.toUpperCase()}(Sequelize.${attribute.dataType.toUpperCase()})` : attribute.dataValues ? `${attribute.dataType.toUpperCase()}(${attribute.dataValues})` : attribute.dataType.toUpperCase() %> }, <% }) %> diff --git a/src/assets/models/model.js b/src/assets/models/model.js index 5491925c..70007aa5 100644 --- a/src/assets/models/model.js +++ b/src/assets/models/model.js @@ -3,7 +3,7 @@ module.exports = (sequelize, DataTypes) => { const <%= name %> = sequelize.define('<%= name %>', { <% attributes.forEach(function(attribute, index) { %> - <%= attribute.fieldName %>: DataTypes.<%= attribute.dataFunction ? `${attribute.dataFunction.toUpperCase()}(DataTypes.${attribute.dataType.toUpperCase()})` : attribute.dataType.toUpperCase() %> + <%= attribute.fieldName %>: DataTypes.<%= attribute.dataFunction ? `${attribute.dataFunction.toUpperCase()}(DataTypes.${attribute.dataType.toUpperCase()})` : attribute.dataValues ? `${attribute.dataType.toUpperCase()}(${attribute.dataValues})` : attribute.dataType.toUpperCase() %> <%= (Object.keys(attributes).length - 1) > index ? ',' : '' %> <% }) %> }, { diff --git a/src/helpers/model-helper.js b/src/helpers/model-helper.js index f1a158e6..7fd5756f 100644 --- a/src/helpers/model-helper.js +++ b/src/helpers/model-helper.js @@ -1,7 +1,7 @@ import helpers from './index'; const Sequelize = helpers.generic.getSequelize(); -const validAttributeFunctionType = 'array'; +const validAttributeFunctionType = ['array', 'enum']; /** * Check the given dataType actual exists. @@ -20,15 +20,22 @@ function formatAttributes (attribute) { const split = attribute.split(':'); if (split.length === 2) { - result = { fieldName: split[0], dataType: split[1], dataFunction: null }; + result = { fieldName: split[0], dataType: split[1], dataFunction: null, dataValues: null }; } else if (split.length === 3) { - const isValidFunction = validAttributeFunctionType === split[1].toLowerCase(); - const isValidValue = validAttributeFunctionType !== split[2].toLowerCase(); + const validValues = /^\{(,? ?[A-z0-9 ]+)+\}$/; + const isValidFunction = validAttributeFunctionType.indexOf(split[1].toLowerCase()) !== -1; + const isValidValue = validAttributeFunctionType.indexOf(split[2].toLowerCase()) === -1 && split[2].match(validValues) === null; + const isValidValues = split[2].match(validValues) !== null; - if (isValidFunction && isValidValue) { - result = { fieldName: split[0], dataType: split[2], dataFunction: split[1] }; + if (isValidFunction && isValidValue && !isValidValues) { + result = { fieldName: split[0], dataType: split[2], dataFunction: split[1], dataValues: null }; + } + + if (isValidFunction && !isValidValue && isValidValues) { + result = { fieldName: split[0], dataType: split[1], dataFunction: null, dataValues: split[2].replace(/(^\{|\}$)/g, '').split(/\s*,\s*/).map(s => `'${s}'`).join(', ') }; } } + return result; } @@ -36,12 +43,26 @@ module.exports = { transformAttributes (flag) { /* possible flag formats: - - first_name:string,last_name:string,bio:text,reviews:array:string - - 'first_name:string last_name:string bio:text reviews:array:string' - - 'first_name:string, last_name:string, bio:text, reviews:array:string' + - first_name:string,last_name:string,bio:text,role:enum:{Admin, 'Guest User'},reviews:array:string + - 'first_name:string last_name:string bio:text role:enum:{Admin, Guest User} reviews:array:string' + - 'first_name:string, last_name:string, bio:text, role:enum:{Admin, Guest User} reviews:array:string' */ + const attributeStrings = flag.split('').map((() => { + let openValues = false; + return a => { + if ((a === ',' || a === ' ') && !openValues) { + return ' '; + } + if (a === '{') { + openValues = true; + } + if (a === '}') { + openValues = false; + } - const attributeStrings = flag.replace(/,/g, ' ').split(/\s+/); + return a; + }; + })()).join('').split(/\s{2,}/); return attributeStrings.map(attribute => { const formattedAttribute = formatAttributes(attribute); @@ -57,6 +78,7 @@ module.exports = { }, generateFileContent (args) { + return helpers.template.render('models/model.js', { name: args.name, attributes: this.transformAttributes(args.attributes), diff --git a/test/model/create.test.js b/test/model/create.test.js index ef4b0e4e..ca44c795 100644 --- a/test/model/create.test.js +++ b/test/model/create.test.js @@ -85,9 +85,10 @@ const _ = require('lodash'); }) ;[ - 'first_name:string,last_name:string,bio:text,reviews:array:text', - '\'first_name:string last_name:string bio:text reviews:array:text\'', - '\'first_name:string, last_name:string, bio:text, reviews:array:text\'' + 'first_name:string,last_name:string,bio:text,role:enum:{Admin,"Guest User"},reviews:array:text', + 'first_name:string,last_name:string,bio:text,role:enum:{Admin,\'Guest User\'},reviews:array:text', + '\'first_name:string last_name:string bio:text role:enum:{Admin,Guest User} reviews:array:text\'', + '\'first_name:string, last_name:string, bio:text, role:enum:{Admin, Guest User}, reviews:array:text\'' ].forEach(attributes => { describe('--attributes ' + attributes, () => { it('exits with exit code 0', done => { @@ -120,6 +121,7 @@ const _ = require('lodash'); .pipe(helpers.ensureContent('first_name: DataTypes.STRING')) .pipe(helpers.ensureContent('last_name: DataTypes.STRING')) .pipe(helpers.ensureContent('bio: DataTypes.TEXT')) + .pipe(helpers.ensureContent('role: DataTypes.ENUM(\'Admin\', \'Guest User\')')) .pipe(helpers.ensureContent('reviews: DataTypes.ARRAY(DataTypes.TEXT)')) .pipe(helpers.teardown(done)); }); @@ -169,6 +171,9 @@ const _ = require('lodash'); .pipe(helpers.ensureContent( 'bio: {\n type: Sequelize.TEXT\n },' )) + .pipe(helpers.ensureContent( + 'role: {\n type: Sequelize.ENUM(\'Admin\', \'Guest User\')\n },' + )) .pipe(helpers.ensureContent( 'reviews: {\n type: Sequelize.ARRAY(Sequelize.TEXT)\n },' ))