@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.cjs
CHANGED
|
@@ -55,14 +55,19 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
55
55
|
QMeta: () => (/* reexport */ QMeta),
|
|
56
56
|
Repo: () => (/* reexport */ Repo),
|
|
57
57
|
Service: () => (/* reexport */ Service),
|
|
58
|
+
UniqueKeyGenerator: () => (/* reexport */ UniqueKeyGenerator),
|
|
58
59
|
convertString: () => (/* reexport */ convertString),
|
|
59
60
|
formatDate: () => (/* reexport */ formatDate),
|
|
61
|
+
generalPost: () => (/* reexport */ generalPost),
|
|
60
62
|
getValidation: () => (/* reexport */ getValidation),
|
|
61
63
|
getValueByKeys: () => (/* reexport */ getValueByKeys),
|
|
62
64
|
makeApiResponse: () => (/* reexport */ makeApiResponse),
|
|
63
65
|
makeService: () => (/* reexport */ makeService),
|
|
66
|
+
objectHelper: () => (/* reexport */ objectHelper),
|
|
67
|
+
pReduce: () => (/* reexport */ pReduce),
|
|
64
68
|
padZeros: () => (/* reexport */ padZeros),
|
|
65
|
-
stringFormatter: () => (/* reexport */ stringFormatter)
|
|
69
|
+
stringFormatter: () => (/* reexport */ stringFormatter),
|
|
70
|
+
stringHelper: () => (/* reexport */ stringHelper)
|
|
66
71
|
});
|
|
67
72
|
|
|
68
73
|
;// ./lib/helpers/convertString/convertString.js
|
|
@@ -289,39 +294,138 @@ function getValueByKeys(keys, data) {
|
|
|
289
294
|
;// ./lib/helpers/getValueByKeys/index.js
|
|
290
295
|
|
|
291
296
|
|
|
292
|
-
;// ./lib/helpers/
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
return
|
|
297
|
+
;// ./lib/helpers/objectHelper/objectHelper.js
|
|
298
|
+
const objectHelper = {
|
|
299
|
+
get(obj, path) {
|
|
300
|
+
const parts = path.split('.')
|
|
301
|
+
return parts.reduce((acc, part) => {
|
|
302
|
+
if (part.endsWith('[]')) {
|
|
303
|
+
// 处理数组遍历
|
|
304
|
+
const key = part.slice(0, -2) // 去掉 '[]' 得到属性名
|
|
305
|
+
if (Array.isArray(acc[key])) {
|
|
306
|
+
return acc[key] // 返回整个数组
|
|
307
|
+
}
|
|
308
|
+
return [] // 如果不是数组,返回空数组
|
|
309
|
+
}
|
|
310
|
+
if (part.includes('[') && part.includes(']')) {
|
|
311
|
+
// 处理数组索引
|
|
312
|
+
const arrayMatch = part.match(/(\w+)\[(\d+)\]/)
|
|
313
|
+
if (arrayMatch) {
|
|
314
|
+
const key = arrayMatch[1]
|
|
315
|
+
const index = arrayMatch[2]
|
|
316
|
+
return acc && acc[key] && acc[key][index]
|
|
317
|
+
}
|
|
318
|
+
} else if (acc && Array.isArray(acc)) {
|
|
319
|
+
// 如果当前值是数组,提取每个对象的指定属性
|
|
320
|
+
return acc.map((item) => item[part])
|
|
321
|
+
} else {
|
|
322
|
+
// 处理普通属性
|
|
323
|
+
return acc && acc[part]
|
|
324
|
+
}
|
|
325
|
+
}, obj)
|
|
326
|
+
},
|
|
327
|
+
merge,
|
|
328
|
+
set(obj, path, value) {
|
|
329
|
+
const parts = path.split('.')
|
|
330
|
+
let current = obj
|
|
331
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
332
|
+
const part = parts[i]
|
|
333
|
+
if (part.endsWith('[]')) {
|
|
334
|
+
// 处理数组遍历
|
|
335
|
+
const key = part.slice(0, -2) // 去掉 '[]' 得到属性名
|
|
336
|
+
if (Array.isArray(current[key])) {
|
|
337
|
+
current[key].forEach((item) => set(item, parts.slice(i + 1).join('.'), value))
|
|
338
|
+
}
|
|
339
|
+
return // 处理完数组后直接返回
|
|
340
|
+
}
|
|
341
|
+
if (part.includes('[') && part.includes(']')) {
|
|
342
|
+
// 处理数组索引
|
|
343
|
+
const arrayMatch = part.match(/(\w+)\[(\d+)\]/)
|
|
344
|
+
if (arrayMatch) {
|
|
345
|
+
const key = arrayMatch[1]
|
|
346
|
+
const index = arrayMatch[2]
|
|
347
|
+
if (Array.isArray(current[key]) && current[key][index]) {
|
|
348
|
+
current = current[key][index]
|
|
349
|
+
} else {
|
|
350
|
+
return // 如果数组或索引不存在,直接返回
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
} else {
|
|
354
|
+
// 处理普通属性
|
|
355
|
+
if (!current[part]) {
|
|
356
|
+
current[part] = {} // 如果属性不存在,创建一个空对象
|
|
357
|
+
}
|
|
358
|
+
current = current[part]
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// 设置最终属性值
|
|
363
|
+
const lastPart = parts[parts.length - 1]
|
|
364
|
+
current[lastPart] = value
|
|
297
365
|
}
|
|
298
|
-
return num
|
|
299
366
|
}
|
|
300
367
|
|
|
368
|
+
function merge(target, ...sources) {
|
|
369
|
+
if (!sources.length) return target
|
|
301
370
|
|
|
371
|
+
const source = sources.shift() // 取出第一个源对象
|
|
302
372
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
373
|
+
if (_isObject(target) && _isObject(source)) {
|
|
374
|
+
for (const key in source) {
|
|
375
|
+
if (_isObject(source[key])) {
|
|
376
|
+
if (!target[key]) {
|
|
377
|
+
// 如果目标对象没有该属性,创建一个空对象
|
|
378
|
+
target[key] = {}
|
|
379
|
+
}
|
|
380
|
+
// 递归合并
|
|
381
|
+
merge(target[key], source[key])
|
|
382
|
+
} else {
|
|
383
|
+
// 直接覆盖
|
|
384
|
+
target[key] = source[key]
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
306
388
|
|
|
389
|
+
// 继续合并剩余的源对象
|
|
390
|
+
return merge(target, ...sources)
|
|
391
|
+
}
|
|
307
392
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
return (str || '').toUpperCase().replace('-', '_').replace(' ', '_')
|
|
393
|
+
function _isObject(obj) {
|
|
394
|
+
return obj && typeof obj === 'object' && !Array.isArray(obj)
|
|
311
395
|
}
|
|
312
396
|
|
|
313
397
|
|
|
314
398
|
|
|
315
|
-
;// ./lib/helpers/
|
|
399
|
+
;// ./lib/helpers/objectHelper/index.js
|
|
316
400
|
|
|
317
401
|
|
|
318
402
|
|
|
319
403
|
|
|
320
|
-
;// ./lib/helpers/
|
|
404
|
+
;// ./lib/helpers/pReduce/pReduce.js
|
|
405
|
+
async function pReduce(iterable, reducer, initialValue) {
|
|
406
|
+
return new Promise((resolve, reject) => {
|
|
407
|
+
const iterator = iterable[Symbol.iterator]()
|
|
408
|
+
let index = 0
|
|
321
409
|
|
|
410
|
+
const next = async total => {
|
|
411
|
+
const element = iterator.next()
|
|
322
412
|
|
|
413
|
+
if (element.done) {
|
|
414
|
+
resolve(total)
|
|
415
|
+
return
|
|
416
|
+
}
|
|
323
417
|
|
|
418
|
+
try {
|
|
419
|
+
const [resolvedTotal, resolvedValue] = await Promise.all([total, element.value])
|
|
420
|
+
next(reducer(resolvedTotal, resolvedValue, index++))
|
|
421
|
+
} catch (error) {
|
|
422
|
+
reject(error)
|
|
423
|
+
}
|
|
424
|
+
}
|
|
324
425
|
|
|
426
|
+
next(initialValue)
|
|
427
|
+
})
|
|
428
|
+
}
|
|
325
429
|
|
|
326
430
|
|
|
327
431
|
|
|
@@ -415,107 +519,85 @@ class KeyValueObject {
|
|
|
415
519
|
}
|
|
416
520
|
|
|
417
521
|
static addItem(arr, key, value) {
|
|
418
|
-
arr.push(
|
|
419
|
-
{ key, value }
|
|
420
|
-
)
|
|
522
|
+
arr.push(this.init({ key, value }))
|
|
421
523
|
}
|
|
422
524
|
static addRecord(arr = [], key, value) {
|
|
423
|
-
const self = this
|
|
424
525
|
if (!this.hasKeyValue(arr, key, value)) {
|
|
425
|
-
arr.push(
|
|
526
|
+
arr.push(this.init({ key, value }))
|
|
426
527
|
}
|
|
427
528
|
return arr
|
|
428
529
|
}
|
|
429
|
-
|
|
430
530
|
static appendRecord(arr = [], key, value) {
|
|
431
531
|
return arr.map((item) => {
|
|
432
|
-
if (item
|
|
532
|
+
if (this.sameKey(item, key)) {
|
|
433
533
|
item.value = [...item.value, ...value]
|
|
434
534
|
}
|
|
435
535
|
return item
|
|
436
536
|
})
|
|
437
537
|
}
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
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)
|
|
538
|
+
static appendValueArray(arr = [], key, value) {
|
|
539
|
+
return arr.map((item) => {
|
|
540
|
+
if (this.sameKey(item, key)) {
|
|
541
|
+
item.value = [...item.value, ...value]
|
|
451
542
|
}
|
|
452
|
-
return
|
|
453
|
-
}
|
|
543
|
+
return item
|
|
544
|
+
})
|
|
454
545
|
}
|
|
455
|
-
|
|
456
546
|
static foundByKey(arr = [], key) {
|
|
457
547
|
const found = arr.find((m) => {
|
|
458
|
-
return m
|
|
548
|
+
return this.sameKey(m, key)
|
|
459
549
|
})
|
|
460
550
|
return found || null
|
|
461
551
|
}
|
|
462
|
-
|
|
463
552
|
static foundValueByKey(arr = [], key) {
|
|
464
553
|
const found = this.foundByKey(arr, key)
|
|
465
554
|
return found ? found.value : null
|
|
466
555
|
}
|
|
467
|
-
|
|
556
|
+
static fromObject(options = {}) {
|
|
557
|
+
return Object.keys(options).reduce((acc, key) => {
|
|
558
|
+
acc.push(this.init({ key, value: options[key] }))
|
|
559
|
+
return acc
|
|
560
|
+
}, [])
|
|
561
|
+
}
|
|
468
562
|
static getValueByKey(arr = [], key) {
|
|
469
|
-
|
|
470
|
-
return i.key === key
|
|
471
|
-
})
|
|
472
|
-
if (found) {
|
|
473
|
-
return found.value
|
|
474
|
-
}
|
|
475
|
-
return null
|
|
563
|
+
return this.foundValueByKey(arr, key)
|
|
476
564
|
}
|
|
477
|
-
|
|
478
565
|
static getValueByKeyFromArray(arr = [], key) {
|
|
479
566
|
if (arr.length === 0) {
|
|
480
567
|
return null
|
|
481
568
|
}
|
|
482
569
|
const firstArr = arr.shift()
|
|
483
570
|
const found = firstArr.find((i) => {
|
|
484
|
-
return i
|
|
571
|
+
return this.sameKey(i, key)
|
|
485
572
|
})
|
|
486
573
|
if (found && found.value) {
|
|
487
574
|
return found.value
|
|
488
575
|
}
|
|
489
576
|
return this.getValueByKeyFromArray(arr, key)
|
|
490
577
|
}
|
|
491
|
-
|
|
492
578
|
static getValuesByKey(arr = [], key) {
|
|
493
579
|
return arr.reduce((acc, item) => {
|
|
494
|
-
if (item
|
|
580
|
+
if (this.sameKey(item, key)) {
|
|
495
581
|
acc.push(item.value)
|
|
496
582
|
}
|
|
497
583
|
return acc
|
|
498
584
|
}, [])
|
|
499
585
|
}
|
|
500
|
-
|
|
501
586
|
static hasKeyValue(arr = [], key, value) {
|
|
502
587
|
if (typeof value === 'undefined') {
|
|
503
|
-
return arr.filter((item) => item
|
|
588
|
+
return arr.filter((item) => this.sameKey(item, key)).length > 0
|
|
504
589
|
}
|
|
505
|
-
return arr.filter((item) => (item
|
|
590
|
+
return arr.filter((item) => (this.sameKey(item, key) && item.value === value)).length > 0
|
|
506
591
|
}
|
|
507
|
-
|
|
508
592
|
static insertOrUpdateRecord(arr = [], key, value) {
|
|
509
|
-
const self = this
|
|
510
593
|
let copy = [...arr]
|
|
511
|
-
if (!
|
|
512
|
-
copy.push(
|
|
594
|
+
if (!this.hasKeyValue(arr, key)) {
|
|
595
|
+
copy.push(this.init({ key, value }))
|
|
513
596
|
} else {
|
|
514
|
-
copy =
|
|
597
|
+
copy = this.updateRecord(arr, key, value)
|
|
515
598
|
}
|
|
516
599
|
return copy
|
|
517
600
|
}
|
|
518
|
-
|
|
519
601
|
static keys(arr = []) {
|
|
520
602
|
if (Array.isArray(arr)) {
|
|
521
603
|
return arr.reduce((acc, item) => {
|
|
@@ -525,7 +607,6 @@ class KeyValueObject {
|
|
|
525
607
|
}
|
|
526
608
|
return []
|
|
527
609
|
}
|
|
528
|
-
|
|
529
610
|
static merge(toArr, fromArr) {
|
|
530
611
|
(fromArr || []).map((from) => {
|
|
531
612
|
const found = toArr.find((to) => {
|
|
@@ -539,7 +620,17 @@ class KeyValueObject {
|
|
|
539
620
|
})
|
|
540
621
|
return toArr
|
|
541
622
|
}
|
|
542
|
-
|
|
623
|
+
static removeByKey(arr, key) {
|
|
624
|
+
return arr.reduce((acc, item) => {
|
|
625
|
+
if (!this.sameKey(item, key)) {
|
|
626
|
+
acc.push(item)
|
|
627
|
+
}
|
|
628
|
+
return acc
|
|
629
|
+
}, [])
|
|
630
|
+
}
|
|
631
|
+
static sameKey(item, key) {
|
|
632
|
+
return item.key === key
|
|
633
|
+
}
|
|
543
634
|
static toObject(arr = []) {
|
|
544
635
|
if (Array.isArray(arr)) {
|
|
545
636
|
return arr.reduce((acc, item) => {
|
|
@@ -549,7 +640,6 @@ class KeyValueObject {
|
|
|
549
640
|
}
|
|
550
641
|
return {}
|
|
551
642
|
}
|
|
552
|
-
|
|
553
643
|
static toString(arr = [], delimiter = '; ') {
|
|
554
644
|
if (Array.isArray(arr)) {
|
|
555
645
|
return arr.reduce((acc, item) => {
|
|
@@ -559,10 +649,9 @@ class KeyValueObject {
|
|
|
559
649
|
}
|
|
560
650
|
return ''
|
|
561
651
|
}
|
|
562
|
-
|
|
563
652
|
static updateRecord(arr = [], key, value) {
|
|
564
653
|
return arr.map((item) => {
|
|
565
|
-
if (item
|
|
654
|
+
if (this.sameKey(item, key)) {
|
|
566
655
|
return {
|
|
567
656
|
...item,
|
|
568
657
|
value
|
|
@@ -571,11 +660,9 @@ class KeyValueObject {
|
|
|
571
660
|
return item
|
|
572
661
|
})
|
|
573
662
|
}
|
|
574
|
-
|
|
575
663
|
static updateOrInsertRecord(arr = [], key, value) {
|
|
576
664
|
return this.insertOrUpdateRecord(arr, key, value)
|
|
577
665
|
}
|
|
578
|
-
|
|
579
666
|
static updateRecordsFromArray(arr = [], updateArr = []) {
|
|
580
667
|
if (Array.isArray(arr) && Array.isArray(updateArr)) {
|
|
581
668
|
const obj1 = this.toObject(arr)
|
|
@@ -587,7 +674,6 @@ class KeyValueObject {
|
|
|
587
674
|
}
|
|
588
675
|
return []
|
|
589
676
|
}
|
|
590
|
-
|
|
591
677
|
static values(arr = []) {
|
|
592
678
|
if (Array.isArray(arr)) {
|
|
593
679
|
return arr.reduce((acc, item) => {
|
|
@@ -619,6 +705,13 @@ class KeyValueObject {
|
|
|
619
705
|
|
|
620
706
|
|
|
621
707
|
|
|
708
|
+
;// ./lib/helpers/stringFormatter/stringFormatter.js
|
|
709
|
+
function stringFormatter(str) {
|
|
710
|
+
return (str || '').toUpperCase().replace('-', '_').replace(' ', '_')
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
|
|
714
|
+
|
|
622
715
|
;// ./lib/models/metadata/metadata.js
|
|
623
716
|
|
|
624
717
|
|
|
@@ -634,16 +727,25 @@ class Metadata extends KeyValueObject {
|
|
|
634
727
|
})
|
|
635
728
|
return instance.isValid ? instance : null
|
|
636
729
|
}
|
|
730
|
+
static get _classname() {
|
|
731
|
+
return 'Metadata'
|
|
732
|
+
}
|
|
637
733
|
|
|
638
|
-
static
|
|
639
|
-
|
|
640
|
-
|
|
734
|
+
static merge(toArr, fromArr) {
|
|
735
|
+
(fromArr || []).map((from) => {
|
|
736
|
+
const found = toArr.find((to) => {
|
|
737
|
+
return stringFormatter(to.key) === stringFormatter(from.key)
|
|
738
|
+
})
|
|
739
|
+
if (found) {
|
|
740
|
+
found.value = (found.value || []).concat(from.value)
|
|
741
|
+
} else {
|
|
742
|
+
toArr.push(from)
|
|
743
|
+
}
|
|
641
744
|
})
|
|
642
|
-
return
|
|
745
|
+
return toArr
|
|
643
746
|
}
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
return 'Metadata'
|
|
747
|
+
static sameKey(item, key) {
|
|
748
|
+
return stringFormatter(item.key) === stringFormatter(key)
|
|
647
749
|
}
|
|
648
750
|
}
|
|
649
751
|
|
|
@@ -790,68 +892,112 @@ class Repo {
|
|
|
790
892
|
}
|
|
791
893
|
}
|
|
792
894
|
|
|
793
|
-
|
|
895
|
+
// systemLog is optional
|
|
896
|
+
findAll({ query, systemLog }) {
|
|
897
|
+
const log = _makeLog({
|
|
898
|
+
systemLog,
|
|
899
|
+
label: 'REPO_READ',
|
|
900
|
+
message: `fn ${this._classname}.prototype.findAll`,
|
|
901
|
+
input: [{ query: { ...query }, systemLog: { ...systemLog } }]
|
|
902
|
+
})
|
|
794
903
|
return new Promise((resolve, reject) => {
|
|
795
904
|
this.model.findAll(query, this.queryOptions, (err, data, total) => {
|
|
796
905
|
if (err) {
|
|
906
|
+
log({ level: 'warn', output: err.toString() })
|
|
797
907
|
reject(err)
|
|
798
908
|
} else {
|
|
799
|
-
|
|
909
|
+
const result = {
|
|
800
910
|
isNew: false,
|
|
801
911
|
data,
|
|
802
912
|
total: total || data.length
|
|
803
|
-
}
|
|
913
|
+
}
|
|
914
|
+
log({ level: 'info', output: { ...result } })
|
|
915
|
+
resolve(result)
|
|
804
916
|
}
|
|
805
917
|
})
|
|
806
918
|
})
|
|
807
919
|
}
|
|
808
920
|
|
|
809
|
-
findOne({ query }) {
|
|
921
|
+
findOne({ query, systemLog }) {
|
|
922
|
+
const log = _makeLog({
|
|
923
|
+
systemLog,
|
|
924
|
+
label: 'REPO_READ',
|
|
925
|
+
message: `fn ${this._classname}.prototype.findOne`,
|
|
926
|
+
input: [{ query: { ...query }, systemLog: { ...systemLog } }]
|
|
927
|
+
})
|
|
810
928
|
return new Promise((resolve, reject) => {
|
|
811
929
|
this.model.findAll(query, this.queryOptions, (err, data) => {
|
|
812
930
|
if (err) {
|
|
813
931
|
reject(err)
|
|
814
932
|
} else if (data.length === 1) {
|
|
815
|
-
|
|
933
|
+
const result = {
|
|
816
934
|
isNew: false,
|
|
817
935
|
data,
|
|
818
936
|
total: 1
|
|
819
|
-
}
|
|
937
|
+
}
|
|
938
|
+
log({ level: 'info', output: { ...result } })
|
|
939
|
+
resolve(result)
|
|
820
940
|
} else if (data.length === 0) {
|
|
821
941
|
reject(new Error('record not found'))
|
|
822
942
|
} else {
|
|
823
943
|
reject(new Error('more than one is found'))
|
|
824
944
|
}
|
|
825
945
|
})
|
|
946
|
+
.catch((err) => {
|
|
947
|
+
log({ level: 'warn', output: err.toString() })
|
|
948
|
+
throw err
|
|
949
|
+
})
|
|
826
950
|
})
|
|
827
951
|
}
|
|
828
952
|
|
|
829
|
-
saveAll({ docs }) {
|
|
953
|
+
saveAll({ docs, systemLog }) {
|
|
830
954
|
let isNew
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
955
|
+
const log = _makeLog({
|
|
956
|
+
systemLog,
|
|
957
|
+
label: 'REPO_WRITE',
|
|
958
|
+
message: `fn ${this._classname}.prototype.saveAll`,
|
|
959
|
+
input: [{ docs: [...docs], systemLog: { ...systemLog } }]
|
|
960
|
+
})
|
|
961
|
+
const promise = typeof this.model.saveAll === 'function'
|
|
962
|
+
? this.model.saveAll({ docs })
|
|
963
|
+
: Promise.all(docs.map(async (doc) => {
|
|
964
|
+
if (doc) {
|
|
965
|
+
const result = await this.saveOne({ doc })
|
|
966
|
+
isNew = result.isNew
|
|
967
|
+
const _data = result._data || result.data
|
|
968
|
+
return _data[0]
|
|
969
|
+
}
|
|
970
|
+
return null
|
|
971
|
+
}))
|
|
972
|
+
return promise.then((savedData) => {
|
|
840
973
|
if (savedData.length !== 1) isNew = null
|
|
841
|
-
|
|
974
|
+
const result = {
|
|
842
975
|
data: savedData,
|
|
843
976
|
isNew,
|
|
844
977
|
total: savedData.length
|
|
845
978
|
}
|
|
979
|
+
log({ level: 'info', output: { ...result } })
|
|
980
|
+
return result
|
|
981
|
+
}).catch((err) => {
|
|
982
|
+
log({ level: 'warn', output: err.toString() })
|
|
983
|
+
throw err
|
|
846
984
|
})
|
|
847
985
|
}
|
|
848
986
|
|
|
849
|
-
saveOne({ doc }) {
|
|
987
|
+
saveOne({ doc, systemLog }) {
|
|
988
|
+
const log = _makeLog({
|
|
989
|
+
systemLog,
|
|
990
|
+
label: 'REPO_WRITE',
|
|
991
|
+
message: `fn ${this._classname}.prototype.saveOne`,
|
|
992
|
+
input: [{ doc: { ...doc }, systemLog: { ...systemLog } }]
|
|
993
|
+
})
|
|
850
994
|
return new Promise((resolve, reject) => {
|
|
851
995
|
this.model.saveOne(doc, this.saveOptions, (err, result) => {
|
|
852
996
|
if (err) {
|
|
997
|
+
log({ level: 'warn', output: err.toString() })
|
|
853
998
|
reject(err)
|
|
854
999
|
} else {
|
|
1000
|
+
log({ level: 'info', output: { ...result } })
|
|
855
1001
|
resolve(result)
|
|
856
1002
|
}
|
|
857
1003
|
})
|
|
@@ -859,6 +1005,25 @@ class Repo {
|
|
|
859
1005
|
}
|
|
860
1006
|
}
|
|
861
1007
|
|
|
1008
|
+
function _makeLog({ systemLog, label, message: message1, input } = {}) {
|
|
1009
|
+
return ({ level, messgae: massage2, output } = {}) => {
|
|
1010
|
+
if (systemLog && systemLog.systemLogHelper) {
|
|
1011
|
+
systemLog.systemLogHelper.log({
|
|
1012
|
+
batchId: systemLog.batchId,
|
|
1013
|
+
label,
|
|
1014
|
+
level,
|
|
1015
|
+
message: massage2 || message1,
|
|
1016
|
+
data: {
|
|
1017
|
+
payload: {
|
|
1018
|
+
input,
|
|
1019
|
+
output
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
})
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
|
|
862
1027
|
|
|
863
1028
|
|
|
864
1029
|
;// ./lib/models/repo/index.js
|
|
@@ -889,16 +1054,16 @@ class Service {
|
|
|
889
1054
|
})
|
|
890
1055
|
}
|
|
891
1056
|
|
|
892
|
-
async findAll({ query = {} } = {}) {
|
|
893
|
-
const result = await this.repo.findAll({ query })
|
|
1057
|
+
async findAll({ query = {}, systemLog } = {}) {
|
|
1058
|
+
const result = await this.repo.findAll({ query, systemLog })
|
|
894
1059
|
return makeApiResponse({
|
|
895
1060
|
repo: this.repo,
|
|
896
1061
|
result
|
|
897
1062
|
})
|
|
898
1063
|
}
|
|
899
1064
|
|
|
900
|
-
async findOne({ query = {} } = {}) {
|
|
901
|
-
const result = await this.repo.findOne({ query })
|
|
1065
|
+
async findOne({ query = {}, systemLog } = {}) {
|
|
1066
|
+
const result = await this.repo.findOne({ query, systemLog })
|
|
902
1067
|
return makeApiResponse({
|
|
903
1068
|
repo: this.repo,
|
|
904
1069
|
result
|
|
@@ -912,21 +1077,21 @@ class Service {
|
|
|
912
1077
|
return arr.map((i) => this.init(i))
|
|
913
1078
|
}
|
|
914
1079
|
|
|
915
|
-
async saveAll({ docs = [] } = {}) {
|
|
1080
|
+
async saveAll({ docs = [], systemLog } = {}) {
|
|
916
1081
|
const copies = docs.map((doc) => {
|
|
917
1082
|
return this.init(doc)
|
|
918
1083
|
})
|
|
919
|
-
const result = await this.repo.saveAll({ docs: copies })
|
|
1084
|
+
const result = await this.repo.saveAll({ docs: copies, systemLog })
|
|
920
1085
|
return makeApiResponse({
|
|
921
1086
|
repo: this.repo,
|
|
922
1087
|
result
|
|
923
1088
|
})
|
|
924
1089
|
}
|
|
925
1090
|
|
|
926
|
-
async saveOne({ doc = {} } = {}) {
|
|
1091
|
+
async saveOne({ doc = {}, systemLog } = {}) {
|
|
927
1092
|
const copy = this.init(doc)
|
|
928
1093
|
if (copy) {
|
|
929
|
-
const result = await this.repo.saveOne({ doc: copy })
|
|
1094
|
+
const result = await this.repo.saveOne({ doc: copy, systemLog })
|
|
930
1095
|
return makeApiResponse({
|
|
931
1096
|
repo: this.repo,
|
|
932
1097
|
result
|
|
@@ -957,6 +1122,54 @@ function makeService({ repo }) {
|
|
|
957
1122
|
|
|
958
1123
|
|
|
959
1124
|
|
|
1125
|
+
;// ./lib/models/uniqueKeyGenerator/uniqueKeyGenerator.js
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
class UniqueKeyGenerator {
|
|
1129
|
+
static get _classname() {
|
|
1130
|
+
return 'UniqueKeyGenerator'
|
|
1131
|
+
}
|
|
1132
|
+
static get _superclass() {
|
|
1133
|
+
return 'UniqueKeyGenerator'
|
|
1134
|
+
}
|
|
1135
|
+
static makeFormatter({ fieldName, format, options }) {
|
|
1136
|
+
switch (format) {
|
|
1137
|
+
case 'set_code':
|
|
1138
|
+
return _makeSetCode(fieldName, options)
|
|
1139
|
+
default:
|
|
1140
|
+
return _makeSetCode(fieldName, options)
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
static makeGenerator(arr) {
|
|
1144
|
+
const fns = arr.map((item) => this.makeFormatter(item))
|
|
1145
|
+
return async (obj) => {
|
|
1146
|
+
const output = await pReduce(fns, async (acc, fn) => {
|
|
1147
|
+
const _obj = await fn(obj)
|
|
1148
|
+
return Object.assign(acc, _obj)
|
|
1149
|
+
}, obj)
|
|
1150
|
+
return output
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
function _makeSetCode(fieldName, options) {
|
|
1156
|
+
return async (obj = {}) => {
|
|
1157
|
+
if (obj[fieldName]) {
|
|
1158
|
+
return {}
|
|
1159
|
+
}
|
|
1160
|
+
return {
|
|
1161
|
+
[fieldName]: stringHelper.setCode()
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
|
|
1167
|
+
|
|
1168
|
+
;// ./lib/models/uniqueKeyGenerator/index.js
|
|
1169
|
+
|
|
1170
|
+
|
|
1171
|
+
|
|
1172
|
+
|
|
960
1173
|
;// ./lib/models/index.js
|
|
961
1174
|
|
|
962
1175
|
|
|
@@ -965,6 +1178,193 @@ function makeService({ repo }) {
|
|
|
965
1178
|
|
|
966
1179
|
|
|
967
1180
|
|
|
1181
|
+
|
|
1182
|
+
;// ./lib/helpers/generalPost/generalPost.js
|
|
1183
|
+
|
|
1184
|
+
|
|
1185
|
+
|
|
1186
|
+
|
|
1187
|
+
async function generalPost({ body = {}, GeneralModel, UniqueKeyGenerator, resourceInfo }) {
|
|
1188
|
+
const { resources, data, globalShared = {}, shared = {}, relationship = {} } = body
|
|
1189
|
+
const _resourceInfo = resourceInfo || body.resourceInfo
|
|
1190
|
+
_attachShared(data, globalShared, shared)
|
|
1191
|
+
const obj = await pReduce(resources, async (acc, resource) => {
|
|
1192
|
+
const service = _makeService(resource, _resourceInfo, UniqueKeyGenerator, GeneralModel)
|
|
1193
|
+
_createRelationship(data, relationship[resource], acc)
|
|
1194
|
+
const _data = data[resource]
|
|
1195
|
+
const result = await service.saveAll({ docs: [].concat(_data) })
|
|
1196
|
+
acc[resource] = Array.isArray(_data) ? result._data : result._data[0]
|
|
1197
|
+
return acc
|
|
1198
|
+
}, {})
|
|
1199
|
+
return obj
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
function _attachShared(data, globalShared = {}, shared = {}) {
|
|
1203
|
+
Object.keys(shared).forEach((key) => {
|
|
1204
|
+
const _data = data[key]
|
|
1205
|
+
data[key] = objectHelper.merge({}, _data, globalShared, shared[key] || {})
|
|
1206
|
+
})
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
function _createRelationship(data, relationship = {}, object) {
|
|
1210
|
+
Object.keys(relationship).forEach((key) => {
|
|
1211
|
+
const path = relationship[key]
|
|
1212
|
+
const val = objectHelper.get(object, path)
|
|
1213
|
+
objectHelper.set(data, key, val)
|
|
1214
|
+
})
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
function _makeService(resource, resourceInfo, UniqueKeyGenerator, GeneralModel) {
|
|
1218
|
+
const { collectionName, fields } = resourceInfo[resource]
|
|
1219
|
+
const uniqueKeyGenerator = UniqueKeyGenerator.makeGenerator(fields)
|
|
1220
|
+
const model = new GeneralModel({ collectionName, uniqueKeyGenerator })
|
|
1221
|
+
return makeService({
|
|
1222
|
+
repo: new Repo({ model })
|
|
1223
|
+
})
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
|
|
1227
|
+
|
|
1228
|
+
;// ./lib/helpers/generalPost/index.js
|
|
1229
|
+
|
|
1230
|
+
|
|
1231
|
+
|
|
1232
|
+
|
|
1233
|
+
;// ./lib/helpers/padZeros/padZeros.js
|
|
1234
|
+
function padZeros(num, minLength = 6) {
|
|
1235
|
+
num = num.toString()
|
|
1236
|
+
if (num.length < minLength) {
|
|
1237
|
+
return padZeros('0' + num, minLength)
|
|
1238
|
+
}
|
|
1239
|
+
return num
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1242
|
+
|
|
1243
|
+
|
|
1244
|
+
;// ./lib/helpers/padZeros/index.js
|
|
1245
|
+
|
|
1246
|
+
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
;// ./lib/helpers/pReduce/index.js
|
|
1250
|
+
|
|
1251
|
+
|
|
1252
|
+
|
|
1253
|
+
|
|
1254
|
+
;// ./lib/helpers/stringFormatter/index.js
|
|
1255
|
+
|
|
1256
|
+
|
|
1257
|
+
|
|
1258
|
+
|
|
1259
|
+
;// ./lib/helpers/stringHelper/stringHelper.js
|
|
1260
|
+
function baseXEncode(num, base = 34) {
|
|
1261
|
+
const charset = getBaseCharset(base)
|
|
1262
|
+
return encode(num, charset)
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
function encode(int, charset) {
|
|
1266
|
+
let byCode = charset.byCode;
|
|
1267
|
+
if (int === 0) {
|
|
1268
|
+
return byCode[0];
|
|
1269
|
+
}
|
|
1270
|
+
|
|
1271
|
+
var res = "",
|
|
1272
|
+
max = charset.length;
|
|
1273
|
+
while (int > 0) {
|
|
1274
|
+
res = byCode[int % max] + res;
|
|
1275
|
+
int = Math.floor(int / max);
|
|
1276
|
+
}
|
|
1277
|
+
return res;
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
function getBaseCharset(base) {
|
|
1281
|
+
let charset = '9876543210ABCDEFGHJKLMNPQRSTUVWXYZ'
|
|
1282
|
+
if (base === 58) {
|
|
1283
|
+
charset = '9876543210ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz'
|
|
1284
|
+
}
|
|
1285
|
+
return indexCharset(charset)
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
function indexCharset(str) {
|
|
1289
|
+
var byCode = {},
|
|
1290
|
+
byChar = {},
|
|
1291
|
+
length = str.length,
|
|
1292
|
+
i, char;
|
|
1293
|
+
for (i = 0; i < length; i++) {
|
|
1294
|
+
char = str[i];
|
|
1295
|
+
byCode[i] = char;
|
|
1296
|
+
byChar[char] = i;
|
|
1297
|
+
}
|
|
1298
|
+
return { byCode: byCode, byChar: byChar, length: length };
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
function randomString({ len = 16, pattern = 'a1' } = {}) {
|
|
1302
|
+
const A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
1303
|
+
const a = 'abcdefghijklmnopqrstuvwxyz'
|
|
1304
|
+
const num = '1234567890'
|
|
1305
|
+
const mark = '~!@#$%^&*_+-='
|
|
1306
|
+
let str = ''
|
|
1307
|
+
if (pattern.includes('A')) {
|
|
1308
|
+
str += A
|
|
1309
|
+
}
|
|
1310
|
+
if (pattern.includes('a')) {
|
|
1311
|
+
str += a
|
|
1312
|
+
}
|
|
1313
|
+
if (pattern.includes('1')) {
|
|
1314
|
+
str += num
|
|
1315
|
+
}
|
|
1316
|
+
if (pattern.includes('#')) {
|
|
1317
|
+
str += mark
|
|
1318
|
+
}
|
|
1319
|
+
const chars = [...str]
|
|
1320
|
+
return [...Array(len)].map(i => {
|
|
1321
|
+
return chars[(Math.random() * chars.length) | 0]
|
|
1322
|
+
}).join``
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
function reverse(str) {
|
|
1326
|
+
if (typeof str !== 'string') {
|
|
1327
|
+
str = str.toString()
|
|
1328
|
+
}
|
|
1329
|
+
const splitString = str.split('')
|
|
1330
|
+
const reverseArray = splitString.reverse()
|
|
1331
|
+
return reverseArray.join('')
|
|
1332
|
+
}
|
|
1333
|
+
|
|
1334
|
+
function setCode(base = 34) {
|
|
1335
|
+
const now = (new Date()).valueOf()
|
|
1336
|
+
const random = randomString({
|
|
1337
|
+
len: 8,
|
|
1338
|
+
pattern: '1'
|
|
1339
|
+
})
|
|
1340
|
+
const str = reverse(`${now}${random}`)
|
|
1341
|
+
// const str = `${now}${random}`
|
|
1342
|
+
return baseXEncode(str, base)
|
|
1343
|
+
}
|
|
1344
|
+
|
|
1345
|
+
const stringHelper = {
|
|
1346
|
+
setCode
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
|
|
1350
|
+
;// ./lib/helpers/stringHelper/index.js
|
|
1351
|
+
|
|
1352
|
+
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
|
|
1356
|
+
;// ./lib/helpers/index.js
|
|
1357
|
+
|
|
1358
|
+
|
|
1359
|
+
|
|
1360
|
+
|
|
1361
|
+
|
|
1362
|
+
|
|
1363
|
+
|
|
1364
|
+
|
|
1365
|
+
|
|
1366
|
+
|
|
1367
|
+
|
|
968
1368
|
;// ./lib/index.js
|
|
969
1369
|
|
|
970
1370
|
|