-
-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathmodel-helper.js
More file actions
executable file
·128 lines (111 loc) · 3.27 KB
/
model-helper.js
File metadata and controls
executable file
·128 lines (111 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import helpers from './index';
const Sequelize = helpers.generic.getSequelize();
const validAttributeFunctionType = ['array', 'enum'];
/**
* Check the given dataType actual exists.
* @param {string} dataType
*/
function validateDataType(dataType) {
if (!Sequelize.DataTypes[dataType.toUpperCase()]) {
throw new Error(`Unknown type '${dataType}'`);
}
return dataType;
}
function formatAttributes(attribute) {
let result;
const split = attribute.split(':');
if (split.length === 2) {
result = {
fieldName: split[0],
dataType: split[1],
dataFunction: null,
dataValues: null,
};
} else if (split.length === 3) {
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 && !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,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;
}
return a;
};
})()
)
.join('')
.split(/\s{2,}/);
return attributeStrings.map((attribute) => {
const formattedAttribute = formatAttributes(attribute);
try {
validateDataType(formattedAttribute.dataType);
} catch (err) {
throw new Error(
`Attribute '${attribute}' cannot be parsed: ${err.message}`
);
}
return formattedAttribute;
});
},
generateFileContent(args) {
return helpers.template.render('models/model.js', {
name: args.name,
attributes: this.transformAttributes(args.attributes),
underscored: args.underscored,
paranoid: args.paranoid,
});
},
generateFile(args) {
const modelPath = helpers.path.getModelPath(args.name);
helpers.asset.write(modelPath, this.generateFileContent(args));
},
modelFileExists(filePath) {
return helpers.path.existsSync(filePath);
},
};