Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mode:generate) add enum support #704

Merged
merged 2 commits into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion src/assets/migrations/create-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() %>
},
<% }) %>

Expand Down
2 changes: 1 addition & 1 deletion src/assets/models/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? ',' : '' %>
<% }) %>
}, {
Expand Down
42 changes: 32 additions & 10 deletions src/helpers/model-helper.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,28 +20,49 @@ 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;
}

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);
Expand All @@ -57,6 +78,7 @@ module.exports = {
},

generateFileContent (args) {

return helpers.template.render('models/model.js', {
name: args.name,
attributes: this.transformAttributes(args.attributes),
Expand Down
11 changes: 8 additions & 3 deletions test/model/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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));
});
Expand Down Expand Up @@ -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 },'
))
Expand Down