evaengine 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/package.json +1 -1
  2. package/src/commands/index.js +12 -0
  3. package/src/commands/interface.js +45 -0
  4. package/src/commands/make_entity.js +490 -0
  5. package/src/commands/tramp.js +34 -0
  6. package/src/config/index.js +96 -0
  7. package/src/di.js +84 -0
  8. package/src/engine.js +551 -0
  9. package/src/entities/index.js +273 -0
  10. package/src/exceptions/index.js +501 -0
  11. package/src/middlewares/auth.js +67 -0
  12. package/src/middlewares/auth_kong.js +28 -0
  13. package/src/middlewares/debug.js +88 -0
  14. package/src/middlewares/index.js +18 -0
  15. package/src/middlewares/providers.js +73 -0
  16. package/src/middlewares/session.js +58 -0
  17. package/src/middlewares/trace.js +229 -0
  18. package/src/middlewares/validator.js +50 -0
  19. package/src/middlewares/view_cache.js +127 -0
  20. package/src/services/cache.js +223 -0
  21. package/src/services/config.js +111 -0
  22. package/src/services/env.js +28 -0
  23. package/src/services/event_manager.js +84 -0
  24. package/src/services/http_client.js +160 -0
  25. package/src/services/index.js +27 -0
  26. package/src/services/interface.js +5 -0
  27. package/src/services/joi.js +13 -0
  28. package/src/services/jwt_token.js +89 -0
  29. package/src/services/jwt_token_kong.js +75 -0
  30. package/src/services/logger.js +148 -0
  31. package/src/services/namespace.js +176 -0
  32. package/src/services/now.js +49 -0
  33. package/src/services/providers.js +158 -0
  34. package/src/services/redis.js +55 -0
  35. package/src/services/rest_client.js +112 -0
  36. package/src/swagger/index.js +668 -0
  37. package/src/utils/api_scaffold.js +240 -0
  38. package/src/utils/case_converter.js +33 -0
  39. package/src/utils/crc32.js +44 -0
  40. package/src/utils/datetime.js +13 -0
  41. package/src/utils/host.js +39 -0
  42. package/src/utils/index.js +35 -0
  43. package/src/utils/pagination.js +260 -0
  44. package/src/utils/random.js +23 -0
  45. package/src/utils/smart_query.js +342 -0
  46. package/src/utils/test.js +48 -0
  47. package/src/utils/wrapper.js +8 -0
