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