model-helper.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. var _index = _interopRequireDefault(require("./index"));
  3. function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
  4. const Sequelize = _index.default.generic.getSequelize();
  5. const validAttributeFunctionType = ['array', 'enum'];
  6. /**
  7. * Check the given dataType actual exists.
  8. * @param {string} dataType
  9. */
  10. function validateDataType(dataType) {
  11. if (!Sequelize.DataTypes[dataType.toUpperCase()]) {
  12. throw new Error(`Unknown type '${dataType}'`);
  13. }
  14. return dataType;
  15. }
  16. function formatAttributes(attribute) {
  17. let result;
  18. const split = attribute.split(':');
  19. if (split.length === 2) {
  20. result = {
  21. fieldName: split[0],
  22. dataType: split[1],
  23. dataFunction: null,
  24. dataValues: null
  25. };
  26. } else if (split.length === 3) {
  27. const validValues = /^\{(,? ?[A-z0-9 ]+)+\}$/;
  28. const isValidFunction = validAttributeFunctionType.indexOf(split[1].toLowerCase()) !== -1;
  29. const isValidValue = validAttributeFunctionType.indexOf(split[2].toLowerCase()) === -1 && split[2].match(validValues) === null;
  30. const isValidValues = split[2].match(validValues) !== null;
  31. if (isValidFunction && isValidValue && !isValidValues) {
  32. result = {
  33. fieldName: split[0],
  34. dataType: split[2],
  35. dataFunction: split[1],
  36. dataValues: null
  37. };
  38. }
  39. if (isValidFunction && !isValidValue && isValidValues) {
  40. result = {
  41. fieldName: split[0],
  42. dataType: split[1],
  43. dataFunction: null,
  44. dataValues: split[2].replace(/(^\{|\}$)/g, '').split(/\s*,\s*/).map(s => `'${s}'`).join(', ')
  45. };
  46. }
  47. }
  48. return result;
  49. }
  50. module.exports = {
  51. transformAttributes(flag) {
  52. /*
  53. possible flag formats:
  54. - first_name:string,last_name:string,bio:text,role:enum:{Admin, 'Guest User'},reviews:array:string
  55. - 'first_name:string last_name:string bio:text role:enum:{Admin, Guest User} reviews:array:string'
  56. - 'first_name:string, last_name:string, bio:text, role:enum:{Admin, Guest User} reviews:array:string'
  57. */
  58. const attributeStrings = flag.split('').map((() => {
  59. let openValues = false;
  60. return a => {
  61. if ((a === ',' || a === ' ') && !openValues) {
  62. return ' ';
  63. }
  64. if (a === '{') {
  65. openValues = true;
  66. }
  67. if (a === '}') {
  68. openValues = false;
  69. }
  70. return a;
  71. };
  72. })()).join('').split(/\s{2,}/);
  73. return attributeStrings.map(attribute => {
  74. const formattedAttribute = formatAttributes(attribute);
  75. try {
  76. validateDataType(formattedAttribute.dataType);
  77. } catch (err) {
  78. throw new Error(`Attribute '${attribute}' cannot be parsed: ${err.message}`);
  79. }
  80. return formattedAttribute;
  81. });
  82. },
  83. generateFileContent(args) {
  84. return _index.default.template.render('models/model.js', {
  85. name: args.name,
  86. attributes: this.transformAttributes(args.attributes),
  87. underscored: args.underscored
  88. });
  89. },
  90. generateFile(args) {
  91. const modelPath = _index.default.path.getModelPath(args.name);
  92. _index.default.asset.write(modelPath, this.generateFileContent(args));
  93. },
  94. modelFileExists(filePath) {
  95. return _index.default.path.existsSync(filePath);
  96. }
  97. };