@questwork/q-utilities 0.1.4 → 0.1.6
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 +472 -46
- package/dist/index.min.js +478 -47
- package/package.json +7 -7
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
|
|
|
@@ -483,7 +587,7 @@ class KeyValueObject {
|
|
|
483
587
|
if (typeof value === 'undefined') {
|
|
484
588
|
return arr.filter((item) => this.sameKey(item, key)).length > 0
|
|
485
589
|
}
|
|
486
|
-
return arr.filter((item) => (this.sameKey(item, key) && item.value
|
|
590
|
+
return arr.filter((item) => (this.sameKey(item, key) && _isSame(item.value, value))).length > 0
|
|
487
591
|
}
|
|
488
592
|
static insertOrUpdateRecord(arr = [], key, value) {
|
|
489
593
|
let copy = [...arr]
|
|
@@ -525,7 +629,7 @@ class KeyValueObject {
|
|
|
525
629
|
}, [])
|
|
526
630
|
}
|
|
527
631
|
static sameKey(item, key) {
|
|
528
|
-
return item.key
|
|
632
|
+
return _isSame(item.key, key)
|
|
529
633
|
}
|
|
530
634
|
static toObject(arr = []) {
|
|
531
635
|
if (Array.isArray(arr)) {
|
|
@@ -594,6 +698,10 @@ class KeyValueObject {
|
|
|
594
698
|
}
|
|
595
699
|
}
|
|
596
700
|
|
|
701
|
+
function _isSame(key1, key2) {
|
|
702
|
+
return key1 === key2
|
|
703
|
+
}
|
|
704
|
+
|
|
597
705
|
|
|
598
706
|
|
|
599
707
|
;// ./lib/models/keyValueObject/index.js
|
|
@@ -601,10 +709,26 @@ class KeyValueObject {
|
|
|
601
709
|
|
|
602
710
|
|
|
603
711
|
|
|
712
|
+
;// ./lib/helpers/stringFormatter/stringFormatter.js
|
|
713
|
+
function stringFormatter(str, delimiter = '_') {
|
|
714
|
+
if (str === null || typeof str === 'undefined' || typeof str.toString === 'undefined') {
|
|
715
|
+
return null
|
|
716
|
+
}
|
|
717
|
+
return str.toString()
|
|
718
|
+
.trim()
|
|
719
|
+
.toUpperCase()
|
|
720
|
+
.replace('-', delimiter)
|
|
721
|
+
.replace(' ', delimiter)
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
|
|
604
726
|
;// ./lib/models/metadata/metadata.js
|
|
605
727
|
|
|
606
728
|
|
|
607
729
|
|
|
730
|
+
const DELIMITER = '_'
|
|
731
|
+
|
|
608
732
|
class Metadata extends KeyValueObject {
|
|
609
733
|
static init(options = {}) {
|
|
610
734
|
if (options instanceof this) {
|
|
@@ -612,7 +736,7 @@ class Metadata extends KeyValueObject {
|
|
|
612
736
|
}
|
|
613
737
|
const instance = new this({
|
|
614
738
|
...options,
|
|
615
|
-
key: stringFormatter(options.key),
|
|
739
|
+
key: stringFormatter(options.key, DELIMITER),
|
|
616
740
|
})
|
|
617
741
|
return instance.isValid ? instance : null
|
|
618
742
|
}
|
|
@@ -623,7 +747,7 @@ class Metadata extends KeyValueObject {
|
|
|
623
747
|
static merge(toArr, fromArr) {
|
|
624
748
|
(fromArr || []).map((from) => {
|
|
625
749
|
const found = toArr.find((to) => {
|
|
626
|
-
return
|
|
750
|
+
return metadata_isSame(to.key, from.key)
|
|
627
751
|
})
|
|
628
752
|
if (found) {
|
|
629
753
|
found.value = (found.value || []).concat(from.value)
|
|
@@ -634,10 +758,14 @@ class Metadata extends KeyValueObject {
|
|
|
634
758
|
return toArr
|
|
635
759
|
}
|
|
636
760
|
static sameKey(item, key) {
|
|
637
|
-
return
|
|
761
|
+
return metadata_isSame(item.key, key)
|
|
638
762
|
}
|
|
639
763
|
}
|
|
640
764
|
|
|
765
|
+
function metadata_isSame(key1, key2) {
|
|
766
|
+
return stringFormatter(key1, DELIMITER) === stringFormatter(key2, DELIMITER)
|
|
767
|
+
}
|
|
768
|
+
|
|
641
769
|
|
|
642
770
|
|
|
643
771
|
;// ./lib/models/metadata/index.js
|
|
@@ -781,68 +909,112 @@ class Repo {
|
|
|
781
909
|
}
|
|
782
910
|
}
|
|
783
911
|
|
|
784
|
-
|
|
912
|
+
// systemLog is optional
|
|
913
|
+
findAll({ query, systemLog }) {
|
|
914
|
+
const log = _makeLog({
|
|
915
|
+
systemLog,
|
|
916
|
+
label: 'REPO_READ',
|
|
917
|
+
message: `fn ${this._classname}.prototype.findAll`,
|
|
918
|
+
input: [{ query: { ...query }, systemLog: { ...systemLog } }]
|
|
919
|
+
})
|
|
785
920
|
return new Promise((resolve, reject) => {
|
|
786
921
|
this.model.findAll(query, this.queryOptions, (err, data, total) => {
|
|
787
922
|
if (err) {
|
|
923
|
+
log({ level: 'warn', output: err.toString() })
|
|
788
924
|
reject(err)
|
|
789
925
|
} else {
|
|
790
|
-
|
|
926
|
+
const result = {
|
|
791
927
|
isNew: false,
|
|
792
928
|
data,
|
|
793
929
|
total: total || data.length
|
|
794
|
-
}
|
|
930
|
+
}
|
|
931
|
+
log({ level: 'info', output: { ...result } })
|
|
932
|
+
resolve(result)
|
|
795
933
|
}
|
|
796
934
|
})
|
|
797
935
|
})
|
|
798
936
|
}
|
|
799
937
|
|
|
800
|
-
findOne({ query }) {
|
|
938
|
+
findOne({ query, systemLog }) {
|
|
939
|
+
const log = _makeLog({
|
|
940
|
+
systemLog,
|
|
941
|
+
label: 'REPO_READ',
|
|
942
|
+
message: `fn ${this._classname}.prototype.findOne`,
|
|
943
|
+
input: [{ query: { ...query }, systemLog: { ...systemLog } }]
|
|
944
|
+
})
|
|
801
945
|
return new Promise((resolve, reject) => {
|
|
802
946
|
this.model.findAll(query, this.queryOptions, (err, data) => {
|
|
803
947
|
if (err) {
|
|
804
948
|
reject(err)
|
|
805
949
|
} else if (data.length === 1) {
|
|
806
|
-
|
|
950
|
+
const result = {
|
|
807
951
|
isNew: false,
|
|
808
952
|
data,
|
|
809
953
|
total: 1
|
|
810
|
-
}
|
|
954
|
+
}
|
|
955
|
+
log({ level: 'info', output: { ...result } })
|
|
956
|
+
resolve(result)
|
|
811
957
|
} else if (data.length === 0) {
|
|
812
958
|
reject(new Error('record not found'))
|
|
813
959
|
} else {
|
|
814
960
|
reject(new Error('more than one is found'))
|
|
815
961
|
}
|
|
816
962
|
})
|
|
963
|
+
.catch((err) => {
|
|
964
|
+
log({ level: 'warn', output: err.toString() })
|
|
965
|
+
throw err
|
|
966
|
+
})
|
|
817
967
|
})
|
|
818
968
|
}
|
|
819
969
|
|
|
820
|
-
saveAll({ docs }) {
|
|
970
|
+
saveAll({ docs, systemLog }) {
|
|
821
971
|
let isNew
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
972
|
+
const log = _makeLog({
|
|
973
|
+
systemLog,
|
|
974
|
+
label: 'REPO_WRITE',
|
|
975
|
+
message: `fn ${this._classname}.prototype.saveAll`,
|
|
976
|
+
input: [{ docs: [...docs], systemLog: { ...systemLog } }]
|
|
977
|
+
})
|
|
978
|
+
const promise = typeof this.model.saveAll === 'function'
|
|
979
|
+
? this.model.saveAll({ docs })
|
|
980
|
+
: Promise.all(docs.map(async (doc) => {
|
|
981
|
+
if (doc) {
|
|
982
|
+
const result = await this.saveOne({ doc })
|
|
983
|
+
isNew = result.isNew
|
|
984
|
+
const _data = result._data || result.data
|
|
985
|
+
return _data[0]
|
|
986
|
+
}
|
|
987
|
+
return null
|
|
988
|
+
}))
|
|
989
|
+
return promise.then((savedData) => {
|
|
831
990
|
if (savedData.length !== 1) isNew = null
|
|
832
|
-
|
|
991
|
+
const result = {
|
|
833
992
|
data: savedData,
|
|
834
993
|
isNew,
|
|
835
994
|
total: savedData.length
|
|
836
995
|
}
|
|
996
|
+
log({ level: 'info', output: { ...result } })
|
|
997
|
+
return result
|
|
998
|
+
}).catch((err) => {
|
|
999
|
+
log({ level: 'warn', output: err.toString() })
|
|
1000
|
+
throw err
|
|
837
1001
|
})
|
|
838
1002
|
}
|
|
839
1003
|
|
|
840
|
-
saveOne({ doc }) {
|
|
1004
|
+
saveOne({ doc, systemLog }) {
|
|
1005
|
+
const log = _makeLog({
|
|
1006
|
+
systemLog,
|
|
1007
|
+
label: 'REPO_WRITE',
|
|
1008
|
+
message: `fn ${this._classname}.prototype.saveOne`,
|
|
1009
|
+
input: [{ doc: { ...doc }, systemLog: { ...systemLog } }]
|
|
1010
|
+
})
|
|
841
1011
|
return new Promise((resolve, reject) => {
|
|
842
1012
|
this.model.saveOne(doc, this.saveOptions, (err, result) => {
|
|
843
1013
|
if (err) {
|
|
1014
|
+
log({ level: 'warn', output: err.toString() })
|
|
844
1015
|
reject(err)
|
|
845
1016
|
} else {
|
|
1017
|
+
log({ level: 'info', output: { ...result } })
|
|
846
1018
|
resolve(result)
|
|
847
1019
|
}
|
|
848
1020
|
})
|
|
@@ -850,6 +1022,25 @@ class Repo {
|
|
|
850
1022
|
}
|
|
851
1023
|
}
|
|
852
1024
|
|
|
1025
|
+
function _makeLog({ systemLog, label, message: message1, input } = {}) {
|
|
1026
|
+
return ({ level, messgae: massage2, output } = {}) => {
|
|
1027
|
+
if (systemLog && systemLog.systemLogHelper) {
|
|
1028
|
+
systemLog.systemLogHelper.log({
|
|
1029
|
+
batchId: systemLog.batchId,
|
|
1030
|
+
label,
|
|
1031
|
+
level,
|
|
1032
|
+
message: massage2 || message1,
|
|
1033
|
+
data: {
|
|
1034
|
+
payload: {
|
|
1035
|
+
input,
|
|
1036
|
+
output
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
})
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
|
|
853
1044
|
|
|
854
1045
|
|
|
855
1046
|
;// ./lib/models/repo/index.js
|
|
@@ -880,16 +1071,16 @@ class Service {
|
|
|
880
1071
|
})
|
|
881
1072
|
}
|
|
882
1073
|
|
|
883
|
-
async findAll({ query = {} } = {}) {
|
|
884
|
-
const result = await this.repo.findAll({ query })
|
|
1074
|
+
async findAll({ query = {}, systemLog } = {}) {
|
|
1075
|
+
const result = await this.repo.findAll({ query, systemLog })
|
|
885
1076
|
return makeApiResponse({
|
|
886
1077
|
repo: this.repo,
|
|
887
1078
|
result
|
|
888
1079
|
})
|
|
889
1080
|
}
|
|
890
1081
|
|
|
891
|
-
async findOne({ query = {} } = {}) {
|
|
892
|
-
const result = await this.repo.findOne({ query })
|
|
1082
|
+
async findOne({ query = {}, systemLog } = {}) {
|
|
1083
|
+
const result = await this.repo.findOne({ query, systemLog })
|
|
893
1084
|
return makeApiResponse({
|
|
894
1085
|
repo: this.repo,
|
|
895
1086
|
result
|
|
@@ -903,21 +1094,21 @@ class Service {
|
|
|
903
1094
|
return arr.map((i) => this.init(i))
|
|
904
1095
|
}
|
|
905
1096
|
|
|
906
|
-
async saveAll({ docs = [] } = {}) {
|
|
1097
|
+
async saveAll({ docs = [], systemLog } = {}) {
|
|
907
1098
|
const copies = docs.map((doc) => {
|
|
908
1099
|
return this.init(doc)
|
|
909
1100
|
})
|
|
910
|
-
const result = await this.repo.saveAll({ docs: copies })
|
|
1101
|
+
const result = await this.repo.saveAll({ docs: copies, systemLog })
|
|
911
1102
|
return makeApiResponse({
|
|
912
1103
|
repo: this.repo,
|
|
913
1104
|
result
|
|
914
1105
|
})
|
|
915
1106
|
}
|
|
916
1107
|
|
|
917
|
-
async saveOne({ doc = {} } = {}) {
|
|
1108
|
+
async saveOne({ doc = {}, systemLog } = {}) {
|
|
918
1109
|
const copy = this.init(doc)
|
|
919
1110
|
if (copy) {
|
|
920
|
-
const result = await this.repo.saveOne({ doc: copy })
|
|
1111
|
+
const result = await this.repo.saveOne({ doc: copy, systemLog })
|
|
921
1112
|
return makeApiResponse({
|
|
922
1113
|
repo: this.repo,
|
|
923
1114
|
result
|
|
@@ -948,6 +1139,54 @@ function makeService({ repo }) {
|
|
|
948
1139
|
|
|
949
1140
|
|
|
950
1141
|
|
|
1142
|
+
;// ./lib/models/uniqueKeyGenerator/uniqueKeyGenerator.js
|
|
1143
|
+
|
|
1144
|
+
|
|
1145
|
+
class UniqueKeyGenerator {
|
|
1146
|
+
static get _classname() {
|
|
1147
|
+
return 'UniqueKeyGenerator'
|
|
1148
|
+
}
|
|
1149
|
+
static get _superclass() {
|
|
1150
|
+
return 'UniqueKeyGenerator'
|
|
1151
|
+
}
|
|
1152
|
+
static makeFormatter({ fieldName, format, options }) {
|
|
1153
|
+
switch (format) {
|
|
1154
|
+
case 'set_code':
|
|
1155
|
+
return _makeSetCode(fieldName, options)
|
|
1156
|
+
default:
|
|
1157
|
+
return _makeSetCode(fieldName, options)
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
static makeGenerator(arr) {
|
|
1161
|
+
const fns = arr.map((item) => this.makeFormatter(item))
|
|
1162
|
+
return async (obj) => {
|
|
1163
|
+
const output = await pReduce(fns, async (acc, fn) => {
|
|
1164
|
+
const _obj = await fn(obj)
|
|
1165
|
+
return Object.assign(acc, _obj)
|
|
1166
|
+
}, obj)
|
|
1167
|
+
return output
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
function _makeSetCode(fieldName, options) {
|
|
1173
|
+
return async (obj = {}) => {
|
|
1174
|
+
if (obj[fieldName]) {
|
|
1175
|
+
return {}
|
|
1176
|
+
}
|
|
1177
|
+
return {
|
|
1178
|
+
[fieldName]: stringHelper.setCode()
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
|
|
1184
|
+
|
|
1185
|
+
;// ./lib/models/uniqueKeyGenerator/index.js
|
|
1186
|
+
|
|
1187
|
+
|
|
1188
|
+
|
|
1189
|
+
|
|
951
1190
|
;// ./lib/models/index.js
|
|
952
1191
|
|
|
953
1192
|
|
|
@@ -956,6 +1195,193 @@ function makeService({ repo }) {
|
|
|
956
1195
|
|
|
957
1196
|
|
|
958
1197
|
|
|
1198
|
+
|
|
1199
|
+
;// ./lib/helpers/generalPost/generalPost.js
|
|
1200
|
+
|
|
1201
|
+
|
|
1202
|
+
|
|
1203
|
+
|
|
1204
|
+
async function generalPost({ body = {}, GeneralModel, UniqueKeyGenerator, resourceInfo }) {
|
|
1205
|
+
const { resources, data, globalShared = {}, shared = {}, relationship = {} } = body
|
|
1206
|
+
const _resourceInfo = resourceInfo || body.resourceInfo
|
|
1207
|
+
_attachShared(data, globalShared, shared)
|
|
1208
|
+
const obj = await pReduce(resources, async (acc, resource) => {
|
|
1209
|
+
const service = _makeService(resource, _resourceInfo, UniqueKeyGenerator, GeneralModel)
|
|
1210
|
+
_createRelationship(data, relationship[resource], acc)
|
|
1211
|
+
const _data = data[resource]
|
|
1212
|
+
const result = await service.saveAll({ docs: [].concat(_data) })
|
|
1213
|
+
acc[resource] = Array.isArray(_data) ? result._data : result._data[0]
|
|
1214
|
+
return acc
|
|
1215
|
+
}, {})
|
|
1216
|
+
return obj
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
function _attachShared(data, globalShared = {}, shared = {}) {
|
|
1220
|
+
Object.keys(shared).forEach((key) => {
|
|
1221
|
+
const _data = data[key]
|
|
1222
|
+
data[key] = objectHelper.merge({}, _data, globalShared, shared[key] || {})
|
|
1223
|
+
})
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
function _createRelationship(data, relationship = {}, object) {
|
|
1227
|
+
Object.keys(relationship).forEach((key) => {
|
|
1228
|
+
const path = relationship[key]
|
|
1229
|
+
const val = objectHelper.get(object, path)
|
|
1230
|
+
objectHelper.set(data, key, val)
|
|
1231
|
+
})
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
function _makeService(resource, resourceInfo, UniqueKeyGenerator, GeneralModel) {
|
|
1235
|
+
const { collectionName, fields } = resourceInfo[resource]
|
|
1236
|
+
const uniqueKeyGenerator = UniqueKeyGenerator.makeGenerator(fields)
|
|
1237
|
+
const model = new GeneralModel({ collectionName, uniqueKeyGenerator })
|
|
1238
|
+
return makeService({
|
|
1239
|
+
repo: new Repo({ model })
|
|
1240
|
+
})
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
|
|
1244
|
+
|
|
1245
|
+
;// ./lib/helpers/generalPost/index.js
|
|
1246
|
+
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
|
|
1250
|
+
;// ./lib/helpers/padZeros/padZeros.js
|
|
1251
|
+
function padZeros(num, minLength = 6) {
|
|
1252
|
+
num = num.toString()
|
|
1253
|
+
if (num.length < minLength) {
|
|
1254
|
+
return padZeros('0' + num, minLength)
|
|
1255
|
+
}
|
|
1256
|
+
return num
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
|
|
1260
|
+
|
|
1261
|
+
;// ./lib/helpers/padZeros/index.js
|
|
1262
|
+
|
|
1263
|
+
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
;// ./lib/helpers/pReduce/index.js
|
|
1267
|
+
|
|
1268
|
+
|
|
1269
|
+
|
|
1270
|
+
|
|
1271
|
+
;// ./lib/helpers/stringFormatter/index.js
|
|
1272
|
+
|
|
1273
|
+
|
|
1274
|
+
|
|
1275
|
+
|
|
1276
|
+
;// ./lib/helpers/stringHelper/stringHelper.js
|
|
1277
|
+
function baseXEncode(num, base = 34) {
|
|
1278
|
+
const charset = getBaseCharset(base)
|
|
1279
|
+
return encode(num, charset)
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
function encode(int, charset) {
|
|
1283
|
+
let byCode = charset.byCode;
|
|
1284
|
+
if (int === 0) {
|
|
1285
|
+
return byCode[0];
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
var res = "",
|
|
1289
|
+
max = charset.length;
|
|
1290
|
+
while (int > 0) {
|
|
1291
|
+
res = byCode[int % max] + res;
|
|
1292
|
+
int = Math.floor(int / max);
|
|
1293
|
+
}
|
|
1294
|
+
return res;
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
function getBaseCharset(base) {
|
|
1298
|
+
let charset = '9876543210ABCDEFGHJKLMNPQRSTUVWXYZ'
|
|
1299
|
+
if (base === 58) {
|
|
1300
|
+
charset = '9876543210ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz'
|
|
1301
|
+
}
|
|
1302
|
+
return indexCharset(charset)
|
|
1303
|
+
}
|
|
1304
|
+
|
|
1305
|
+
function indexCharset(str) {
|
|
1306
|
+
var byCode = {},
|
|
1307
|
+
byChar = {},
|
|
1308
|
+
length = str.length,
|
|
1309
|
+
i, char;
|
|
1310
|
+
for (i = 0; i < length; i++) {
|
|
1311
|
+
char = str[i];
|
|
1312
|
+
byCode[i] = char;
|
|
1313
|
+
byChar[char] = i;
|
|
1314
|
+
}
|
|
1315
|
+
return { byCode: byCode, byChar: byChar, length: length };
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
function randomString({ len = 16, pattern = 'a1' } = {}) {
|
|
1319
|
+
const A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
1320
|
+
const a = 'abcdefghijklmnopqrstuvwxyz'
|
|
1321
|
+
const num = '1234567890'
|
|
1322
|
+
const mark = '~!@#$%^&*_+-='
|
|
1323
|
+
let str = ''
|
|
1324
|
+
if (pattern.includes('A')) {
|
|
1325
|
+
str += A
|
|
1326
|
+
}
|
|
1327
|
+
if (pattern.includes('a')) {
|
|
1328
|
+
str += a
|
|
1329
|
+
}
|
|
1330
|
+
if (pattern.includes('1')) {
|
|
1331
|
+
str += num
|
|
1332
|
+
}
|
|
1333
|
+
if (pattern.includes('#')) {
|
|
1334
|
+
str += mark
|
|
1335
|
+
}
|
|
1336
|
+
const chars = [...str]
|
|
1337
|
+
return [...Array(len)].map(i => {
|
|
1338
|
+
return chars[(Math.random() * chars.length) | 0]
|
|
1339
|
+
}).join``
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
function reverse(str) {
|
|
1343
|
+
if (typeof str !== 'string') {
|
|
1344
|
+
str = str.toString()
|
|
1345
|
+
}
|
|
1346
|
+
const splitString = str.split('')
|
|
1347
|
+
const reverseArray = splitString.reverse()
|
|
1348
|
+
return reverseArray.join('')
|
|
1349
|
+
}
|
|
1350
|
+
|
|
1351
|
+
function setCode(base = 34) {
|
|
1352
|
+
const now = (new Date()).valueOf()
|
|
1353
|
+
const random = randomString({
|
|
1354
|
+
len: 8,
|
|
1355
|
+
pattern: '1'
|
|
1356
|
+
})
|
|
1357
|
+
const str = reverse(`${now}${random}`)
|
|
1358
|
+
// const str = `${now}${random}`
|
|
1359
|
+
return baseXEncode(str, base)
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
const stringHelper = {
|
|
1363
|
+
setCode
|
|
1364
|
+
}
|
|
1365
|
+
|
|
1366
|
+
|
|
1367
|
+
;// ./lib/helpers/stringHelper/index.js
|
|
1368
|
+
|
|
1369
|
+
|
|
1370
|
+
|
|
1371
|
+
|
|
1372
|
+
|
|
1373
|
+
;// ./lib/helpers/index.js
|
|
1374
|
+
|
|
1375
|
+
|
|
1376
|
+
|
|
1377
|
+
|
|
1378
|
+
|
|
1379
|
+
|
|
1380
|
+
|
|
1381
|
+
|
|
1382
|
+
|
|
1383
|
+
|
|
1384
|
+
|
|
959
1385
|
;// ./lib/index.js
|
|
960
1386
|
|
|
961
1387
|
|