Skip to content
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
8 changes: 7 additions & 1 deletion src/commands/model_generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ exports.handler = function (args) {
ensureMigrationsFolder();
checkModelFileExistence(args);

helpers.model.generateFile(args);

try {
helpers.model.generateFile(args);
} catch (err) {
helpers.view.error(err.message);
}

helpers.migration.generateTableCreationFile(args);
helpers.view.log(
'New model was created at',
Expand Down
28 changes: 21 additions & 7 deletions src/helpers/model-helper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import helpers from './index';
import { DataTypes } from 'sequelize';

const validAttributeFunctionType = 'array';

/**
* Check the given dataType actual exists.
* @param {string} dataType
*/
function validateDataType (dataType) {
if (!DataTypes[dataType.toUpperCase()]) {
throw new Error(`Unknown type '${dataType}'`);
}

return dataType;
}

function formatAttributes (attribute) {
let result;
const split = attribute.split(':');
Expand All @@ -28,18 +41,19 @@ module.exports = {
- 'first_name:string, last_name:string, bio:text, reviews:array:string'
*/

const set = flag.replace(/,/g, ' ').split(/\s+/);
const result = [];
const attributeStrings = flag.replace(/,/g, ' ').split(/\s+/);

set.forEach(attribute => {
return attributeStrings.map(attribute => {
const formattedAttribute = formatAttributes(attribute);

if (formattedAttribute) {
result.push(formattedAttribute);
try {
validateDataType(formattedAttribute.dataType);
} catch (err) {
throw new Error(`Attribute '${attribute}' cannot be parsed: ${err.message}`);
}
});

return result;
return formattedAttribute;
});
},

generateFileContent (args) {
Expand Down
9 changes: 9 additions & 0 deletions test/model/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ const _ = require('lodash');
});
});

describe('when passed an invalid data type', () => {
it('exits with an error code', done => {
prepare({
flags: { name: 'User', attributes: 'badAttribute:datetime' },
cli: { exitCode: 1 }
}, done);
});
})

;[
'first_name:string,last_name:string,bio:text,reviews:array:text',
'\'first_name:string last_name:string bio:text reviews:array:text\'',
Expand Down
2 changes: 1 addition & 1 deletion test/support/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module.exports = {
expect(err.code).to.equal(1);
callback(null, result);
} catch (e) {
callback(e, result);
callback(new Error('Expected cli to exit with a non-zero code'), null);
}
} else {
err = options.pipeStderr ? null : err;
Expand Down