-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
221 lines (192 loc) · 6.26 KB
/
index.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* @namespace
* @property {object} Sequelize
* @property {function} Sequelize.BLOB
* @property {function} Sequelize.ENUM
* @property {function} Sequelize.STRING
* @property {function} Sequelize.STRING.BINARY
* @property {function} Sequelize.DATE
* @property {function} Sequelize.ARRAY
* @property {function} Sequelize.BOOLEAN
* @property {function} Sequelize.DOUBLE
* @property {function} Sequelize.FLOAT
* @property {function} Sequelize.INTEGER
* @property {function} Sequelize.BIGINT
* @property {function} Sequelize.UUID
* @property {function} Sequelize.JSONB
*/
var Sequelize = require("sequelize");
var dialect = "mysql";
/**
* @param {string} newDialect
* @returns {*}
*/
function setDialect(newDialect) {
if (
["mysql", "mariadb", "sqlite", "postgres", "mssql"].indexOf(newDialect) ===
-1
) {
throw new Error("Unknown sequalize dialect");
}
dialect = newDialect;
}
/**
*
* @param {Object|string} swaggerPropertySchema
* @param {Object} swaggerPropertySchema.properties
* @param {Object} swaggerPropertySchema.$ref
* @param {Array} swaggerPropertySchema.enum
* @param {string} swaggerPropertySchema.type
* @param {string} swaggerPropertySchema.format
* @param {Object|string} swaggerPropertySchema.items
* @returns {*}
*/
function getSequalizeType(swaggerPropertySchema) {
if (typeof swaggerPropertySchema === "string") {
swaggerPropertySchema = {
type: swaggerPropertySchema
};
}
if (swaggerPropertySchema.properties) {
console.log(
"Warning: encountered",
JSON.stringify(swaggerPropertySchema.properties)
);
console.log(
"Cannot handle complex subschemas (yet?), falling back to blob"
);
return Sequelize.BLOB;
}
if (swaggerPropertySchema.$ref) {
console.log(
"Warning: encountered",
JSON.stringify(swaggerPropertySchema.$ref)
);
console.log("Cannot handle $ref (yet?), falling back to blob");
return Sequelize.BLOB;
}
if (swaggerPropertySchema.enum) {
return Sequelize.ENUM.apply(null, swaggerPropertySchema.enum);
}
// as seen http://swagger.io/specification/#dataTypeType
switch (swaggerPropertySchema.type) {
case "string":
switch (swaggerPropertySchema.format || "") {
case "byte":
case "binary":
if (swaggerPropertySchema.maxLength > 5592415) {
return Sequelize.BLOB("long");
}
if (swaggerPropertySchema.maxLength > 21845) {
return Sequelize.BLOB("medium");
}
// NOTE: VARCHAR(255) may container 255 multibyte chars: it's _NOT_ byte delimited
if (swaggerPropertySchema.maxLength > 255) {
return Sequelize.BLOB();
}
return Sequelize.STRING.BINARY;
case "date":
return Sequelize.DATEONLY;
case "date-time":
//return Sequelize.DATETIME; //not working?
return Sequelize.DATE;
case "uuid":
return Sequelize.UUID;
case "jsonb":
return Sequelize.JSONB;
default:
if (swaggerPropertySchema.maxLength) {
// http://stackoverflow.com/questions/13932750/tinytext-text-mediumtext-and-longtext-maximum-sto
// http://stackoverflow.com/questions/7755629/varchar255-vs-tinytext-tinyblob-and-varchar65535-v
// NOTE: text may be in multibyte format!
if (swaggerPropertySchema.maxLength > 5592415) {
return Sequelize.TEXT("long");
}
if (swaggerPropertySchema.maxLength > 21845) {
return Sequelize.TEXT("medium");
}
// NOTE: VARCHAR(255) may container 255 multibyte chars: it's _NOT_ byte delimited
if (swaggerPropertySchema.maxLength > 255) {
return Sequelize.TEXT();
}
}
return Sequelize.STRING; // === VARCHAR
}
case "array":
if (dialect === "postgres") {
return Sequelize.ARRAY(getSequalizeType(swaggerPropertySchema.items));
}
console.log(
"Warning: encountered",
JSON.stringify(swaggerPropertySchema)
);
console.log(
"Can only handle array for postgres (yet?), see http://docs.sequelizejs.com/en/latest/api/datatypes/#array, falling back to blob"
);
return Sequelize.BLOB;
case "boolean":
return Sequelize.BOOLEAN;
case "integer":
switch (swaggerPropertySchema.format || "") {
case "int32":
if (
typeof swaggerPropertySchema.minimum === "number" &&
swaggerPropertySchema.minimum >= 0
) {
return Sequelize.INTEGER.UNSIGNED;
}
return Sequelize.INTEGER;
default:
if (
typeof swaggerPropertySchema.minimum === "number" &&
swaggerPropertySchema.minimum >= 0
) {
return Sequelize.BIGINT.UNSIGNED;
}
return Sequelize.BIGINT;
}
case "number":
switch (swaggerPropertySchema.format || "") {
case "float":
return Sequelize.FLOAT;
default:
return Sequelize.DOUBLE;
}
default:
console.log(
"Warning: encountered",
JSON.stringify(swaggerPropertySchema)
);
console.log("Unknown data type, falling back to blob");
return Sequelize.BLOB;
}
}
function generate(schema) {
//poor mans deep-clone
var result = JSON.parse(JSON.stringify(schema.properties));
Object.keys(result).forEach(propertyName => {
var propertySchema = result[propertyName];
if (propertySchema["x-primary-key"]) {
propertySchema.primaryKey = true;
}
if (propertySchema["x-autoincrement"]) {
propertySchema.autoIncrement = true;
}
if (propertySchema["x-unique"]) {
propertySchema.unique = propertySchema["x-unique"];
}
if (propertySchema["x-allow-null"] !== undefined) {
propertySchema.allowNull = propertySchema["x-allow-null"];
}
propertySchema.type = getSequalizeType(propertySchema);
if (propertySchema.default !== undefined) {
if (propertySchema.format === "uuid") {
propertySchema.defaultValue = eval(propertySchema.default);
} else {
propertySchema.defaultValue = propertySchema.default;
}
}
});
return result;
}
module.exports = { setDialect, generate };