@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.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
|
|
|
@@ -458,7 +562,7 @@ class KeyValueObject {
|
|
|
458
562
|
if (typeof value === 'undefined') {
|
|
459
563
|
return arr.filter((item) => this.sameKey(item, key)).length > 0
|
|
460
564
|
}
|
|
461
|
-
return arr.filter((item) => (this.sameKey(item, key) && item.value
|
|
565
|
+
return arr.filter((item) => (this.sameKey(item, key) && _isSame(item.value, value))).length > 0
|
|
462
566
|
}
|
|
463
567
|
static insertOrUpdateRecord(arr = [], key, value) {
|
|
464
568
|
let copy = [...arr]
|
|
@@ -500,7 +604,7 @@ class KeyValueObject {
|
|
|
500
604
|
}, [])
|
|
501
605
|
}
|
|
502
606
|
static sameKey(item, key) {
|
|
503
|
-
return item.key
|
|
607
|
+
return _isSame(item.key, key)
|
|
504
608
|
}
|
|
505
609
|
static toObject(arr = []) {
|
|
506
610
|
if (Array.isArray(arr)) {
|
|
@@ -569,6 +673,10 @@ class KeyValueObject {
|
|
|
569
673
|
}
|
|
570
674
|
}
|
|
571
675
|
|
|
676
|
+
function _isSame(key1, key2) {
|
|
677
|
+
return key1 === key2
|
|
678
|
+
}
|
|
679
|
+
|
|
572
680
|
|
|
573
681
|
|
|
574
682
|
;// ./lib/models/keyValueObject/index.js
|
|
@@ -576,10 +684,26 @@ class KeyValueObject {
|
|
|
576
684
|
|
|
577
685
|
|
|
578
686
|
|
|
687
|
+
;// ./lib/helpers/stringFormatter/stringFormatter.js
|
|
688
|
+
function stringFormatter(str, delimiter = '_') {
|
|
689
|
+
if (str === null || typeof str === 'undefined' || typeof str.toString === 'undefined') {
|
|
690
|
+
return null
|
|
691
|
+
}
|
|
692
|
+
return str.toString()
|
|
693
|
+
.trim()
|
|
694
|
+
.toUpperCase()
|
|
695
|
+
.replace('-', delimiter)
|
|
696
|
+
.replace(' ', delimiter)
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
|
|
579
701
|
;// ./lib/models/metadata/metadata.js
|
|
580
702
|
|
|
581
703
|
|
|
582
704
|
|
|
705
|
+
const DELIMITER = '_'
|
|
706
|
+
|
|
583
707
|
class Metadata extends KeyValueObject {
|
|
584
708
|
static init(options = {}) {
|
|
585
709
|
if (options instanceof this) {
|
|
@@ -587,7 +711,7 @@ class Metadata extends KeyValueObject {
|
|
|
587
711
|
}
|
|
588
712
|
const instance = new this({
|
|
589
713
|
...options,
|
|
590
|
-
key: stringFormatter(options.key),
|
|
714
|
+
key: stringFormatter(options.key, DELIMITER),
|
|
591
715
|
})
|
|
592
716
|
return instance.isValid ? instance : null
|
|
593
717
|
}
|
|
@@ -598,7 +722,7 @@ class Metadata extends KeyValueObject {
|
|
|
598
722
|
static merge(toArr, fromArr) {
|
|
599
723
|
(fromArr || []).map((from) => {
|
|
600
724
|
const found = toArr.find((to) => {
|
|
601
|
-
return
|
|
725
|
+
return metadata_isSame(to.key, from.key)
|
|
602
726
|
})
|
|
603
727
|
if (found) {
|
|
604
728
|
found.value = (found.value || []).concat(from.value)
|
|
@@ -609,10 +733,14 @@ class Metadata extends KeyValueObject {
|
|
|
609
733
|
return toArr
|
|
610
734
|
}
|
|
611
735
|
static sameKey(item, key) {
|
|
612
|
-
return
|
|
736
|
+
return metadata_isSame(item.key, key)
|
|
613
737
|
}
|
|
614
738
|
}
|
|
615
739
|
|
|
740
|
+
function metadata_isSame(key1, key2) {
|
|
741
|
+
return stringFormatter(key1, DELIMITER) === stringFormatter(key2, DELIMITER)
|
|
742
|
+
}
|
|
743
|
+
|
|
616
744
|
|
|
617
745
|
|
|
618
746
|
;// ./lib/models/metadata/index.js
|
|
@@ -756,68 +884,112 @@ class Repo {
|
|
|
756
884
|
}
|
|
757
885
|
}
|
|
758
886
|
|
|
759
|
-
|
|
887
|
+
// systemLog is optional
|
|
888
|
+
findAll({ query, systemLog }) {
|
|
889
|
+
const log = _makeLog({
|
|
890
|
+
systemLog,
|
|
891
|
+
label: 'REPO_READ',
|
|
892
|
+
message: `fn ${this._classname}.prototype.findAll`,
|
|
893
|
+
input: [{ query: { ...query }, systemLog: { ...systemLog } }]
|
|
894
|
+
})
|
|
760
895
|
return new Promise((resolve, reject) => {
|
|
761
896
|
this.model.findAll(query, this.queryOptions, (err, data, total) => {
|
|
762
897
|
if (err) {
|
|
898
|
+
log({ level: 'warn', output: err.toString() })
|
|
763
899
|
reject(err)
|
|
764
900
|
} else {
|
|
765
|
-
|
|
901
|
+
const result = {
|
|
766
902
|
isNew: false,
|
|
767
903
|
data,
|
|
768
904
|
total: total || data.length
|
|
769
|
-
}
|
|
905
|
+
}
|
|
906
|
+
log({ level: 'info', output: { ...result } })
|
|
907
|
+
resolve(result)
|
|
770
908
|
}
|
|
771
909
|
})
|
|
772
910
|
})
|
|
773
911
|
}
|
|
774
912
|
|
|
775
|
-
findOne({ query }) {
|
|
913
|
+
findOne({ query, systemLog }) {
|
|
914
|
+
const log = _makeLog({
|
|
915
|
+
systemLog,
|
|
916
|
+
label: 'REPO_READ',
|
|
917
|
+
message: `fn ${this._classname}.prototype.findOne`,
|
|
918
|
+
input: [{ query: { ...query }, systemLog: { ...systemLog } }]
|
|
919
|
+
})
|
|
776
920
|
return new Promise((resolve, reject) => {
|
|
777
921
|
this.model.findAll(query, this.queryOptions, (err, data) => {
|
|
778
922
|
if (err) {
|
|
779
923
|
reject(err)
|
|
780
924
|
} else if (data.length === 1) {
|
|
781
|
-
|
|
925
|
+
const result = {
|
|
782
926
|
isNew: false,
|
|
783
927
|
data,
|
|
784
928
|
total: 1
|
|
785
|
-
}
|
|
929
|
+
}
|
|
930
|
+
log({ level: 'info', output: { ...result } })
|
|
931
|
+
resolve(result)
|
|
786
932
|
} else if (data.length === 0) {
|
|
787
933
|
reject(new Error('record not found'))
|
|
788
934
|
} else {
|
|
789
935
|
reject(new Error('more than one is found'))
|
|
790
936
|
}
|
|
791
937
|
})
|
|
938
|
+
.catch((err) => {
|
|
939
|
+
log({ level: 'warn', output: err.toString() })
|
|
940
|
+
throw err
|
|
941
|
+
})
|
|
792
942
|
})
|
|
793
943
|
}
|
|
794
944
|
|
|
795
|
-
saveAll({ docs }) {
|
|
945
|
+
saveAll({ docs, systemLog }) {
|
|
796
946
|
let isNew
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
947
|
+
const log = _makeLog({
|
|
948
|
+
systemLog,
|
|
949
|
+
label: 'REPO_WRITE',
|
|
950
|
+
message: `fn ${this._classname}.prototype.saveAll`,
|
|
951
|
+
input: [{ docs: [...docs], systemLog: { ...systemLog } }]
|
|
952
|
+
})
|
|
953
|
+
const promise = typeof this.model.saveAll === 'function'
|
|
954
|
+
? this.model.saveAll({ docs })
|
|
955
|
+
: Promise.all(docs.map(async (doc) => {
|
|
956
|
+
if (doc) {
|
|
957
|
+
const result = await this.saveOne({ doc })
|
|
958
|
+
isNew = result.isNew
|
|
959
|
+
const _data = result._data || result.data
|
|
960
|
+
return _data[0]
|
|
961
|
+
}
|
|
962
|
+
return null
|
|
963
|
+
}))
|
|
964
|
+
return promise.then((savedData) => {
|
|
806
965
|
if (savedData.length !== 1) isNew = null
|
|
807
|
-
|
|
966
|
+
const result = {
|
|
808
967
|
data: savedData,
|
|
809
968
|
isNew,
|
|
810
969
|
total: savedData.length
|
|
811
970
|
}
|
|
971
|
+
log({ level: 'info', output: { ...result } })
|
|
972
|
+
return result
|
|
973
|
+
}).catch((err) => {
|
|
974
|
+
log({ level: 'warn', output: err.toString() })
|
|
975
|
+
throw err
|
|
812
976
|
})
|
|
813
977
|
}
|
|
814
978
|
|
|
815
|
-
saveOne({ doc }) {
|
|
979
|
+
saveOne({ doc, systemLog }) {
|
|
980
|
+
const log = _makeLog({
|
|
981
|
+
systemLog,
|
|
982
|
+
label: 'REPO_WRITE',
|
|
983
|
+
message: `fn ${this._classname}.prototype.saveOne`,
|
|
984
|
+
input: [{ doc: { ...doc }, systemLog: { ...systemLog } }]
|
|
985
|
+
})
|
|
816
986
|
return new Promise((resolve, reject) => {
|
|
817
987
|
this.model.saveOne(doc, this.saveOptions, (err, result) => {
|
|
818
988
|
if (err) {
|
|
989
|
+
log({ level: 'warn', output: err.toString() })
|
|
819
990
|
reject(err)
|
|
820
991
|
} else {
|
|
992
|
+
log({ level: 'info', output: { ...result } })
|
|
821
993
|
resolve(result)
|
|
822
994
|
}
|
|
823
995
|
})
|
|
@@ -825,6 +997,25 @@ class Repo {
|
|
|
825
997
|
}
|
|
826
998
|
}
|
|
827
999
|
|
|
1000
|
+
function _makeLog({ systemLog, label, message: message1, input } = {}) {
|
|
1001
|
+
return ({ level, messgae: massage2, output } = {}) => {
|
|
1002
|
+
if (systemLog && systemLog.systemLogHelper) {
|
|
1003
|
+
systemLog.systemLogHelper.log({
|
|
1004
|
+
batchId: systemLog.batchId,
|
|
1005
|
+
label,
|
|
1006
|
+
level,
|
|
1007
|
+
message: massage2 || message1,
|
|
1008
|
+
data: {
|
|
1009
|
+
payload: {
|
|
1010
|
+
input,
|
|
1011
|
+
output
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
})
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
|
|
828
1019
|
|
|
829
1020
|
|
|
830
1021
|
;// ./lib/models/repo/index.js
|
|
@@ -855,16 +1046,16 @@ class Service {
|
|
|
855
1046
|
})
|
|
856
1047
|
}
|
|
857
1048
|
|
|
858
|
-
async findAll({ query = {} } = {}) {
|
|
859
|
-
const result = await this.repo.findAll({ query })
|
|
1049
|
+
async findAll({ query = {}, systemLog } = {}) {
|
|
1050
|
+
const result = await this.repo.findAll({ query, systemLog })
|
|
860
1051
|
return makeApiResponse({
|
|
861
1052
|
repo: this.repo,
|
|
862
1053
|
result
|
|
863
1054
|
})
|
|
864
1055
|
}
|
|
865
1056
|
|
|
866
|
-
async findOne({ query = {} } = {}) {
|
|
867
|
-
const result = await this.repo.findOne({ query })
|
|
1057
|
+
async findOne({ query = {}, systemLog } = {}) {
|
|
1058
|
+
const result = await this.repo.findOne({ query, systemLog })
|
|
868
1059
|
return makeApiResponse({
|
|
869
1060
|
repo: this.repo,
|
|
870
1061
|
result
|
|
@@ -878,21 +1069,21 @@ class Service {
|
|
|
878
1069
|
return arr.map((i) => this.init(i))
|
|
879
1070
|
}
|
|
880
1071
|
|
|
881
|
-
async saveAll({ docs = [] } = {}) {
|
|
1072
|
+
async saveAll({ docs = [], systemLog } = {}) {
|
|
882
1073
|
const copies = docs.map((doc) => {
|
|
883
1074
|
return this.init(doc)
|
|
884
1075
|
})
|
|
885
|
-
const result = await this.repo.saveAll({ docs: copies })
|
|
1076
|
+
const result = await this.repo.saveAll({ docs: copies, systemLog })
|
|
886
1077
|
return makeApiResponse({
|
|
887
1078
|
repo: this.repo,
|
|
888
1079
|
result
|
|
889
1080
|
})
|
|
890
1081
|
}
|
|
891
1082
|
|
|
892
|
-
async saveOne({ doc = {} } = {}) {
|
|
1083
|
+
async saveOne({ doc = {}, systemLog } = {}) {
|
|
893
1084
|
const copy = this.init(doc)
|
|
894
1085
|
if (copy) {
|
|
895
|
-
const result = await this.repo.saveOne({ doc: copy })
|
|
1086
|
+
const result = await this.repo.saveOne({ doc: copy, systemLog })
|
|
896
1087
|
return makeApiResponse({
|
|
897
1088
|
repo: this.repo,
|
|
898
1089
|
result
|
|
@@ -923,6 +1114,54 @@ function makeService({ repo }) {
|
|
|
923
1114
|
|
|
924
1115
|
|
|
925
1116
|
|
|
1117
|
+
;// ./lib/models/uniqueKeyGenerator/uniqueKeyGenerator.js
|
|
1118
|
+
|
|
1119
|
+
|
|
1120
|
+
class UniqueKeyGenerator {
|
|
1121
|
+
static get _classname() {
|
|
1122
|
+
return 'UniqueKeyGenerator'
|
|
1123
|
+
}
|
|
1124
|
+
static get _superclass() {
|
|
1125
|
+
return 'UniqueKeyGenerator'
|
|
1126
|
+
}
|
|
1127
|
+
static makeFormatter({ fieldName, format, options }) {
|
|
1128
|
+
switch (format) {
|
|
1129
|
+
case 'set_code':
|
|
1130
|
+
return _makeSetCode(fieldName, options)
|
|
1131
|
+
default:
|
|
1132
|
+
return _makeSetCode(fieldName, options)
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
static makeGenerator(arr) {
|
|
1136
|
+
const fns = arr.map((item) => this.makeFormatter(item))
|
|
1137
|
+
return async (obj) => {
|
|
1138
|
+
const output = await pReduce(fns, async (acc, fn) => {
|
|
1139
|
+
const _obj = await fn(obj)
|
|
1140
|
+
return Object.assign(acc, _obj)
|
|
1141
|
+
}, obj)
|
|
1142
|
+
return output
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
function _makeSetCode(fieldName, options) {
|
|
1148
|
+
return async (obj = {}) => {
|
|
1149
|
+
if (obj[fieldName]) {
|
|
1150
|
+
return {}
|
|
1151
|
+
}
|
|
1152
|
+
return {
|
|
1153
|
+
[fieldName]: stringHelper.setCode()
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
|
|
1159
|
+
|
|
1160
|
+
;// ./lib/models/uniqueKeyGenerator/index.js
|
|
1161
|
+
|
|
1162
|
+
|
|
1163
|
+
|
|
1164
|
+
|
|
926
1165
|
;// ./lib/models/index.js
|
|
927
1166
|
|
|
928
1167
|
|
|
@@ -931,6 +1170,193 @@ function makeService({ repo }) {
|
|
|
931
1170
|
|
|
932
1171
|
|
|
933
1172
|
|
|
1173
|
+
|
|
1174
|
+
;// ./lib/helpers/generalPost/generalPost.js
|
|
1175
|
+
|
|
1176
|
+
|
|
1177
|
+
|
|
1178
|
+
|
|
1179
|
+
async function generalPost({ body = {}, GeneralModel, UniqueKeyGenerator, resourceInfo }) {
|
|
1180
|
+
const { resources, data, globalShared = {}, shared = {}, relationship = {} } = body
|
|
1181
|
+
const _resourceInfo = resourceInfo || body.resourceInfo
|
|
1182
|
+
_attachShared(data, globalShared, shared)
|
|
1183
|
+
const obj = await pReduce(resources, async (acc, resource) => {
|
|
1184
|
+
const service = _makeService(resource, _resourceInfo, UniqueKeyGenerator, GeneralModel)
|
|
1185
|
+
_createRelationship(data, relationship[resource], acc)
|
|
1186
|
+
const _data = data[resource]
|
|
1187
|
+
const result = await service.saveAll({ docs: [].concat(_data) })
|
|
1188
|
+
acc[resource] = Array.isArray(_data) ? result._data : result._data[0]
|
|
1189
|
+
return acc
|
|
1190
|
+
}, {})
|
|
1191
|
+
return obj
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
function _attachShared(data, globalShared = {}, shared = {}) {
|
|
1195
|
+
Object.keys(shared).forEach((key) => {
|
|
1196
|
+
const _data = data[key]
|
|
1197
|
+
data[key] = objectHelper.merge({}, _data, globalShared, shared[key] || {})
|
|
1198
|
+
})
|
|
1199
|
+
}
|
|
1200
|
+
|
|
1201
|
+
function _createRelationship(data, relationship = {}, object) {
|
|
1202
|
+
Object.keys(relationship).forEach((key) => {
|
|
1203
|
+
const path = relationship[key]
|
|
1204
|
+
const val = objectHelper.get(object, path)
|
|
1205
|
+
objectHelper.set(data, key, val)
|
|
1206
|
+
})
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
function _makeService(resource, resourceInfo, UniqueKeyGenerator, GeneralModel) {
|
|
1210
|
+
const { collectionName, fields } = resourceInfo[resource]
|
|
1211
|
+
const uniqueKeyGenerator = UniqueKeyGenerator.makeGenerator(fields)
|
|
1212
|
+
const model = new GeneralModel({ collectionName, uniqueKeyGenerator })
|
|
1213
|
+
return makeService({
|
|
1214
|
+
repo: new Repo({ model })
|
|
1215
|
+
})
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
|
|
1219
|
+
|
|
1220
|
+
;// ./lib/helpers/generalPost/index.js
|
|
1221
|
+
|
|
1222
|
+
|
|
1223
|
+
|
|
1224
|
+
|
|
1225
|
+
;// ./lib/helpers/padZeros/padZeros.js
|
|
1226
|
+
function padZeros(num, minLength = 6) {
|
|
1227
|
+
num = num.toString()
|
|
1228
|
+
if (num.length < minLength) {
|
|
1229
|
+
return padZeros('0' + num, minLength)
|
|
1230
|
+
}
|
|
1231
|
+
return num
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
|
|
1235
|
+
|
|
1236
|
+
;// ./lib/helpers/padZeros/index.js
|
|
1237
|
+
|
|
1238
|
+
|
|
1239
|
+
|
|
1240
|
+
|
|
1241
|
+
;// ./lib/helpers/pReduce/index.js
|
|
1242
|
+
|
|
1243
|
+
|
|
1244
|
+
|
|
1245
|
+
|
|
1246
|
+
;// ./lib/helpers/stringFormatter/index.js
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
|
|
1250
|
+
|
|
1251
|
+
;// ./lib/helpers/stringHelper/stringHelper.js
|
|
1252
|
+
function baseXEncode(num, base = 34) {
|
|
1253
|
+
const charset = getBaseCharset(base)
|
|
1254
|
+
return encode(num, charset)
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
function encode(int, charset) {
|
|
1258
|
+
let byCode = charset.byCode;
|
|
1259
|
+
if (int === 0) {
|
|
1260
|
+
return byCode[0];
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
var res = "",
|
|
1264
|
+
max = charset.length;
|
|
1265
|
+
while (int > 0) {
|
|
1266
|
+
res = byCode[int % max] + res;
|
|
1267
|
+
int = Math.floor(int / max);
|
|
1268
|
+
}
|
|
1269
|
+
return res;
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
function getBaseCharset(base) {
|
|
1273
|
+
let charset = '9876543210ABCDEFGHJKLMNPQRSTUVWXYZ'
|
|
1274
|
+
if (base === 58) {
|
|
1275
|
+
charset = '9876543210ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz'
|
|
1276
|
+
}
|
|
1277
|
+
return indexCharset(charset)
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
function indexCharset(str) {
|
|
1281
|
+
var byCode = {},
|
|
1282
|
+
byChar = {},
|
|
1283
|
+
length = str.length,
|
|
1284
|
+
i, char;
|
|
1285
|
+
for (i = 0; i < length; i++) {
|
|
1286
|
+
char = str[i];
|
|
1287
|
+
byCode[i] = char;
|
|
1288
|
+
byChar[char] = i;
|
|
1289
|
+
}
|
|
1290
|
+
return { byCode: byCode, byChar: byChar, length: length };
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
function randomString({ len = 16, pattern = 'a1' } = {}) {
|
|
1294
|
+
const A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
1295
|
+
const a = 'abcdefghijklmnopqrstuvwxyz'
|
|
1296
|
+
const num = '1234567890'
|
|
1297
|
+
const mark = '~!@#$%^&*_+-='
|
|
1298
|
+
let str = ''
|
|
1299
|
+
if (pattern.includes('A')) {
|
|
1300
|
+
str += A
|
|
1301
|
+
}
|
|
1302
|
+
if (pattern.includes('a')) {
|
|
1303
|
+
str += a
|
|
1304
|
+
}
|
|
1305
|
+
if (pattern.includes('1')) {
|
|
1306
|
+
str += num
|
|
1307
|
+
}
|
|
1308
|
+
if (pattern.includes('#')) {
|
|
1309
|
+
str += mark
|
|
1310
|
+
}
|
|
1311
|
+
const chars = [...str]
|
|
1312
|
+
return [...Array(len)].map(i => {
|
|
1313
|
+
return chars[(Math.random() * chars.length) | 0]
|
|
1314
|
+
}).join``
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
function reverse(str) {
|
|
1318
|
+
if (typeof str !== 'string') {
|
|
1319
|
+
str = str.toString()
|
|
1320
|
+
}
|
|
1321
|
+
const splitString = str.split('')
|
|
1322
|
+
const reverseArray = splitString.reverse()
|
|
1323
|
+
return reverseArray.join('')
|
|
1324
|
+
}
|
|
1325
|
+
|
|
1326
|
+
function setCode(base = 34) {
|
|
1327
|
+
const now = (new Date()).valueOf()
|
|
1328
|
+
const random = randomString({
|
|
1329
|
+
len: 8,
|
|
1330
|
+
pattern: '1'
|
|
1331
|
+
})
|
|
1332
|
+
const str = reverse(`${now}${random}`)
|
|
1333
|
+
// const str = `${now}${random}`
|
|
1334
|
+
return baseXEncode(str, base)
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
const stringHelper = {
|
|
1338
|
+
setCode
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
|
|
1342
|
+
;// ./lib/helpers/stringHelper/index.js
|
|
1343
|
+
|
|
1344
|
+
|
|
1345
|
+
|
|
1346
|
+
|
|
1347
|
+
|
|
1348
|
+
;// ./lib/helpers/index.js
|
|
1349
|
+
|
|
1350
|
+
|
|
1351
|
+
|
|
1352
|
+
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
|
|
1356
|
+
|
|
1357
|
+
|
|
1358
|
+
|
|
1359
|
+
|
|
934
1360
|
;// ./lib/index.js
|
|
935
1361
|
|
|
936
1362
|
|
|
@@ -944,12 +1370,17 @@ var __webpack_exports__Metadata = __webpack_exports__.OS;
|
|
|
944
1370
|
var __webpack_exports__QMeta = __webpack_exports__.Z8;
|
|
945
1371
|
var __webpack_exports__Repo = __webpack_exports__.lc;
|
|
946
1372
|
var __webpack_exports__Service = __webpack_exports__.kl;
|
|
1373
|
+
var __webpack_exports__UniqueKeyGenerator = __webpack_exports__._x;
|
|
947
1374
|
var __webpack_exports__convertString = __webpack_exports__.l0;
|
|
948
1375
|
var __webpack_exports__formatDate = __webpack_exports__.Yq;
|
|
1376
|
+
var __webpack_exports__generalPost = __webpack_exports__.zn;
|
|
949
1377
|
var __webpack_exports__getValidation = __webpack_exports__.G8;
|
|
950
1378
|
var __webpack_exports__getValueByKeys = __webpack_exports__.pY;
|
|
951
1379
|
var __webpack_exports__makeApiResponse = __webpack_exports__.su;
|
|
952
1380
|
var __webpack_exports__makeService = __webpack_exports__.Q6;
|
|
1381
|
+
var __webpack_exports__objectHelper = __webpack_exports__.UI;
|
|
1382
|
+
var __webpack_exports__pReduce = __webpack_exports__.d;
|
|
953
1383
|
var __webpack_exports__padZeros = __webpack_exports__.Lv;
|
|
954
1384
|
var __webpack_exports__stringFormatter = __webpack_exports__.Qy;
|
|
955
|
-
|
|
1385
|
+
var __webpack_exports__stringHelper = __webpack_exports__.yO;
|
|
1386
|
+
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 };
|