@@ -0,0 +1,342 @@
1
+ import moment from 'moment-timezone';
2
+ import { InvalidArgumentException } from '../exceptions/index.js';
3
+
4
+ /**
5
+ * 自动根据 req.query 生成 sequelize 查询条件, 自动判断参数清单中是否有值
6
+ */
7
+ export default class SmartQuery {
8
+ constructor(query) {
9
+ this.query = query;
10
+ /**
11
+ * WHERE 查询条件
12
+ * @type {{}}
13
+ */
14
+ this.where = {};
15
+ /**'
16
+ * 排序列表
17
+ * @type {Array}
18
+ */
19
+ this.order = [];
20
+ }
21
+
22
+ /**
23
+ * 当 query 中包含名为 paramName 的属性时,为 fieldName 添加全等查询条件
24
+ * @param {String} paramName query 中的参数名称
25
+ * @param {String} fieldName 对应的数据库字段名称,默认与 paramName 一致
26
+ * @param {String} defaultValue 当 query 中不包含 paramName 或为空时,使用该值
27
+ */
28
+ equal(paramName, fieldName = paramName, defaultValue = null) {
29
+ const value = this.determineParam(paramName) ? this.query[paramName] : defaultValue;
30
+ if (value != null) {
31
+ this.applyWhere(fieldName, '$eq', value);
32
+ }
33
+
34
+ return this;
35
+ }
36
+
37
+ /**
38
+ * 当 query 中包含名为 paramName 的属性时,为 fieldName 添加 in 查询条件, 值可以为 `delimiter` 分割的字符串或者数组
39
+ * @param {String} paramName query 中的参数名称
40
+ * @param {String} [fieldName=paramName] 对应的数据库字段名称,默认与 paramName 一致
41
+ * @param {String|Array} [defaultValue=null] 当 query 中不包含 paramName 或为空时,使用该值
42
+ * @param {String} [delimiter=,] 当值为字符串时的分隔符,默认为 ,
43
+ */
44
+ in(paramName, fieldName = paramName, defaultValue = null, delimiter = ',') {
45
+ return this.applyIn(paramName, fieldName, defaultValue, delimiter, false);
46
+ }
47
+
48
+ /**
49
+ * 当 query 中包含名为 paramName 的属性时,为 fieldName 添加 not in 查询条件, 值可以为 `delimiter` 分割的字符串或者数组
50
+ * @param {String} paramName query 中的参数名称
51
+ * @param {String} [fieldName=paramName] 对应的数据库字段名称,默认与 paramName 一致
52
+ * @param {String|Array} [defaultValue=null] 当 query 中不包含 paramName 或为空时,使用该值
53
+ * @param {String} [delimiter=,] 当值为字符串时的分隔符,默认为 ,
54
+ */
55
+ notIn(paramName, fieldName = paramName, defaultValue = null, delimiter = ',') {
56
+ return this.applyIn(paramName, fieldName, defaultValue, delimiter, true);
57
+ }
58
+
59
+ /**
60
+ * 当 query 中包含名为 paramName 的属性时,为 fieldName 添加 LIKE 查询条件,并自动在值的头尾添加 %
61
+ * @param {String} paramName query 中的参数名称
62
+ * @param {String} fieldName 对应的数据库字段名称,默认与 paramName 一致
63
+ * @param {String} defaultValue 当 query 中不包含 paramName 或为空时,使用该值
64
+ */
65
+ like(paramName, fieldName = paramName, defaultValue = null) {
66
+ const value = this.determineParam(paramName) ? this.query[paramName] : defaultValue;
67
+ if (value != null) {
68
+ this.applyWhere(fieldName, '$like', `%${value}%`);
69
+ }
70
+
71
+ return this;
72
+ }
73
+
74
+ /**
75
+ * 当 query 中包含名为 paramName 的属性时,为 fieldName 添加 LIKE 查询条件,并自动在值的尾部添加 %
76
+ * @param {String} paramName query 中的参数名称
77
+ * @param {String} fieldName 对应的数据库字段名称,默认与 paramName 一致
78
+ * @param {String} defaultValue 当 query 中不包含 paramName 或为空时,使用该值
79
+ */
80
+ startsWith(paramName, fieldName = paramName, defaultValue = null) {
81
+ const value = this.determineParam(paramName) ? this.query[paramName] : defaultValue;
82
+ if (value != null) {
83
+ this.applyWhere(fieldName, '$like', `${value}%`);
84
+ }
85
+
86
+ return this;
87
+ }
88
+
89
+ /**
90
+ * 当 query 中包含名为 paramName 的属性时,为 fieldName 添加 LIKE 查询条件,并自动在值的头部添加 %
91
+ * @param {String} paramName query 中的参数名称
92
+ * @param {String} fieldName 对应的数据库字段名称,默认与 paramName 一致
93
+ * @param {String} defaultValue 当 query 中不包含 paramName 或为空时,使用该值
94
+ */
95
+ endsWith(paramName, fieldName = paramName, defaultValue = null) {
96
+ const value = this.determineParam(paramName) ? this.query[paramName] : defaultValue;
97
+ if (value != null) {
98
+ this.applyWhere(fieldName, '$like', `%${value}`);
99
+ }
100
+
101
+ return this;
102
+ }
103
+
104
+ /**
105
+ * 当 query 中包含名为 paramName 的属性时,为 fieldName 添加 >= 查询条件
106
+ * @param {String} paramName query 中的参数名称
107
+ * @param {String} fieldName 对应的数据库字段名称,默认与 paramName 一致
108
+ * @param {String} defaultValue 当 query 中不包含 paramName 或为空时,使用该值
109
+ */
110
+ gte(paramName, fieldName = paramName, defaultValue = null) {
111
+ const value = this.determineParam(paramName) ? this.query[paramName] : defaultValue;
112
+ if (value != null) {
113
+ this.applyWhere(fieldName, '$gte', value);
114
+ }
115
+
116
+ return this;
117
+ }
118
+
119
+ /**
120
+ * 当 query 中包含名为 paramName 的属性时,为 fieldName 添加 <= 查询条件
121
+ * @param {String} paramName query 中的参数名称
122
+ * @param {String} fieldName 对应的数据库字段名称,默认与 paramName 一致
123
+ * @param {String} defaultValue 当 query 中不包含 paramName 或为空时,使用该值
124
+ */
125
+ lte(paramName, fieldName = paramName, defaultValue = null) {
126
+ const value = this.determineParam(paramName) ? this.query[paramName] : defaultValue;
127
+ if (value != null) {
128
+ this.applyWhere(fieldName, '$lte', value);
129
+ }
130
+
131
+ return this;
132
+ }
133
+
134
+ /**
135
+ * 当 query 中包含名为 minParamName 的属性时,为 fieldName 添加 >= 查询条件,
136
+ * 当 query 中包含名为 maxParamName 的属性时,为 fieldName 添加 <= 查询条件
137
+ * @param {String} minParamName query 中的最小值参数名称
138
+ * @param {String} maxParamName query 中的最大值参数名称
139
+ * @param {String} fieldName 对应的数据库字段名称
140
+ * @param {String} minDefault 当 query 中不包含 minParamName 或为空时,使用该值作为默认值
141
+ * @param {String} maxDefault 当 query 中不包含 maxParamName 或为空时,使用该值作为默认值
142
+ */
143
+ range(minParamName, maxParamName, fieldName, minDefault = null, maxDefault = null) {
144
+ const min = this.determineParam(minParamName) ? this.query[minParamName] : minDefault;
145
+ const max = this.determineParam(maxParamName) ? this.query[maxParamName] : maxDefault;
146
+ if (min != null) {
147
+ this.applyWhere(fieldName, '$gte', min);
148
+ }
149
+ if (max != null) {
150
+ this.applyWhere(fieldName, '$lte', max);
151
+ }
152
+ return this;
153
+ }
154
+
155
+ /**
156
+ * 当 query 中包含名为 minParamName 的属性时,为 fieldName 添加 >= 查询条件并将时分秒设为0格式化到 targetFormat ,
157
+ * 当 query 中包含名为 maxParamName 的属性时,为 fieldName 添加 <= 查询条件并将时分秒分别设为 23:59:59 后格式化到 targetFormat
158
+ * @param {String} minParamName query 中的最小值参数名称
159
+ * @param {String} maxParamName query 中的最大值参数名称
160
+ * @param {String} fieldName 对应的数据库字段名称
161
+ * @param {String} [minDefault=null] 当 query 中不包含 minParamName 或为空时,使用该值作为默认值
162
+ * @param {String} [maxDefault=null] 当 query 中不包含 maxParamName 或为空时,使用该值作为默认值
163
+ * @param {String} [sourceFormat='YYYY-MM-DD'] 原值时间格式
164
+ * @param {String } [targetFormat='X'] 数据库时间格式
165
+ */
166
+ dateRange(minParamName, maxParamName, fieldName, minDefault = null, maxDefault = null, sourceFormat = 'YYYY-MM-DD', targetFormat = 'X') {
167
+ let min = this.determineParam(minParamName)
168
+ ? this.query[minParamName]
169
+ : null;
170
+ min = min != null && moment(min, sourceFormat).isValid()
171
+ ? moment(min, sourceFormat)
172
+ : moment(minDefault, sourceFormat);
173
+ let max = this.determineParam(maxParamName) ? this.query[maxParamName] : null;
174
+ max = max != null && moment(max, sourceFormat).isValid()
175
+ ? moment(max, sourceFormat)
176
+ : moment(maxDefault, sourceFormat);
177
+
178
+ if (min.isValid()) {
179
+ min = min.hours(0).minutes(0).seconds(0).format(targetFormat);
180
+ this.applyWhere(fieldName, '$gte', min);
181
+ }
182
+ if (max.isValid()) {
183
+ max = max.hours(23).minutes(59).seconds(59).format(targetFormat);
184
+ this.applyWhere(fieldName, '$lte', max);
185
+ }
186
+
187
+ return this;
188
+ }
189
+
190
+ /**
191
+ * 当 query 中包含名为 minParamName 的属性时,为 fieldName 添加 >= 查询条件并格式化到 targetFormat ,
192
+ * 当 query 中包含名为 maxParamName 的属性时,为 fieldName 添加 <= 查询条件并并格式化到 targetFormat
193
+ * @param {String} minParamName query 中的最小值参数名称
194
+ * @param {String} maxParamName query 中的最大值参数名称
195
+ * @param {String} fieldName 对应的数据库字段名称
196
+ * @param {String} [minDefault=null] 当 query 中不包含 minParamName 或为空时,使用该值作为默认值
197
+ * @param {String} [maxDefault=null] 当 query 中不包含 maxParamName 或为空时,使用该值作为默认值
198
+ * @param {String} [sourceFormat='YYYY-MM-DD'] 原值时间格式
199
+ * @param {String } [targetFormat='X'] 数据库时间格式
200
+ */
201
+ dateTimeRange(minParamName, maxParamName, fieldName, minDefault = null, maxDefault = null, sourceFormat = 'YYYY-MM-DD HH:mm:ss', targetFormat = 'X') {
202
+ let min = this.determineParam(minParamName)
203
+ ? this.query[minParamName]
204
+ : null;
205
+ min = min != null && moment(min, sourceFormat).isValid()
206
+ ? moment(min, sourceFormat)
207
+ : moment(minDefault, sourceFormat);
208
+ let max = this.determineParam(maxParamName) ? this.query[maxParamName] : null;
209
+ max = max != null && moment(max, sourceFormat).isValid()
210
+ ? moment(max, sourceFormat)
211
+ : moment(maxDefault, sourceFormat);
212
+
213
+ if (min.isValid()) {
214
+ min = min.format(targetFormat);
215
+ this.applyWhere(fieldName, '$gte', min);
216
+ }
217
+ if (max.isValid()) {
218
+ max = max.format(targetFormat);
219
+ this.applyWhere(fieldName, '$lte', max);
220
+ }
221
+
222
+ return this;
223
+ }
224
+
225
+ /**
226
+ * 处理用户的排序请求
227
+ * @param {Array} [autoMapping=[]] 根据字段名自动排序,数组成员为字段名,每个字段名自动生成正反序,
228
+ * 如 `created_at` 生成 `created_at` 正序和 `-created_at` 倒序
229
+ * @param {Object} [manualMapping={}]
230
+ * @param [defaultOrder=null] 默认排序,当 query 中不包含 `paramName` 属性时使用。如不设置则为 `autoMapping` 的第一个字段的倒序
231
+ * @param {String} [paramName='sort'] 参数名称
232
+ */
233
+ orderable(autoMapping = [], manualMapping = {}, defaultOrder = null, paramName = 'order') {
234
+ const ordersMapping = {};
235
+ let conventionOrder = defaultOrder;
236
+ if (autoMapping instanceof Array) {
237
+ autoMapping.forEach((field) => {
238
+ ordersMapping[`-${field}`] = [field, 'DESC'];
239
+ ordersMapping[field] = [field, 'ASC'];
240
+ });
241
+ if (conventionOrder === null) {
242
+ conventionOrder = [autoMapping[0], 'DESC'];
243
+ }
244
+ }
245
+
246
+ Object.assign(ordersMapping, manualMapping);
247
+
248
+ const order =
249
+ this.determineParam(paramName) && Object.keys(ordersMapping).includes(this.query[paramName])
250
+ ? ordersMapping[this.query[paramName]] : conventionOrder;
251
+ if (order != null) {
252
+ this.order = [order];
253
+ }
254
+
255
+ return this;
256
+ }
257
+
258
+ /**
259
+ * 获取完整的有效查询参数
260
+ * @returns {{}}
261
+ */
262
+ getCriteria() {
263
+ const criteria = {};
264
+ if (this.order instanceof Array && this.order.length > 0) {
265
+ criteria.order = this.order;
266
+ }
267
+
268
+ if (this.where instanceof Object && this.where.length > 0) {
269
+ criteria.where = this.where;
270
+ }
271
+
272
+ return criteria;
273
+ }
274
+
275
+ /**
276
+ * 当 query 中包含名为 paramName 的属性时,为 fieldName 添加 in 或 not in 查询条件, 值可以为 `delimiter` 分割的字符串或者数组
277
+ * @protected
278
+ * @param {String} paramName query 中的参数名称
279
+ * @param {String} fieldName 对应的数据库字段名称
280
+ * @param {String|Array} [defaultValue=null] 当 query 中不包含 paramName 或为空时,使用该值
281
+ * @param {String} [delimiter=,] 当值为字符串时的分隔符,默认为 ,
282
+ * @param {Boolean} [negation=false] 是否取反,为 true 时使用 $notIn
283
+ */
284
+ applyIn(paramName, fieldName, defaultValue = null, delimiter = ',', negation = false) {
285
+ let value = this.determineParam(paramName) ? this.query[paramName] : defaultValue;
286
+ if (value === null) {
287
+ return this;
288
+ }
289
+ if (typeof value === 'number') {
290
+ value = String(value);
291
+ }
292
+ if (typeof value === 'string') {
293
+ value = value.split(delimiter);
294
+ } else if (value instanceof Array === false) {
295
+ throw new InvalidArgumentException(`Value for contains must be typeof string or array, but \`${typeof value}\` given`);
296
+ }
297
+
298
+ this.applyWhere(fieldName, negation ? '$notIn' : '$in', value);
299
+ return this;
300
+ }
301
+
302
+ /**
303
+ * 添加 where
304
+ *
305
+ * @protected
306
+ * @param filedName
307
+ * @param predicate
308
+ * @param value
309
+ */
310
+ applyWhere(filedName, predicate, value) {
311
+ if (!Object.keys(this.where).includes(filedName)) {
312
+ this.where[filedName] = {};
313
+ }
314
+ this.where[filedName][predicate] = value;
315
+ }
316
+
317
+ /**
318
+ * 判定指定名称的参数是否有效
319
+ *
320
+ * @param paramName
321
+ * @returns {boolean}
322
+ */
323
+ determineParam(paramName) {
324
+ if (!Object.keys(this.query).includes(paramName)) {
325
+ return false;
326
+ }
327
+ const value = this.query[paramName];
328
+ if (value === null) {
329
+ return false;
330
+ }
331
+ if (typeof value === 'string') {
332
+ return value.trim() !== '';
333
+ }
334
+ if (value instanceof Array) {
335
+ return value.length > 0;
336
+ }
337
+ if (value instanceof Object) {
338
+ return Object.keys(value).length > 0;
339
+ }
340
+ return true;
341
+ }
342
+ }
@@ -0,0 +1,48 @@
1
+ import httpMocker from 'node-mocks-http';
2
+ import EventEmitter from 'events';
3
+ import DI from '../di.js';
4
+
5
+ export const truncateAll = async (entities) => {
6
+ const names = [];
7
+ const allEntities = entities.getAll();
8
+ Object.values(allEntities).forEach((entity) => {
9
+ if (typeof entity.truncate === 'function' && entity.tableName) {
10
+ names.push(`TRUNCATE \`${entity.tableName}\`;\n`);
11
+ }
12
+ });
13
+ await entities.getInstance().query(names.join(''));
14
+ };
15
+
16
+ const mockResponse = () => httpMocker.createResponse({ eventEmitter: EventEmitter });
17
+ export { mockResponse };
18
+
19
+ export const mockRequest = (...args) => httpMocker.createRequest(...args);
20
+
21
+ export const mockInstance = () =>
22
+ new Proxy({}, {
23
+ get: () =>
24
+ () => {
25
+ }
26
+ });
27
+
28
+ export const mockAuthRequest = (...args) => {
29
+ const uid = DI.get('config').get('token.faker.uid');
30
+ Object.assign(args[0], {
31
+ auth: {
32
+ uid
33
+ }
34
+ });
35
+ return httpMocker.createRequest(...args);
36
+ };
37
+
38
+ export { httpMocker };
39
+
40
+ export const runController =
41
+ (controller, request, response = mockResponse()) =>
42
+ new Promise((resolve, reject) => {
43
+ response.on(
44
+ 'end',
45
+ () => resolve(JSON.parse(response._getData()))
46
+ );
47
+ controller.handle(request, response, err => reject(err));
48
+ });
@@ -0,0 +1,8 @@
1
+ const wrapper = fn =>
2
+ (req, res, next) =>
3
+ Promise
4
+ .resolve(fn(req, res, next))
5
+ .catch(next);
6
+
7
+
8
+ export default wrapper;