@questwork/q-utilities 0.1.1 → 0.1.3
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.
- package/dist/index.min.cjs +978 -0
- package/dist/index.min.js +964 -0
- package/package.json +33 -17
- package/index.js +0 -3
- package/lib/authenticator/authenticator.js +0 -56
- package/lib/authenticator/clientApp.js +0 -46
- package/lib/authenticator/index.js +0 -5
- package/lib/convertString/convertString.js +0 -20
- package/lib/convertString/index.js +0 -1
- package/lib/cryptoHelper/cryptoHelper.js +0 -93
- package/lib/cryptoHelper/index.js +0 -5
- package/lib/getValidation/getValidation.js +0 -115
- package/lib/getValidation/index.js +0 -1
- package/lib/getValueByKeys/getValueByKeys.js +0 -20
- package/lib/getValueByKeys/index.js +0 -1
- package/lib/index.js +0 -14
- package/lib/jwtHelper/index.js +0 -5
- package/lib/jwtHelper/jwtHelper.js +0 -254
- package/lib/keyValueObject/index.js +0 -1
- package/lib/keyValueObject/keyValueObject.js +0 -163
|
@@ -0,0 +1,964 @@
|
|
|
1
|
+
/******/ // The require scope
|
|
2
|
+
/******/ var __webpack_require__ = {};
|
|
3
|
+
/******/
|
|
4
|
+
/************************************************************************/
|
|
5
|
+
/******/ /* webpack/runtime/define property getters */
|
|
6
|
+
/******/ (() => {
|
|
7
|
+
/******/ // define getter functions for harmony exports
|
|
8
|
+
/******/ __webpack_require__.d = (exports, definition) => {
|
|
9
|
+
/******/ for(var key in definition) {
|
|
10
|
+
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
|
|
11
|
+
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
|
|
12
|
+
/******/ }
|
|
13
|
+
/******/ }
|
|
14
|
+
/******/ };
|
|
15
|
+
/******/ })();
|
|
16
|
+
/******/
|
|
17
|
+
/******/ /* webpack/runtime/hasOwnProperty shorthand */
|
|
18
|
+
/******/ (() => {
|
|
19
|
+
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
20
|
+
/******/ })();
|
|
21
|
+
/******/
|
|
22
|
+
/************************************************************************/
|
|
23
|
+
var __webpack_exports__ = {};
|
|
24
|
+
|
|
25
|
+
// EXPORTS
|
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
+
sh: () => (/* reexport */ ApiResponse),
|
|
28
|
+
Yc: () => (/* reexport */ KeyValueObject),
|
|
29
|
+
OS: () => (/* reexport */ Metadata),
|
|
30
|
+
Z8: () => (/* reexport */ QMeta),
|
|
31
|
+
lc: () => (/* reexport */ Repo),
|
|
32
|
+
kl: () => (/* reexport */ Service),
|
|
33
|
+
l0: () => (/* reexport */ convertString),
|
|
34
|
+
Yq: () => (/* reexport */ formatDate),
|
|
35
|
+
G8: () => (/* reexport */ getValidation),
|
|
36
|
+
pY: () => (/* reexport */ getValueByKeys),
|
|
37
|
+
su: () => (/* reexport */ makeApiResponse),
|
|
38
|
+
Q6: () => (/* reexport */ makeService),
|
|
39
|
+
Lv: () => (/* reexport */ padZeros),
|
|
40
|
+
Qy: () => (/* reexport */ stringFormatter)
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
;// ./lib/helpers/convertString/convertString.js
|
|
44
|
+
function convertString(string, patternMatch = /\$\{(.+?)\}/g, value, getValueByKeys) {
|
|
45
|
+
if (!string || typeof getValueByKeys !== 'function') {
|
|
46
|
+
return ''
|
|
47
|
+
}
|
|
48
|
+
const reg = new RegExp(patternMatch, 'g')
|
|
49
|
+
return string.replace(reg, (match, key) => {
|
|
50
|
+
const result = getValueByKeys({ keys: key.split('.'), obj: value })
|
|
51
|
+
if (result === null || result === undefined) {
|
|
52
|
+
return ''
|
|
53
|
+
}
|
|
54
|
+
return typeof result === 'object' ? JSON.stringify(result) : result
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/* harmony default export */ const convertString_convertString = ({
|
|
59
|
+
convertString
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
;// ./lib/helpers/convertString/index.js
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
;// ./lib/helpers/formatDate/formatDate.js
|
|
67
|
+
|
|
68
|
+
function formatDate(date, format) {
|
|
69
|
+
const _date = date && date instanceof Date ? date : new Date(date)
|
|
70
|
+
const dayMapChi = ['日','一','二','三','四','五','六']
|
|
71
|
+
const dayMapEng = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
|
|
72
|
+
const dayMapEngShort = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat']
|
|
73
|
+
const _format = format || 'YYYY/MM/DD hh:mm'
|
|
74
|
+
const e = _date.getDay()
|
|
75
|
+
const ee = dayMapEngShort[e]
|
|
76
|
+
const eee = dayMapChi[e]
|
|
77
|
+
const eeee = dayMapEng[e]
|
|
78
|
+
const y = _date.getFullYear()
|
|
79
|
+
const m = _date.getMonth() + 1
|
|
80
|
+
const d = _date.getDate()
|
|
81
|
+
const h = _date.getHours()
|
|
82
|
+
const mm = _date.getMinutes()
|
|
83
|
+
const s = _date.getSeconds()
|
|
84
|
+
|
|
85
|
+
return _format.replace('YYYY', y)
|
|
86
|
+
.replace('MM', padding(m))
|
|
87
|
+
.replace('MM', padding(m))
|
|
88
|
+
.replace('DD', padding(d))
|
|
89
|
+
.replace('hh', padding(h))
|
|
90
|
+
.replace('mm', padding(mm))
|
|
91
|
+
.replace('ss', padding(s))
|
|
92
|
+
.replace('M', m)
|
|
93
|
+
.replace('D', d)
|
|
94
|
+
.replace('h', h)
|
|
95
|
+
.replace('m', mm)
|
|
96
|
+
.replace('s', s)
|
|
97
|
+
.replace('EEEE', padding(eeee))
|
|
98
|
+
.replace('EEE', padding(eee))
|
|
99
|
+
.replace('EE', padding(ee))
|
|
100
|
+
.replace('E', padding(e))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function padding(m) {
|
|
104
|
+
return m < 10 ? `0${m}` : m
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
/* harmony default export */ const formatDate_formatDate = ({
|
|
109
|
+
formatDate
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
;// ./lib/helpers/formatDate/index.js
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
;// ./lib/helpers/getValidation/getValidation.js
|
|
118
|
+
function getValidation(rule, data, getDataByKey, KeyValueObject) {
|
|
119
|
+
if (!rule) {
|
|
120
|
+
return true
|
|
121
|
+
}
|
|
122
|
+
if (typeof getDataByKey !== 'function' || typeof KeyValueObject !== 'function') {
|
|
123
|
+
return false
|
|
124
|
+
}
|
|
125
|
+
const { key = '', value, keyValuePath = '' } = rule
|
|
126
|
+
const [valueAttribute] = Object.keys(value)
|
|
127
|
+
|
|
128
|
+
if (!key) {
|
|
129
|
+
switch (valueAttribute) {
|
|
130
|
+
case '$and': {
|
|
131
|
+
return value['$and'].reduce((acc, item) => (acc && getValidation(item, data, getDataByKey, KeyValueObject)), true)
|
|
132
|
+
}
|
|
133
|
+
case '$or': {
|
|
134
|
+
return value['$or'].reduce((acc, item) => (acc || getValidation(item, data, getDataByKey, KeyValueObject)), false)
|
|
135
|
+
}
|
|
136
|
+
default:
|
|
137
|
+
return false
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
let rowValue = getDataByKey(key, data)
|
|
142
|
+
|
|
143
|
+
// debugger
|
|
144
|
+
|
|
145
|
+
// if KeyValue object
|
|
146
|
+
if (keyValuePath) {
|
|
147
|
+
console.log('keyValuePath', keyValuePath)
|
|
148
|
+
const rowValueData = KeyValueObject.toObject(rowValue)
|
|
149
|
+
rowValue = getDataByKey(keyValuePath, rowValueData)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
switch (valueAttribute) {
|
|
153
|
+
case '$empty': {
|
|
154
|
+
const isEmpty = rowValue === null || rowValue === undefined
|
|
155
|
+
return isEmpty === value['$empty']
|
|
156
|
+
}
|
|
157
|
+
case '$eq': {
|
|
158
|
+
return rowValue === value['$eq']
|
|
159
|
+
}
|
|
160
|
+
case '$gt': {
|
|
161
|
+
return rowValue > value['$gt']
|
|
162
|
+
}
|
|
163
|
+
case '$gte': {
|
|
164
|
+
return rowValue >= value['$gte']
|
|
165
|
+
}
|
|
166
|
+
case '$lt': {
|
|
167
|
+
return rowValue < value['$lt']
|
|
168
|
+
}
|
|
169
|
+
case '$lte': {
|
|
170
|
+
return rowValue <= value['$lte']
|
|
171
|
+
}
|
|
172
|
+
case '$in': {
|
|
173
|
+
if (Array.isArray(rowValue)) {
|
|
174
|
+
return !!rowValue.find((e) => (value['$in'].includes(e)))
|
|
175
|
+
}
|
|
176
|
+
if (typeof rowValue !== 'object') {
|
|
177
|
+
return !!value['$in'].includes(rowValue)
|
|
178
|
+
}
|
|
179
|
+
return false
|
|
180
|
+
}
|
|
181
|
+
case '$inValue': {
|
|
182
|
+
const result = getDataByKey(value['$inValue'], data)
|
|
183
|
+
const _value = Array.isArray(result) ? result : []
|
|
184
|
+
if (Array.isArray(rowValue)) {
|
|
185
|
+
return !!rowValue.find((e) => (_value.includes(e)))
|
|
186
|
+
}
|
|
187
|
+
if (typeof rowValue === 'string') {
|
|
188
|
+
return !!_value.includes(rowValue)
|
|
189
|
+
}
|
|
190
|
+
return false
|
|
191
|
+
}
|
|
192
|
+
case '$ne': {
|
|
193
|
+
return rowValue !== value['$ne']
|
|
194
|
+
}
|
|
195
|
+
case '$notIn': {
|
|
196
|
+
if (Array.isArray(rowValue)) {
|
|
197
|
+
return !rowValue.find((e) => (value['$notIn'].includes(e)))
|
|
198
|
+
}
|
|
199
|
+
if (typeof rowValue !== 'object') {
|
|
200
|
+
return !value['$notIn'].includes(rowValue)
|
|
201
|
+
}
|
|
202
|
+
return false
|
|
203
|
+
}
|
|
204
|
+
case '$notInValue': {
|
|
205
|
+
const result = getDataByKey(value['$notInValue'], data)
|
|
206
|
+
const _value = Array.isArray(result) ? result : []
|
|
207
|
+
if (Array.isArray(rowValue)) {
|
|
208
|
+
return !rowValue.find((e) => (_value.includes(e)))
|
|
209
|
+
}
|
|
210
|
+
if (typeof rowValue !== 'object') {
|
|
211
|
+
return !_value.includes(rowValue)
|
|
212
|
+
}
|
|
213
|
+
return false
|
|
214
|
+
}
|
|
215
|
+
case '$range': {
|
|
216
|
+
const [min, max] = value['$range']
|
|
217
|
+
if (typeof min === 'number' && typeof max === 'number' && rowValue >= min && rowValue <= max) {
|
|
218
|
+
return true
|
|
219
|
+
}
|
|
220
|
+
return false
|
|
221
|
+
}
|
|
222
|
+
default:
|
|
223
|
+
return false
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/* harmony default export */ const getValidation_getValidation = ({
|
|
228
|
+
getValidation
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
;// ./lib/helpers/getValidation/index.js
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
;// ./lib/helpers/getValueByKeys/getValueByKeys.js
|
|
236
|
+
// keys can be array or string
|
|
237
|
+
function getValueByKeys(keys, data) {
|
|
238
|
+
let _keys = keys
|
|
239
|
+
let _data = data
|
|
240
|
+
if (!Array.isArray(keys)) {
|
|
241
|
+
const { keys: keyArr, obj } = keys
|
|
242
|
+
_keys = keyArr
|
|
243
|
+
_data = obj
|
|
244
|
+
}
|
|
245
|
+
if (_keys.length === 0) {
|
|
246
|
+
return _data
|
|
247
|
+
}
|
|
248
|
+
const firstKey = _keys.shift()
|
|
249
|
+
if (_data && Object.prototype.hasOwnProperty.call(_data, firstKey)) {
|
|
250
|
+
return getValueByKeys(_keys, _data[firstKey])
|
|
251
|
+
}
|
|
252
|
+
if (_data && firstKey) {
|
|
253
|
+
return _data[firstKey]
|
|
254
|
+
}
|
|
255
|
+
return _data
|
|
256
|
+
|
|
257
|
+
}
|
|
258
|
+
/* harmony default export */ const getValueByKeys_getValueByKeys = ({
|
|
259
|
+
getValueByKeys
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
;// ./lib/helpers/getValueByKeys/index.js
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
;// ./lib/helpers/padZeros/padZeros.js
|
|
268
|
+
function padZeros(num, minLength = 6) {
|
|
269
|
+
num = num.toString()
|
|
270
|
+
if (num.length < minLength) {
|
|
271
|
+
return padZeros('0' + num, minLength)
|
|
272
|
+
}
|
|
273
|
+
return num
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
;// ./lib/helpers/padZeros/index.js
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
;// ./lib/helpers/stringFormatter/stringFormatter.js
|
|
284
|
+
function stringFormatter(str) {
|
|
285
|
+
return (str || '').toUpperCase().replace('-', '_').replace(' ', '_')
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
;// ./lib/helpers/stringFormatter/index.js
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
;// ./lib/helpers/index.js
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
;// ./lib/models/apiResponse/apiResponse.js
|
|
304
|
+
class ApiResponse {
|
|
305
|
+
constructor(options = {}) {
|
|
306
|
+
options = options || {}
|
|
307
|
+
this._data = options.data || options._data || []
|
|
308
|
+
this.err = options.err
|
|
309
|
+
this.isNew = options.isNew || false
|
|
310
|
+
this.message = options.message
|
|
311
|
+
this.total = options.total || 0
|
|
312
|
+
this._instanceBuilder = options._instanceBuilder
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
static init(options = {}) {
|
|
316
|
+
if (options instanceof this) {
|
|
317
|
+
return options
|
|
318
|
+
}
|
|
319
|
+
const instance = new this(options)
|
|
320
|
+
return instance
|
|
321
|
+
}
|
|
322
|
+
static get _classname() {
|
|
323
|
+
return 'ApiResponse'
|
|
324
|
+
}
|
|
325
|
+
static get _superclass() {
|
|
326
|
+
return 'ApiResponse'
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// getters
|
|
330
|
+
get data() {
|
|
331
|
+
if (this._instanceBuilder && (typeof this._instanceBuilder === 'function')) {
|
|
332
|
+
return this._data.map(this._instanceBuilder)
|
|
333
|
+
}
|
|
334
|
+
return this._data
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
;// ./lib/models/apiResponse/makeApiResponse.js
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
function makeApiResponse({ repo, result }) {
|
|
344
|
+
return ApiResponse.init({
|
|
345
|
+
...result,
|
|
346
|
+
_instanceBuilder: (i) => {
|
|
347
|
+
return repo.init(i)
|
|
348
|
+
}
|
|
349
|
+
})
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
|
|
354
|
+
;// ./lib/models/apiResponse/index.js
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
;// ./lib/models/keyValueObject/keyValueObject.js
|
|
361
|
+
class KeyValueObject {
|
|
362
|
+
constructor(options = {}) {
|
|
363
|
+
options = options || {}
|
|
364
|
+
this.key = options.key || null
|
|
365
|
+
this.value = (typeof options.value !== 'undefined') ? options.value : ''
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// Class methods
|
|
369
|
+
static init(options = {}) {
|
|
370
|
+
if (options instanceof this) {
|
|
371
|
+
return options
|
|
372
|
+
}
|
|
373
|
+
const instance = new this(options)
|
|
374
|
+
return instance.isValid ? instance : null
|
|
375
|
+
}
|
|
376
|
+
static initFromArray(arr = []) {
|
|
377
|
+
if (Array.isArray(arr)) {
|
|
378
|
+
return arr.map((a) => this.init(a))
|
|
379
|
+
}
|
|
380
|
+
return []
|
|
381
|
+
}
|
|
382
|
+
static initOnlyValidFromArray(arr = []) {
|
|
383
|
+
return this.initFromArray(arr).filter((i) => i)
|
|
384
|
+
}
|
|
385
|
+
static get _classname() {
|
|
386
|
+
return 'KeyValueObject'
|
|
387
|
+
}
|
|
388
|
+
static get _superclass() {
|
|
389
|
+
return 'KeyValueObject'
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
static addItem(arr, key, value) {
|
|
393
|
+
arr.push(
|
|
394
|
+
{ key, value }
|
|
395
|
+
)
|
|
396
|
+
}
|
|
397
|
+
static addRecord(arr = [], key, value) {
|
|
398
|
+
const self = this
|
|
399
|
+
if (!this.hasKeyValue(arr, key, value)) {
|
|
400
|
+
arr.push(self.init({ key, value }))
|
|
401
|
+
}
|
|
402
|
+
return arr
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
static appendRecord(arr = [], key, value) {
|
|
406
|
+
return arr.map((item) => {
|
|
407
|
+
if (item.key === key) {
|
|
408
|
+
item.value = [...item.value, ...value]
|
|
409
|
+
}
|
|
410
|
+
return item
|
|
411
|
+
})
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
static fromObject(options = {}) {
|
|
415
|
+
const self = this
|
|
416
|
+
return Object.keys(options).reduce((acc, key) => {
|
|
417
|
+
acc.push(self.init({ key, value: options[key] }))
|
|
418
|
+
return acc
|
|
419
|
+
}, [])
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
static removeByKey(arr, key) {
|
|
423
|
+
return arr.reduce((acc, item) => {
|
|
424
|
+
if (item.key !== key) {
|
|
425
|
+
acc.push(item)
|
|
426
|
+
}
|
|
427
|
+
return acc
|
|
428
|
+
}, [])
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
static foundByKey(arr = [], key) {
|
|
432
|
+
const found = arr.find((m) => {
|
|
433
|
+
return m.key === key
|
|
434
|
+
})
|
|
435
|
+
return found || null
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
static foundValueByKey(arr = [], key) {
|
|
439
|
+
const found = this.foundByKey(arr, key)
|
|
440
|
+
return found ? found.value : null
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
static getValueByKey(arr = [], key) {
|
|
444
|
+
const found = arr.find((i) => {
|
|
445
|
+
return i.key === key
|
|
446
|
+
})
|
|
447
|
+
if (found) {
|
|
448
|
+
return found.value
|
|
449
|
+
}
|
|
450
|
+
return null
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
static getValueByKeyFromArray(arr = [], key) {
|
|
454
|
+
if (arr.length === 0) {
|
|
455
|
+
return null
|
|
456
|
+
}
|
|
457
|
+
const firstArr = arr.shift()
|
|
458
|
+
const found = firstArr.find((i) => {
|
|
459
|
+
return i.key === key
|
|
460
|
+
})
|
|
461
|
+
if (found && found.value) {
|
|
462
|
+
return found.value
|
|
463
|
+
}
|
|
464
|
+
return this.getValueByKeyFromArray(arr, key)
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
static getValuesByKey(arr = [], key) {
|
|
468
|
+
return arr.reduce((acc, item) => {
|
|
469
|
+
if (item.key === key) {
|
|
470
|
+
acc.push(item.value)
|
|
471
|
+
}
|
|
472
|
+
return acc
|
|
473
|
+
}, [])
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
static hasKeyValue(arr = [], key, value) {
|
|
477
|
+
if (typeof value === 'undefined') {
|
|
478
|
+
return arr.filter((item) => item.key === key).length > 0
|
|
479
|
+
}
|
|
480
|
+
return arr.filter((item) => (item.key === key && item.value === value)).length > 0
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
static insertOrUpdateRecord(arr = [], key, value) {
|
|
484
|
+
const self = this
|
|
485
|
+
let copy = [...arr]
|
|
486
|
+
if (!self.hasKeyValue(arr, key)) {
|
|
487
|
+
copy.push(self.init({ key, value }))
|
|
488
|
+
} else {
|
|
489
|
+
copy = self.updateRecord(arr, key, value)
|
|
490
|
+
}
|
|
491
|
+
return copy
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
static keys(arr = []) {
|
|
495
|
+
if (Array.isArray(arr)) {
|
|
496
|
+
return arr.reduce((acc, item) => {
|
|
497
|
+
acc.push(item.key)
|
|
498
|
+
return acc
|
|
499
|
+
}, [])
|
|
500
|
+
}
|
|
501
|
+
return []
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
static merge(toArr, fromArr) {
|
|
505
|
+
(fromArr || []).map((from) => {
|
|
506
|
+
const found = toArr.find((to) => {
|
|
507
|
+
return to.key === from.key
|
|
508
|
+
})
|
|
509
|
+
if (found) {
|
|
510
|
+
found.value = (found.value || []).concat(from.value)
|
|
511
|
+
} else {
|
|
512
|
+
toArr.push(from)
|
|
513
|
+
}
|
|
514
|
+
})
|
|
515
|
+
return toArr
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
static toObject(arr = []) {
|
|
519
|
+
if (Array.isArray(arr)) {
|
|
520
|
+
return arr.reduce((acc, item) => {
|
|
521
|
+
acc[item.key] = item.value
|
|
522
|
+
return acc
|
|
523
|
+
}, {})
|
|
524
|
+
}
|
|
525
|
+
return {}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
static toString(arr = [], delimiter = '; ') {
|
|
529
|
+
if (Array.isArray(arr)) {
|
|
530
|
+
return arr.reduce((acc, item) => {
|
|
531
|
+
acc.push(`${item.key}: ${item.value}`)
|
|
532
|
+
return acc
|
|
533
|
+
}, []).join(delimiter)
|
|
534
|
+
}
|
|
535
|
+
return ''
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
static updateRecord(arr = [], key, value) {
|
|
539
|
+
return arr.map((item) => {
|
|
540
|
+
if (item.key === key) {
|
|
541
|
+
return {
|
|
542
|
+
...item,
|
|
543
|
+
value
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
return item
|
|
547
|
+
})
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
static updateOrInsertRecord(arr = [], key, value) {
|
|
551
|
+
return this.insertOrUpdateRecord(arr, key, value)
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
static updateRecordsFromArray(arr = [], updateArr = []) {
|
|
555
|
+
if (Array.isArray(arr) && Array.isArray(updateArr)) {
|
|
556
|
+
const obj1 = this.toObject(arr)
|
|
557
|
+
const obj2 = this.toObject(updateArr)
|
|
558
|
+
return this.fromObject({
|
|
559
|
+
...obj1,
|
|
560
|
+
...obj2
|
|
561
|
+
})
|
|
562
|
+
}
|
|
563
|
+
return []
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
static values(arr = []) {
|
|
567
|
+
if (Array.isArray(arr)) {
|
|
568
|
+
return arr.reduce((acc, item) => {
|
|
569
|
+
acc.push(item.value)
|
|
570
|
+
return acc
|
|
571
|
+
}, [])
|
|
572
|
+
}
|
|
573
|
+
return []
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// getters
|
|
577
|
+
get isValid() {
|
|
578
|
+
return !!this.key
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
get toObject() {
|
|
582
|
+
const obj = {}
|
|
583
|
+
if (this.isValid) {
|
|
584
|
+
obj[this.key] = this.value
|
|
585
|
+
}
|
|
586
|
+
return obj
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
;// ./lib/models/keyValueObject/index.js
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
;// ./lib/models/metadata/metadata.js
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
class Metadata extends KeyValueObject {
|
|
602
|
+
static init(options = {}) {
|
|
603
|
+
if (options instanceof this) {
|
|
604
|
+
return options
|
|
605
|
+
}
|
|
606
|
+
const instance = new this({
|
|
607
|
+
...options,
|
|
608
|
+
key: stringFormatter(options.key),
|
|
609
|
+
})
|
|
610
|
+
return instance.isValid ? instance : null
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
static foundByKey(arr = [], key) {
|
|
614
|
+
const found = (arr || []).find((m) => {
|
|
615
|
+
return m.key === stringFormatter(key)
|
|
616
|
+
})
|
|
617
|
+
return found || null
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
static get _classname() {
|
|
621
|
+
return 'Metadata'
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
;// ./lib/models/metadata/index.js
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
;// ./lib/models/qMeta/qMeta.js
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+
const updateAllowedProps = [
|
|
636
|
+
'attributes',
|
|
637
|
+
'ref'
|
|
638
|
+
]
|
|
639
|
+
|
|
640
|
+
class QMeta {
|
|
641
|
+
constructor(options = {}) {
|
|
642
|
+
options = options || {}
|
|
643
|
+
this.attributes = KeyValueObject.initOnlyValidFromArray(options.attributes)
|
|
644
|
+
this.ref = options.ref || {}
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
static get _classname() {
|
|
648
|
+
return 'QMeta'
|
|
649
|
+
}
|
|
650
|
+
static get _superclass() {
|
|
651
|
+
return 'QMeta'
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
// Class methods
|
|
655
|
+
static init(options = {}) {
|
|
656
|
+
if (options instanceof QMeta) {
|
|
657
|
+
return options
|
|
658
|
+
}
|
|
659
|
+
return new QMeta(options)
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// instance methods
|
|
663
|
+
addAttribute(obj) {
|
|
664
|
+
const kvObject = KeyValueObject.init(obj)
|
|
665
|
+
if (!kvObject) {
|
|
666
|
+
throw new Error('invalid meta attribute')
|
|
667
|
+
}
|
|
668
|
+
this.attributes.push(kvObject)
|
|
669
|
+
return this
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
update(obj) {
|
|
673
|
+
Object.keys(obj).forEach((key) => {
|
|
674
|
+
if (updateAllowedProps.includes(key)) {
|
|
675
|
+
if (key === 'attributes') {
|
|
676
|
+
this[key] = KeyValueObject.initOnlyValidFromArray(obj[key])
|
|
677
|
+
} else {
|
|
678
|
+
this[key] = obj[key]
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
})
|
|
682
|
+
return this
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
;// ./lib/models/qMeta/index.js
|
|
689
|
+
|
|
690
|
+
|
|
691
|
+
|
|
692
|
+
|
|
693
|
+
;// ./lib/models/repo/repo.js
|
|
694
|
+
class Repo {
|
|
695
|
+
constructor(options) {
|
|
696
|
+
options = options || {}
|
|
697
|
+
this.model = options.model
|
|
698
|
+
this._sharedOptions = options._sharedOptions // { session: this.dbTransaction }
|
|
699
|
+
this._queryOptions = options._queryOptions
|
|
700
|
+
this._saveOptions = options._saveOptions
|
|
701
|
+
this._Class = options._constructor && options._constructor._Class
|
|
702
|
+
? options._constructor._Class
|
|
703
|
+
: null
|
|
704
|
+
}
|
|
705
|
+
static init(options = {}) {
|
|
706
|
+
if (options instanceof this) {
|
|
707
|
+
return options
|
|
708
|
+
}
|
|
709
|
+
const instance = new this(options)
|
|
710
|
+
return instance.isValid ? instance : null
|
|
711
|
+
}
|
|
712
|
+
static get _classname() {
|
|
713
|
+
return 'Repo'
|
|
714
|
+
}
|
|
715
|
+
static get _superclass() {
|
|
716
|
+
return 'Repo'
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
get _classname() {
|
|
720
|
+
return 'Repo'
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
get _superclass() {
|
|
724
|
+
return 'Repo'
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
get isValid() {
|
|
728
|
+
return this.model
|
|
729
|
+
&& (typeof this.model.deleteOne === 'function')
|
|
730
|
+
&& (typeof this.model.findAll === 'function')
|
|
731
|
+
&& (typeof this.model.saveOne === 'function')
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
get queryOptions() {
|
|
735
|
+
return {
|
|
736
|
+
...this._sharedOptions,
|
|
737
|
+
...this._queryOptions,
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
get saveOptions() {
|
|
742
|
+
return {
|
|
743
|
+
...this._sharedOptions,
|
|
744
|
+
...this._saveOptions,
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
init(options) {
|
|
749
|
+
if (this._Class && typeof this._Class.init === 'function') {
|
|
750
|
+
return this._Class.init(options)
|
|
751
|
+
}
|
|
752
|
+
return options
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
async deleteOne({ id }) {
|
|
756
|
+
try {
|
|
757
|
+
const result = await this.model.deleteOne({ _id: id })
|
|
758
|
+
return {
|
|
759
|
+
...result, // { message: 'ok', total }
|
|
760
|
+
isNew: false,
|
|
761
|
+
data: []
|
|
762
|
+
}
|
|
763
|
+
} catch (err) {
|
|
764
|
+
throw err
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
findAll({ query }) {
|
|
769
|
+
return new Promise((resolve, reject) => {
|
|
770
|
+
this.model.findAll(query, this.queryOptions, (err, data, total) => {
|
|
771
|
+
if (err) {
|
|
772
|
+
reject(err)
|
|
773
|
+
} else {
|
|
774
|
+
resolve({
|
|
775
|
+
isNew: false,
|
|
776
|
+
data,
|
|
777
|
+
total: total || data.length
|
|
778
|
+
})
|
|
779
|
+
}
|
|
780
|
+
})
|
|
781
|
+
})
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
findOne({ query }) {
|
|
785
|
+
return new Promise((resolve, reject) => {
|
|
786
|
+
this.model.findAll(query, this.queryOptions, (err, data) => {
|
|
787
|
+
if (err) {
|
|
788
|
+
reject(err)
|
|
789
|
+
} else if (data.length === 1) {
|
|
790
|
+
resolve({
|
|
791
|
+
isNew: false,
|
|
792
|
+
data,
|
|
793
|
+
total: 1
|
|
794
|
+
})
|
|
795
|
+
} else if (data.length === 0) {
|
|
796
|
+
reject(new Error('record not found'))
|
|
797
|
+
} else {
|
|
798
|
+
reject(new Error('more than one is found'))
|
|
799
|
+
}
|
|
800
|
+
})
|
|
801
|
+
})
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
saveAll({ docs }) {
|
|
805
|
+
let isNew
|
|
806
|
+
return Promise.all(docs.map(async (doc) => {
|
|
807
|
+
if (doc) {
|
|
808
|
+
const result = await this.saveOne({ doc })
|
|
809
|
+
isNew = result.isNew
|
|
810
|
+
const _data = result._data || result.data
|
|
811
|
+
return _data[0]
|
|
812
|
+
}
|
|
813
|
+
return null
|
|
814
|
+
})).then((savedData) => {
|
|
815
|
+
if (savedData.length !== 1) isNew = null
|
|
816
|
+
return {
|
|
817
|
+
data: savedData,
|
|
818
|
+
isNew,
|
|
819
|
+
total: savedData.length
|
|
820
|
+
}
|
|
821
|
+
})
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
saveOne({ doc }) {
|
|
825
|
+
return new Promise((resolve, reject) => {
|
|
826
|
+
this.model.saveOne(doc, this.saveOptions, (err, result) => {
|
|
827
|
+
if (err) {
|
|
828
|
+
reject(err)
|
|
829
|
+
} else {
|
|
830
|
+
resolve(result)
|
|
831
|
+
}
|
|
832
|
+
})
|
|
833
|
+
})
|
|
834
|
+
}
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
;// ./lib/models/repo/index.js
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
|
|
844
|
+
;// ./lib/models/service/service.js
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
|
|
848
|
+
class Service {
|
|
849
|
+
constructor({ repo }) {
|
|
850
|
+
this.repo = repo
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
static get _classname() {
|
|
854
|
+
return 'Service'
|
|
855
|
+
}
|
|
856
|
+
static get _superclass() {
|
|
857
|
+
return 'Service'
|
|
858
|
+
}
|
|
859
|
+
|
|
860
|
+
deleteOne({ id }) {
|
|
861
|
+
return this.repo.deleteOne({ id })
|
|
862
|
+
.catch(() => {
|
|
863
|
+
throw new Error(`Not found for query: ${id}`)
|
|
864
|
+
})
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
async findAll({ query = {} } = {}) {
|
|
868
|
+
const result = await this.repo.findAll({ query })
|
|
869
|
+
return makeApiResponse({
|
|
870
|
+
repo: this.repo,
|
|
871
|
+
result
|
|
872
|
+
})
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
async findOne({ query = {} } = {}) {
|
|
876
|
+
const result = await this.repo.findOne({ query })
|
|
877
|
+
return makeApiResponse({
|
|
878
|
+
repo: this.repo,
|
|
879
|
+
result
|
|
880
|
+
})
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
init(options) {
|
|
884
|
+
return this.repo.init(options)
|
|
885
|
+
}
|
|
886
|
+
initFromArray(arr = []) {
|
|
887
|
+
return arr.map((i) => this.init(i))
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
async saveAll({ docs = [] } = {}) {
|
|
891
|
+
const copies = docs.map((doc) => {
|
|
892
|
+
return this.init(doc)
|
|
893
|
+
})
|
|
894
|
+
const result = await this.repo.saveAll({ docs: copies })
|
|
895
|
+
return makeApiResponse({
|
|
896
|
+
repo: this.repo,
|
|
897
|
+
result
|
|
898
|
+
})
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
async saveOne({ doc = {} } = {}) {
|
|
902
|
+
const copy = this.init(doc)
|
|
903
|
+
if (copy) {
|
|
904
|
+
const result = await this.repo.saveOne({ doc: copy })
|
|
905
|
+
return makeApiResponse({
|
|
906
|
+
repo: this.repo,
|
|
907
|
+
result
|
|
908
|
+
})
|
|
909
|
+
}
|
|
910
|
+
return {
|
|
911
|
+
isNew: null,
|
|
912
|
+
data: [],
|
|
913
|
+
err: new Error('doc is not a valid instance')
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
function makeService({ repo }) {
|
|
919
|
+
if (repo === undefined) {
|
|
920
|
+
throw new Error('repo is required.')
|
|
921
|
+
}
|
|
922
|
+
if (repo._superclass !== Repo._superclass) {
|
|
923
|
+
throw new Error('repo is not an instance of Repo.')
|
|
924
|
+
}
|
|
925
|
+
return new Service({ repo })
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
;// ./lib/models/service/index.js
|
|
931
|
+
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
|
|
935
|
+
;// ./lib/models/index.js
|
|
936
|
+
|
|
937
|
+
|
|
938
|
+
|
|
939
|
+
|
|
940
|
+
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
;// ./lib/index.js
|
|
944
|
+
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
;// ./index.js
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
var __webpack_exports__ApiResponse = __webpack_exports__.sh;
|
|
951
|
+
var __webpack_exports__KeyValueObject = __webpack_exports__.Yc;
|
|
952
|
+
var __webpack_exports__Metadata = __webpack_exports__.OS;
|
|
953
|
+
var __webpack_exports__QMeta = __webpack_exports__.Z8;
|
|
954
|
+
var __webpack_exports__Repo = __webpack_exports__.lc;
|
|
955
|
+
var __webpack_exports__Service = __webpack_exports__.kl;
|
|
956
|
+
var __webpack_exports__convertString = __webpack_exports__.l0;
|
|
957
|
+
var __webpack_exports__formatDate = __webpack_exports__.Yq;
|
|
958
|
+
var __webpack_exports__getValidation = __webpack_exports__.G8;
|
|
959
|
+
var __webpack_exports__getValueByKeys = __webpack_exports__.pY;
|
|
960
|
+
var __webpack_exports__makeApiResponse = __webpack_exports__.su;
|
|
961
|
+
var __webpack_exports__makeService = __webpack_exports__.Q6;
|
|
962
|
+
var __webpack_exports__padZeros = __webpack_exports__.Lv;
|
|
963
|
+
var __webpack_exports__stringFormatter = __webpack_exports__.Qy;
|
|
964
|
+
export { __webpack_exports__ApiResponse as ApiResponse, __webpack_exports__KeyValueObject as KeyValueObject, __webpack_exports__Metadata as Metadata, __webpack_exports__QMeta as QMeta, __webpack_exports__Repo as Repo, __webpack_exports__Service as Service, __webpack_exports__convertString as convertString, __webpack_exports__formatDate as formatDate, __webpack_exports__getValidation as getValidation, __webpack_exports__getValueByKeys as getValueByKeys, __webpack_exports__makeApiResponse as makeApiResponse, __webpack_exports__makeService as makeService, __webpack_exports__padZeros as padZeros, __webpack_exports__stringFormatter as stringFormatter };
|