@questwork/q-utilities 0.1.3 → 0.1.5
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 +502 -102
- package/dist/index.min.js +508 -103
- package/package.json +1 -1
package/dist/index.min.js
CHANGED
|
@@ -30,14 +30,19 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
30
30
|
Z8: () => (/* reexport */ QMeta),
|
|
31
31
|
lc: () => (/* reexport */ Repo),
|
|
32
32
|
kl: () => (/* reexport */ Service),
|
|
33
|
+
_x: () => (/* reexport */ UniqueKeyGenerator),
|
|
33
34
|
l0: () => (/* reexport */ convertString),
|
|
34
35
|
Yq: () => (/* reexport */ formatDate),
|
|
36
|
+
zn: () => (/* reexport */ generalPost),
|
|
35
37
|
G8: () => (/* reexport */ getValidation),
|
|
36
38
|
pY: () => (/* reexport */ getValueByKeys),
|
|
37
39
|
su: () => (/* reexport */ makeApiResponse),
|
|
38
40
|
Q6: () => (/* reexport */ makeService),
|
|
41
|
+
UI: () => (/* reexport */ objectHelper),
|
|
42
|
+
d: () => (/* reexport */ pReduce),
|
|
39
43
|
Lv: () => (/* reexport */ padZeros),
|
|
40
|
-
Qy: () => (/* reexport */ stringFormatter)
|
|
44
|
+
Qy: () => (/* reexport */ stringFormatter),
|
|
45
|
+
yO: () => (/* reexport */ stringHelper)
|
|
41
46
|
});
|
|
42
47
|
|
|
43
48
|
;// ./lib/helpers/convertString/convertString.js
|
|
@@ -264,39 +269,138 @@ function getValueByKeys(keys, data) {
|
|
|
264
269
|
;// ./lib/helpers/getValueByKeys/index.js
|
|
265
270
|
|
|
266
271
|
|
|
267
|
-
;// ./lib/helpers/
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
return
|
|
272
|
+
;// ./lib/helpers/objectHelper/objectHelper.js
|
|
273
|
+
const objectHelper = {
|
|
274
|
+
get(obj, path) {
|
|
275
|
+
const parts = path.split('.')
|
|
276
|
+
return parts.reduce((acc, part) => {
|
|
277
|
+
if (part.endsWith('[]')) {
|
|
278
|
+
// 处理数组遍历
|
|
279
|
+
const key = part.slice(0, -2) // 去掉 '[]' 得到属性名
|
|
280
|
+
if (Array.isArray(acc[key])) {
|
|
281
|
+
return acc[key] // 返回整个数组
|
|
282
|
+
}
|
|
283
|
+
return [] // 如果不是数组,返回空数组
|
|
284
|
+
}
|
|
285
|
+
if (part.includes('[') && part.includes(']')) {
|
|
286
|
+
// 处理数组索引
|
|
287
|
+
const arrayMatch = part.match(/(\w+)\[(\d+)\]/)
|
|
288
|
+
if (arrayMatch) {
|
|
289
|
+
const key = arrayMatch[1]
|
|
290
|
+
const index = arrayMatch[2]
|
|
291
|
+
return acc && acc[key] && acc[key][index]
|
|
292
|
+
}
|
|
293
|
+
} else if (acc && Array.isArray(acc)) {
|
|
294
|
+
// 如果当前值是数组,提取每个对象的指定属性
|
|
295
|
+
return acc.map((item) => item[part])
|
|
296
|
+
} else {
|
|
297
|
+
// 处理普通属性
|
|
298
|
+
return acc && acc[part]
|
|
299
|
+
}
|
|
300
|
+
}, obj)
|
|
301
|
+
},
|
|
302
|
+
merge,
|
|
303
|
+
set(obj, path, value) {
|
|
304
|
+
const parts = path.split('.')
|
|
305
|
+
let current = obj
|
|
306
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
307
|
+
const part = parts[i]
|
|
308
|
+
if (part.endsWith('[]')) {
|
|
309
|
+
// 处理数组遍历
|
|
310
|
+
const key = part.slice(0, -2) // 去掉 '[]' 得到属性名
|
|
311
|
+
if (Array.isArray(current[key])) {
|
|
312
|
+
current[key].forEach((item) => set(item, parts.slice(i + 1).join('.'), value))
|
|
313
|
+
}
|
|
314
|
+
return // 处理完数组后直接返回
|
|
315
|
+
}
|
|
316
|
+
if (part.includes('[') && part.includes(']')) {
|
|
317
|
+
// 处理数组索引
|
|
318
|
+
const arrayMatch = part.match(/(\w+)\[(\d+)\]/)
|
|
319
|
+
if (arrayMatch) {
|
|
320
|
+
const key = arrayMatch[1]
|
|
321
|
+
const index = arrayMatch[2]
|
|
322
|
+
if (Array.isArray(current[key]) && current[key][index]) {
|
|
323
|
+
current = current[key][index]
|
|
324
|
+
} else {
|
|
325
|
+
return // 如果数组或索引不存在,直接返回
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
} else {
|
|
329
|
+
// 处理普通属性
|
|
330
|
+
if (!current[part]) {
|
|
331
|
+
current[part] = {} // 如果属性不存在,创建一个空对象
|
|
332
|
+
}
|
|
333
|
+
current = current[part]
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// 设置最终属性值
|
|
338
|
+
const lastPart = parts[parts.length - 1]
|
|
339
|
+
current[lastPart] = value
|
|
272
340
|
}
|
|
273
|
-
return num
|
|
274
341
|
}
|
|
275
342
|
|
|
343
|
+
function merge(target, ...sources) {
|
|
344
|
+
if (!sources.length) return target
|
|
276
345
|
|
|
346
|
+
const source = sources.shift() // 取出第一个源对象
|
|
277
347
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
348
|
+
if (_isObject(target) && _isObject(source)) {
|
|
349
|
+
for (const key in source) {
|
|
350
|
+
if (_isObject(source[key])) {
|
|
351
|
+
if (!target[key]) {
|
|
352
|
+
// 如果目标对象没有该属性,创建一个空对象
|
|
353
|
+
target[key] = {}
|
|
354
|
+
}
|
|
355
|
+
// 递归合并
|
|
356
|
+
merge(target[key], source[key])
|
|
357
|
+
} else {
|
|
358
|
+
// 直接覆盖
|
|
359
|
+
target[key] = source[key]
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
281
363
|
|
|
364
|
+
// 继续合并剩余的源对象
|
|
365
|
+
return merge(target, ...sources)
|
|
366
|
+
}
|
|
282
367
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
return (str || '').toUpperCase().replace('-', '_').replace(' ', '_')
|
|
368
|
+
function _isObject(obj) {
|
|
369
|
+
return obj && typeof obj === 'object' && !Array.isArray(obj)
|
|
286
370
|
}
|
|
287
371
|
|
|
288
372
|
|
|
289
373
|
|
|
290
|
-
;// ./lib/helpers/
|
|
374
|
+
;// ./lib/helpers/objectHelper/index.js
|
|
291
375
|
|
|
292
376
|
|
|
293
377
|
|
|
294
378
|
|
|
295
|
-
;// ./lib/helpers/
|
|
379
|
+
;// ./lib/helpers/pReduce/pReduce.js
|
|
380
|
+
async function pReduce(iterable, reducer, initialValue) {
|
|
381
|
+
return new Promise((resolve, reject) => {
|
|
382
|
+
const iterator = iterable[Symbol.iterator]()
|
|
383
|
+
let index = 0
|
|
296
384
|
|
|
385
|
+
const next = async total => {
|
|
386
|
+
const element = iterator.next()
|
|
297
387
|
|
|
388
|
+
if (element.done) {
|
|
389
|
+
resolve(total)
|
|
390
|
+
return
|
|
391
|
+
}
|
|
298
392
|
|
|
393
|
+
try {
|
|
394
|
+
const [resolvedTotal, resolvedValue] = await Promise.all([total, element.value])
|
|
395
|
+
next(reducer(resolvedTotal, resolvedValue, index++))
|
|
396
|
+
} catch (error) {
|
|
397
|
+
reject(error)
|
|
398
|
+
}
|
|
399
|
+
}
|
|
299
400
|
|
|
401
|
+
next(initialValue)
|
|
402
|
+
})
|
|
403
|
+
}
|
|
300
404
|
|
|
301
405
|
|
|
302
406
|
|
|
@@ -390,107 +494,85 @@ class KeyValueObject {
|
|
|
390
494
|
}
|
|
391
495
|
|
|
392
496
|
static addItem(arr, key, value) {
|
|
393
|
-
arr.push(
|
|
394
|
-
{ key, value }
|
|
395
|
-
)
|
|
497
|
+
arr.push(this.init({ key, value }))
|
|
396
498
|
}
|
|
397
499
|
static addRecord(arr = [], key, value) {
|
|
398
|
-
const self = this
|
|
399
500
|
if (!this.hasKeyValue(arr, key, value)) {
|
|
400
|
-
arr.push(
|
|
501
|
+
arr.push(this.init({ key, value }))
|
|
401
502
|
}
|
|
402
503
|
return arr
|
|
403
504
|
}
|
|
404
|
-
|
|
405
505
|
static appendRecord(arr = [], key, value) {
|
|
406
506
|
return arr.map((item) => {
|
|
407
|
-
if (item
|
|
507
|
+
if (this.sameKey(item, key)) {
|
|
408
508
|
item.value = [...item.value, ...value]
|
|
409
509
|
}
|
|
410
510
|
return item
|
|
411
511
|
})
|
|
412
512
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
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)
|
|
513
|
+
static appendValueArray(arr = [], key, value) {
|
|
514
|
+
return arr.map((item) => {
|
|
515
|
+
if (this.sameKey(item, key)) {
|
|
516
|
+
item.value = [...item.value, ...value]
|
|
426
517
|
}
|
|
427
|
-
return
|
|
428
|
-
}
|
|
518
|
+
return item
|
|
519
|
+
})
|
|
429
520
|
}
|
|
430
|
-
|
|
431
521
|
static foundByKey(arr = [], key) {
|
|
432
522
|
const found = arr.find((m) => {
|
|
433
|
-
return m
|
|
523
|
+
return this.sameKey(m, key)
|
|
434
524
|
})
|
|
435
525
|
return found || null
|
|
436
526
|
}
|
|
437
|
-
|
|
438
527
|
static foundValueByKey(arr = [], key) {
|
|
439
528
|
const found = this.foundByKey(arr, key)
|
|
440
529
|
return found ? found.value : null
|
|
441
530
|
}
|
|
442
|
-
|
|
531
|
+
static fromObject(options = {}) {
|
|
532
|
+
return Object.keys(options).reduce((acc, key) => {
|
|
533
|
+
acc.push(this.init({ key, value: options[key] }))
|
|
534
|
+
return acc
|
|
535
|
+
}, [])
|
|
536
|
+
}
|
|
443
537
|
static getValueByKey(arr = [], key) {
|
|
444
|
-
|
|
445
|
-
return i.key === key
|
|
446
|
-
})
|
|
447
|
-
if (found) {
|
|
448
|
-
return found.value
|
|
449
|
-
}
|
|
450
|
-
return null
|
|
538
|
+
return this.foundValueByKey(arr, key)
|
|
451
539
|
}
|
|
452
|
-
|
|
453
540
|
static getValueByKeyFromArray(arr = [], key) {
|
|
454
541
|
if (arr.length === 0) {
|
|
455
542
|
return null
|
|
456
543
|
}
|
|
457
544
|
const firstArr = arr.shift()
|
|
458
545
|
const found = firstArr.find((i) => {
|
|
459
|
-
return i
|
|
546
|
+
return this.sameKey(i, key)
|
|
460
547
|
})
|
|
461
548
|
if (found && found.value) {
|
|
462
549
|
return found.value
|
|
463
550
|
}
|
|
464
551
|
return this.getValueByKeyFromArray(arr, key)
|
|
465
552
|
}
|
|
466
|
-
|
|
467
553
|
static getValuesByKey(arr = [], key) {
|
|
468
554
|
return arr.reduce((acc, item) => {
|
|
469
|
-
if (item
|
|
555
|
+
if (this.sameKey(item, key)) {
|
|
470
556
|
acc.push(item.value)
|
|
471
557
|
}
|
|
472
558
|
return acc
|
|
473
559
|
}, [])
|
|
474
560
|
}
|
|
475
|
-
|
|
476
561
|
static hasKeyValue(arr = [], key, value) {
|
|
477
562
|
if (typeof value === 'undefined') {
|
|
478
|
-
return arr.filter((item) => item
|
|
563
|
+
return arr.filter((item) => this.sameKey(item, key)).length > 0
|
|
479
564
|
}
|
|
480
|
-
return arr.filter((item) => (item
|
|
565
|
+
return arr.filter((item) => (this.sameKey(item, key) && item.value === value)).length > 0
|
|
481
566
|
}
|
|
482
|
-
|
|
483
567
|
static insertOrUpdateRecord(arr = [], key, value) {
|
|
484
|
-
const self = this
|
|
485
568
|
let copy = [...arr]
|
|
486
|
-
if (!
|
|
487
|
-
copy.push(
|
|
569
|
+
if (!this.hasKeyValue(arr, key)) {
|
|
570
|
+
copy.push(this.init({ key, value }))
|
|
488
571
|
} else {
|
|
489
|
-
copy =
|
|
572
|
+
copy = this.updateRecord(arr, key, value)
|
|
490
573
|
}
|
|
491
574
|
return copy
|
|
492
575
|
}
|
|
493
|
-
|
|
494
576
|
static keys(arr = []) {
|
|
495
577
|
if (Array.isArray(arr)) {
|
|
496
578
|
return arr.reduce((acc, item) => {
|
|
@@ -500,7 +582,6 @@ class KeyValueObject {
|
|
|
500
582
|
}
|
|
501
583
|
return []
|
|
502
584
|
}
|
|
503
|
-
|
|
504
585
|
static merge(toArr, fromArr) {
|
|
505
586
|
(fromArr || []).map((from) => {
|
|
506
587
|
const found = toArr.find((to) => {
|
|
@@ -514,7 +595,17 @@ class KeyValueObject {
|
|
|
514
595
|
})
|
|
515
596
|
return toArr
|
|
516
597
|
}
|
|
517
|
-
|
|
598
|
+
static removeByKey(arr, key) {
|
|
599
|
+
return arr.reduce((acc, item) => {
|
|
600
|
+
if (!this.sameKey(item, key)) {
|
|
601
|
+
acc.push(item)
|
|
602
|
+
}
|
|
603
|
+
return acc
|
|
604
|
+
}, [])
|
|
605
|
+
}
|
|
606
|
+
static sameKey(item, key) {
|
|
607
|
+
return item.key === key
|
|
608
|
+
}
|
|
518
609
|
static toObject(arr = []) {
|
|
519
610
|
if (Array.isArray(arr)) {
|
|
520
611
|
return arr.reduce((acc, item) => {
|
|
@@ -524,7 +615,6 @@ class KeyValueObject {
|
|
|
524
615
|
}
|
|
525
616
|
return {}
|
|
526
617
|
}
|
|
527
|
-
|
|
528
618
|
static toString(arr = [], delimiter = '; ') {
|
|
529
619
|
if (Array.isArray(arr)) {
|
|
530
620
|
return arr.reduce((acc, item) => {
|
|
@@ -534,10 +624,9 @@ class KeyValueObject {
|
|
|
534
624
|
}
|
|
535
625
|
return ''
|
|
536
626
|
}
|
|
537
|
-
|
|
538
627
|
static updateRecord(arr = [], key, value) {
|
|
539
628
|
return arr.map((item) => {
|
|
540
|
-
if (item
|
|
629
|
+
if (this.sameKey(item, key)) {
|
|
541
630
|
return {
|
|
542
631
|
...item,
|
|
543
632
|
value
|
|
@@ -546,11 +635,9 @@ class KeyValueObject {
|
|
|
546
635
|
return item
|
|
547
636
|
})
|
|
548
637
|
}
|
|
549
|
-
|
|
550
638
|
static updateOrInsertRecord(arr = [], key, value) {
|
|
551
639
|
return this.insertOrUpdateRecord(arr, key, value)
|
|
552
640
|
}
|
|
553
|
-
|
|
554
641
|
static updateRecordsFromArray(arr = [], updateArr = []) {
|
|
555
642
|
if (Array.isArray(arr) && Array.isArray(updateArr)) {
|
|
556
643
|
const obj1 = this.toObject(arr)
|
|
@@ -562,7 +649,6 @@ class KeyValueObject {
|
|
|
562
649
|
}
|
|
563
650
|
return []
|
|
564
651
|
}
|
|
565
|
-
|
|
566
652
|
static values(arr = []) {
|
|
567
653
|
if (Array.isArray(arr)) {
|
|
568
654
|
return arr.reduce((acc, item) => {
|
|
@@ -594,6 +680,13 @@ class KeyValueObject {
|
|
|
594
680
|
|
|
595
681
|
|
|
596
682
|
|
|
683
|
+
;// ./lib/helpers/stringFormatter/stringFormatter.js
|
|
684
|
+
function stringFormatter(str) {
|
|
685
|
+
return (str || '').toUpperCase().replace('-', '_').replace(' ', '_')
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+
|
|
597
690
|
;// ./lib/models/metadata/metadata.js
|
|
598
691
|
|
|
599
692
|
|
|
@@ -609,16 +702,25 @@ class Metadata extends KeyValueObject {
|
|
|
609
702
|
})
|
|
610
703
|
return instance.isValid ? instance : null
|
|
611
704
|
}
|
|
705
|
+
static get _classname() {
|
|
706
|
+
return 'Metadata'
|
|
707
|
+
}
|
|
612
708
|
|
|
613
|
-
static
|
|
614
|
-
|
|
615
|
-
|
|
709
|
+
static merge(toArr, fromArr) {
|
|
710
|
+
(fromArr || []).map((from) => {
|
|
711
|
+
const found = toArr.find((to) => {
|
|
712
|
+
return stringFormatter(to.key) === stringFormatter(from.key)
|
|
713
|
+
})
|
|
714
|
+
if (found) {
|
|
715
|
+
found.value = (found.value || []).concat(from.value)
|
|
716
|
+
} else {
|
|
717
|
+
toArr.push(from)
|
|
718
|
+
}
|
|
616
719
|
})
|
|
617
|
-
return
|
|
720
|
+
return toArr
|
|
618
721
|
}
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
return 'Metadata'
|
|
722
|
+
static sameKey(item, key) {
|
|
723
|
+
return stringFormatter(item.key) === stringFormatter(key)
|
|
622
724
|
}
|
|
623
725
|
}
|
|
624
726
|
|
|
@@ -765,68 +867,112 @@ class Repo {
|
|
|
765
867
|
}
|
|
766
868
|
}
|
|
767
869
|
|
|
768
|
-
|
|
870
|
+
// systemLog is optional
|
|
871
|
+
findAll({ query, systemLog }) {
|
|
872
|
+
const log = _makeLog({
|
|
873
|
+
systemLog,
|
|
874
|
+
label: 'REPO_READ',
|
|
875
|
+
message: `fn ${this._classname}.prototype.findAll`,
|
|
876
|
+
input: [{ query: { ...query }, systemLog: { ...systemLog } }]
|
|
877
|
+
})
|
|
769
878
|
return new Promise((resolve, reject) => {
|
|
770
879
|
this.model.findAll(query, this.queryOptions, (err, data, total) => {
|
|
771
880
|
if (err) {
|
|
881
|
+
log({ level: 'warn', output: err.toString() })
|
|
772
882
|
reject(err)
|
|
773
883
|
} else {
|
|
774
|
-
|
|
884
|
+
const result = {
|
|
775
885
|
isNew: false,
|
|
776
886
|
data,
|
|
777
887
|
total: total || data.length
|
|
778
|
-
}
|
|
888
|
+
}
|
|
889
|
+
log({ level: 'info', output: { ...result } })
|
|
890
|
+
resolve(result)
|
|
779
891
|
}
|
|
780
892
|
})
|
|
781
893
|
})
|
|
782
894
|
}
|
|
783
895
|
|
|
784
|
-
findOne({ query }) {
|
|
896
|
+
findOne({ query, systemLog }) {
|
|
897
|
+
const log = _makeLog({
|
|
898
|
+
systemLog,
|
|
899
|
+
label: 'REPO_READ',
|
|
900
|
+
message: `fn ${this._classname}.prototype.findOne`,
|
|
901
|
+
input: [{ query: { ...query }, systemLog: { ...systemLog } }]
|
|
902
|
+
})
|
|
785
903
|
return new Promise((resolve, reject) => {
|
|
786
904
|
this.model.findAll(query, this.queryOptions, (err, data) => {
|
|
787
905
|
if (err) {
|
|
788
906
|
reject(err)
|
|
789
907
|
} else if (data.length === 1) {
|
|
790
|
-
|
|
908
|
+
const result = {
|
|
791
909
|
isNew: false,
|
|
792
910
|
data,
|
|
793
911
|
total: 1
|
|
794
|
-
}
|
|
912
|
+
}
|
|
913
|
+
log({ level: 'info', output: { ...result } })
|
|
914
|
+
resolve(result)
|
|
795
915
|
} else if (data.length === 0) {
|
|
796
916
|
reject(new Error('record not found'))
|
|
797
917
|
} else {
|
|
798
918
|
reject(new Error('more than one is found'))
|
|
799
919
|
}
|
|
800
920
|
})
|
|
921
|
+
.catch((err) => {
|
|
922
|
+
log({ level: 'warn', output: err.toString() })
|
|
923
|
+
throw err
|
|
924
|
+
})
|
|
801
925
|
})
|
|
802
926
|
}
|
|
803
927
|
|
|
804
|
-
saveAll({ docs }) {
|
|
928
|
+
saveAll({ docs, systemLog }) {
|
|
805
929
|
let isNew
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
930
|
+
const log = _makeLog({
|
|
931
|
+
systemLog,
|
|
932
|
+
label: 'REPO_WRITE',
|
|
933
|
+
message: `fn ${this._classname}.prototype.saveAll`,
|
|
934
|
+
input: [{ docs: [...docs], systemLog: { ...systemLog } }]
|
|
935
|
+
})
|
|
936
|
+
const promise = typeof this.model.saveAll === 'function'
|
|
937
|
+
? this.model.saveAll({ docs })
|
|
938
|
+
: Promise.all(docs.map(async (doc) => {
|
|
939
|
+
if (doc) {
|
|
940
|
+
const result = await this.saveOne({ doc })
|
|
941
|
+
isNew = result.isNew
|
|
942
|
+
const _data = result._data || result.data
|
|
943
|
+
return _data[0]
|
|
944
|
+
}
|
|
945
|
+
return null
|
|
946
|
+
}))
|
|
947
|
+
return promise.then((savedData) => {
|
|
815
948
|
if (savedData.length !== 1) isNew = null
|
|
816
|
-
|
|
949
|
+
const result = {
|
|
817
950
|
data: savedData,
|
|
818
951
|
isNew,
|
|
819
952
|
total: savedData.length
|
|
820
953
|
}
|
|
954
|
+
log({ level: 'info', output: { ...result } })
|
|
955
|
+
return result
|
|
956
|
+
}).catch((err) => {
|
|
957
|
+
log({ level: 'warn', output: err.toString() })
|
|
958
|
+
throw err
|
|
821
959
|
})
|
|
822
960
|
}
|
|
823
961
|
|
|
824
|
-
saveOne({ doc }) {
|
|
962
|
+
saveOne({ doc, systemLog }) {
|
|
963
|
+
const log = _makeLog({
|
|
964
|
+
systemLog,
|
|
965
|
+
label: 'REPO_WRITE',
|
|
966
|
+
message: `fn ${this._classname}.prototype.saveOne`,
|
|
967
|
+
input: [{ doc: { ...doc }, systemLog: { ...systemLog } }]
|
|
968
|
+
})
|
|
825
969
|
return new Promise((resolve, reject) => {
|
|
826
970
|
this.model.saveOne(doc, this.saveOptions, (err, result) => {
|
|
827
971
|
if (err) {
|
|
972
|
+
log({ level: 'warn', output: err.toString() })
|
|
828
973
|
reject(err)
|
|
829
974
|
} else {
|
|
975
|
+
log({ level: 'info', output: { ...result } })
|
|
830
976
|
resolve(result)
|
|
831
977
|
}
|
|
832
978
|
})
|
|
@@ -834,6 +980,25 @@ class Repo {
|
|
|
834
980
|
}
|
|
835
981
|
}
|
|
836
982
|
|
|
983
|
+
function _makeLog({ systemLog, label, message: message1, input } = {}) {
|
|
984
|
+
return ({ level, messgae: massage2, output } = {}) => {
|
|
985
|
+
if (systemLog && systemLog.systemLogHelper) {
|
|
986
|
+
systemLog.systemLogHelper.log({
|
|
987
|
+
batchId: systemLog.batchId,
|
|
988
|
+
label,
|
|
989
|
+
level,
|
|
990
|
+
message: massage2 || message1,
|
|
991
|
+
data: {
|
|
992
|
+
payload: {
|
|
993
|
+
input,
|
|
994
|
+
output
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
})
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
|
|
837
1002
|
|
|
838
1003
|
|
|
839
1004
|
;// ./lib/models/repo/index.js
|
|
@@ -864,16 +1029,16 @@ class Service {
|
|
|
864
1029
|
})
|
|
865
1030
|
}
|
|
866
1031
|
|
|
867
|
-
async findAll({ query = {} } = {}) {
|
|
868
|
-
const result = await this.repo.findAll({ query })
|
|
1032
|
+
async findAll({ query = {}, systemLog } = {}) {
|
|
1033
|
+
const result = await this.repo.findAll({ query, systemLog })
|
|
869
1034
|
return makeApiResponse({
|
|
870
1035
|
repo: this.repo,
|
|
871
1036
|
result
|
|
872
1037
|
})
|
|
873
1038
|
}
|
|
874
1039
|
|
|
875
|
-
async findOne({ query = {} } = {}) {
|
|
876
|
-
const result = await this.repo.findOne({ query })
|
|
1040
|
+
async findOne({ query = {}, systemLog } = {}) {
|
|
1041
|
+
const result = await this.repo.findOne({ query, systemLog })
|
|
877
1042
|
return makeApiResponse({
|
|
878
1043
|
repo: this.repo,
|
|
879
1044
|
result
|
|
@@ -887,21 +1052,21 @@ class Service {
|
|
|
887
1052
|
return arr.map((i) => this.init(i))
|
|
888
1053
|
}
|
|
889
1054
|
|
|
890
|
-
async saveAll({ docs = [] } = {}) {
|
|
1055
|
+
async saveAll({ docs = [], systemLog } = {}) {
|
|
891
1056
|
const copies = docs.map((doc) => {
|
|
892
1057
|
return this.init(doc)
|
|
893
1058
|
})
|
|
894
|
-
const result = await this.repo.saveAll({ docs: copies })
|
|
1059
|
+
const result = await this.repo.saveAll({ docs: copies, systemLog })
|
|
895
1060
|
return makeApiResponse({
|
|
896
1061
|
repo: this.repo,
|
|
897
1062
|
result
|
|
898
1063
|
})
|
|
899
1064
|
}
|
|
900
1065
|
|
|
901
|
-
async saveOne({ doc = {} } = {}) {
|
|
1066
|
+
async saveOne({ doc = {}, systemLog } = {}) {
|
|
902
1067
|
const copy = this.init(doc)
|
|
903
1068
|
if (copy) {
|
|
904
|
-
const result = await this.repo.saveOne({ doc: copy })
|
|
1069
|
+
const result = await this.repo.saveOne({ doc: copy, systemLog })
|
|
905
1070
|
return makeApiResponse({
|
|
906
1071
|
repo: this.repo,
|
|
907
1072
|
result
|
|
@@ -932,6 +1097,54 @@ function makeService({ repo }) {
|
|
|
932
1097
|
|
|
933
1098
|
|
|
934
1099
|
|
|
1100
|
+
;// ./lib/models/uniqueKeyGenerator/uniqueKeyGenerator.js
|
|
1101
|
+
|
|
1102
|
+
|
|
1103
|
+
class UniqueKeyGenerator {
|
|
1104
|
+
static get _classname() {
|
|
1105
|
+
return 'UniqueKeyGenerator'
|
|
1106
|
+
}
|
|
1107
|
+
static get _superclass() {
|
|
1108
|
+
return 'UniqueKeyGenerator'
|
|
1109
|
+
}
|
|
1110
|
+
static makeFormatter({ fieldName, format, options }) {
|
|
1111
|
+
switch (format) {
|
|
1112
|
+
case 'set_code':
|
|
1113
|
+
return _makeSetCode(fieldName, options)
|
|
1114
|
+
default:
|
|
1115
|
+
return _makeSetCode(fieldName, options)
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
static makeGenerator(arr) {
|
|
1119
|
+
const fns = arr.map((item) => this.makeFormatter(item))
|
|
1120
|
+
return async (obj) => {
|
|
1121
|
+
const output = await pReduce(fns, async (acc, fn) => {
|
|
1122
|
+
const _obj = await fn(obj)
|
|
1123
|
+
return Object.assign(acc, _obj)
|
|
1124
|
+
}, obj)
|
|
1125
|
+
return output
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
function _makeSetCode(fieldName, options) {
|
|
1131
|
+
return async (obj = {}) => {
|
|
1132
|
+
if (obj[fieldName]) {
|
|
1133
|
+
return {}
|
|
1134
|
+
}
|
|
1135
|
+
return {
|
|
1136
|
+
[fieldName]: stringHelper.setCode()
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
|
|
1142
|
+
|
|
1143
|
+
;// ./lib/models/uniqueKeyGenerator/index.js
|
|
1144
|
+
|
|
1145
|
+
|
|
1146
|
+
|
|
1147
|
+
|
|
935
1148
|
;// ./lib/models/index.js
|
|
936
1149
|
|
|
937
1150
|
|
|
@@ -940,6 +1153,193 @@ function makeService({ repo }) {
|
|
|
940
1153
|
|
|
941
1154
|
|
|
942
1155
|
|
|
1156
|
+
|
|
1157
|
+
;// ./lib/helpers/generalPost/generalPost.js
|
|
1158
|
+
|
|
1159
|
+
|
|
1160
|
+
|
|
1161
|
+
|
|
1162
|
+
async function generalPost({ body = {}, GeneralModel, UniqueKeyGenerator, resourceInfo }) {
|
|
1163
|
+
const { resources, data, globalShared = {}, shared = {}, relationship = {} } = body
|
|
1164
|
+
const _resourceInfo = resourceInfo || body.resourceInfo
|
|
1165
|
+
_attachShared(data, globalShared, shared)
|
|
1166
|
+
const obj = await pReduce(resources, async (acc, resource) => {
|
|
1167
|
+
const service = _makeService(resource, _resourceInfo, UniqueKeyGenerator, GeneralModel)
|
|
1168
|
+
_createRelationship(data, relationship[resource], acc)
|
|
1169
|
+
const _data = data[resource]
|
|
1170
|
+
const result = await service.saveAll({ docs: [].concat(_data) })
|
|
1171
|
+
acc[resource] = Array.isArray(_data) ? result._data : result._data[0]
|
|
1172
|
+
return acc
|
|
1173
|
+
}, {})
|
|
1174
|
+
return obj
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
function _attachShared(data, globalShared = {}, shared = {}) {
|
|
1178
|
+
Object.keys(shared).forEach((key) => {
|
|
1179
|
+
const _data = data[key]
|
|
1180
|
+
data[key] = objectHelper.merge({}, _data, globalShared, shared[key] || {})
|
|
1181
|
+
})
|
|
1182
|
+
}
|
|
1183
|
+
|
|
1184
|
+
function _createRelationship(data, relationship = {}, object) {
|
|
1185
|
+
Object.keys(relationship).forEach((key) => {
|
|
1186
|
+
const path = relationship[key]
|
|
1187
|
+
const val = objectHelper.get(object, path)
|
|
1188
|
+
objectHelper.set(data, key, val)
|
|
1189
|
+
})
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
function _makeService(resource, resourceInfo, UniqueKeyGenerator, GeneralModel) {
|
|
1193
|
+
const { collectionName, fields } = resourceInfo[resource]
|
|
1194
|
+
const uniqueKeyGenerator = UniqueKeyGenerator.makeGenerator(fields)
|
|
1195
|
+
const model = new GeneralModel({ collectionName, uniqueKeyGenerator })
|
|
1196
|
+
return makeService({
|
|
1197
|
+
repo: new Repo({ model })
|
|
1198
|
+
})
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
|
|
1202
|
+
|
|
1203
|
+
;// ./lib/helpers/generalPost/index.js
|
|
1204
|
+
|
|
1205
|
+
|
|
1206
|
+
|
|
1207
|
+
|
|
1208
|
+
;// ./lib/helpers/padZeros/padZeros.js
|
|
1209
|
+
function padZeros(num, minLength = 6) {
|
|
1210
|
+
num = num.toString()
|
|
1211
|
+
if (num.length < minLength) {
|
|
1212
|
+
return padZeros('0' + num, minLength)
|
|
1213
|
+
}
|
|
1214
|
+
return num
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
|
|
1218
|
+
|
|
1219
|
+
;// ./lib/helpers/padZeros/index.js
|
|
1220
|
+
|
|
1221
|
+
|
|
1222
|
+
|
|
1223
|
+
|
|
1224
|
+
;// ./lib/helpers/pReduce/index.js
|
|
1225
|
+
|
|
1226
|
+
|
|
1227
|
+
|
|
1228
|
+
|
|
1229
|
+
;// ./lib/helpers/stringFormatter/index.js
|
|
1230
|
+
|
|
1231
|
+
|
|
1232
|
+
|
|
1233
|
+
|
|
1234
|
+
;// ./lib/helpers/stringHelper/stringHelper.js
|
|
1235
|
+
function baseXEncode(num, base = 34) {
|
|
1236
|
+
const charset = getBaseCharset(base)
|
|
1237
|
+
return encode(num, charset)
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1240
|
+
function encode(int, charset) {
|
|
1241
|
+
let byCode = charset.byCode;
|
|
1242
|
+
if (int === 0) {
|
|
1243
|
+
return byCode[0];
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
var res = "",
|
|
1247
|
+
max = charset.length;
|
|
1248
|
+
while (int > 0) {
|
|
1249
|
+
res = byCode[int % max] + res;
|
|
1250
|
+
int = Math.floor(int / max);
|
|
1251
|
+
}
|
|
1252
|
+
return res;
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
function getBaseCharset(base) {
|
|
1256
|
+
let charset = '9876543210ABCDEFGHJKLMNPQRSTUVWXYZ'
|
|
1257
|
+
if (base === 58) {
|
|
1258
|
+
charset = '9876543210ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz'
|
|
1259
|
+
}
|
|
1260
|
+
return indexCharset(charset)
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
function indexCharset(str) {
|
|
1264
|
+
var byCode = {},
|
|
1265
|
+
byChar = {},
|
|
1266
|
+
length = str.length,
|
|
1267
|
+
i, char;
|
|
1268
|
+
for (i = 0; i < length; i++) {
|
|
1269
|
+
char = str[i];
|
|
1270
|
+
byCode[i] = char;
|
|
1271
|
+
byChar[char] = i;
|
|
1272
|
+
}
|
|
1273
|
+
return { byCode: byCode, byChar: byChar, length: length };
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
function randomString({ len = 16, pattern = 'a1' } = {}) {
|
|
1277
|
+
const A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
1278
|
+
const a = 'abcdefghijklmnopqrstuvwxyz'
|
|
1279
|
+
const num = '1234567890'
|
|
1280
|
+
const mark = '~!@#$%^&*_+-='
|
|
1281
|
+
let str = ''
|
|
1282
|
+
if (pattern.includes('A')) {
|
|
1283
|
+
str += A
|
|
1284
|
+
}
|
|
1285
|
+
if (pattern.includes('a')) {
|
|
1286
|
+
str += a
|
|
1287
|
+
}
|
|
1288
|
+
if (pattern.includes('1')) {
|
|
1289
|
+
str += num
|
|
1290
|
+
}
|
|
1291
|
+
if (pattern.includes('#')) {
|
|
1292
|
+
str += mark
|
|
1293
|
+
}
|
|
1294
|
+
const chars = [...str]
|
|
1295
|
+
return [...Array(len)].map(i => {
|
|
1296
|
+
return chars[(Math.random() * chars.length) | 0]
|
|
1297
|
+
}).join``
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
function reverse(str) {
|
|
1301
|
+
if (typeof str !== 'string') {
|
|
1302
|
+
str = str.toString()
|
|
1303
|
+
}
|
|
1304
|
+
const splitString = str.split('')
|
|
1305
|
+
const reverseArray = splitString.reverse()
|
|
1306
|
+
return reverseArray.join('')
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
function setCode(base = 34) {
|
|
1310
|
+
const now = (new Date()).valueOf()
|
|
1311
|
+
const random = randomString({
|
|
1312
|
+
len: 8,
|
|
1313
|
+
pattern: '1'
|
|
1314
|
+
})
|
|
1315
|
+
const str = reverse(`${now}${random}`)
|
|
1316
|
+
// const str = `${now}${random}`
|
|
1317
|
+
return baseXEncode(str, base)
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
const stringHelper = {
|
|
1321
|
+
setCode
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
|
|
1325
|
+
;// ./lib/helpers/stringHelper/index.js
|
|
1326
|
+
|
|
1327
|
+
|
|
1328
|
+
|
|
1329
|
+
|
|
1330
|
+
|
|
1331
|
+
;// ./lib/helpers/index.js
|
|
1332
|
+
|
|
1333
|
+
|
|
1334
|
+
|
|
1335
|
+
|
|
1336
|
+
|
|
1337
|
+
|
|
1338
|
+
|
|
1339
|
+
|
|
1340
|
+
|
|
1341
|
+
|
|
1342
|
+
|
|
943
1343
|
;// ./lib/index.js
|
|
944
1344
|
|
|
945
1345
|
|
|
@@ -953,12 +1353,17 @@ var __webpack_exports__Metadata = __webpack_exports__.OS;
|
|
|
953
1353
|
var __webpack_exports__QMeta = __webpack_exports__.Z8;
|
|
954
1354
|
var __webpack_exports__Repo = __webpack_exports__.lc;
|
|
955
1355
|
var __webpack_exports__Service = __webpack_exports__.kl;
|
|
1356
|
+
var __webpack_exports__UniqueKeyGenerator = __webpack_exports__._x;
|
|
956
1357
|
var __webpack_exports__convertString = __webpack_exports__.l0;
|
|
957
1358
|
var __webpack_exports__formatDate = __webpack_exports__.Yq;
|
|
1359
|
+
var __webpack_exports__generalPost = __webpack_exports__.zn;
|
|
958
1360
|
var __webpack_exports__getValidation = __webpack_exports__.G8;
|
|
959
1361
|
var __webpack_exports__getValueByKeys = __webpack_exports__.pY;
|
|
960
1362
|
var __webpack_exports__makeApiResponse = __webpack_exports__.su;
|
|
961
1363
|
var __webpack_exports__makeService = __webpack_exports__.Q6;
|
|
1364
|
+
var __webpack_exports__objectHelper = __webpack_exports__.UI;
|
|
1365
|
+
var __webpack_exports__pReduce = __webpack_exports__.d;
|
|
962
1366
|
var __webpack_exports__padZeros = __webpack_exports__.Lv;
|
|
963
1367
|
var __webpack_exports__stringFormatter = __webpack_exports__.Qy;
|
|
964
|
-
|
|
1368
|
+
var __webpack_exports__stringHelper = __webpack_exports__.yO;
|
|
1369
|
+
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__UniqueKeyGenerator as UniqueKeyGenerator, __webpack_exports__convertString as convertString, __webpack_exports__formatDate as formatDate, __webpack_exports__generalPost as generalPost, __webpack_exports__getValidation as getValidation, __webpack_exports__getValueByKeys as getValueByKeys, __webpack_exports__makeApiResponse as makeApiResponse, __webpack_exports__makeService as makeService, __webpack_exports__objectHelper as objectHelper, __webpack_exports__pReduce as pReduce, __webpack_exports__padZeros as padZeros, __webpack_exports__stringFormatter as stringFormatter, __webpack_exports__stringHelper as stringHelper };
|