w-orm-mongodb 1.1.43 → 1.1.44

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.
@@ -599,6 +599,192 @@ describe('insert', function() {
599
599
  })
600
600
 
601
601
 
602
+ describe('insert returnList', function() {
603
+ let vans = {}
604
+ let vget = {}
605
+
606
+ before(async function() {
607
+ this.timeout(120000)
608
+
609
+ let cl = 'inslist'
610
+ let wo = WOrm(genOpt(cl))
611
+ await wo.delAll()
612
+
613
+ //預設不給option時之形狀須為單一聚合物件
614
+ vget[1] = await wo.insert([{ id: 'r1' }, { id: 'r2' }])
615
+
616
+ //明確給false時形狀同預設
617
+ vget[2] = await wo.insert({ id: 'r3' }, { returnList: false })
618
+
619
+ //開啟後須回與輸入等長且保序之陣列, 全新者nInserted皆為1
620
+ vget[3] = await wo.insert([{ id: 'r4' }, { id: 'r5' }, { id: 'r6' }], { returnList: true })
621
+
622
+ //對位正確: 中間那筆已存在, 僅該位置之nInserted為0
623
+ vget[4] = await wo.insert([
624
+ { id: 'r7' },
625
+ { id: 'r5' }, //已存在
626
+ { id: 'r8' },
627
+ ], { returnList: true })
628
+
629
+ //同批含重複主鍵, 僅首筆之nInserted為1
630
+ vget[5] = await wo.insert([
631
+ { id: 'r9' },
632
+ { id: 'r9' },
633
+ { id: 'r9' },
634
+ ], { returnList: true })
635
+
636
+ //不變式: filter計數須等於聚合模式之nInserted
637
+ await wo.delAll()
638
+ let rsIn = [
639
+ { id: 's1' },
640
+ { id: 's2' },
641
+ { id: 's3' },
642
+ { id: 's4' },
643
+ ]
644
+ await wo.insert([{ id: 's2' }, { id: 's4' }]) //先寫入其中2筆
645
+ let rl = await wo.insert(rsIn, { returnList: true })
646
+ await wo.delAll()
647
+ await wo.insert([{ id: 's2' }, { id: 's4' }])
648
+ let rg = await wo.insert(rsIn)
649
+ vget[6] = rl.filter((v) => v.nInserted === 1).length
650
+ vget[7] = rg.nInserted
651
+ vget[8] = vget[6] === vget[7]
652
+
653
+ //逐筆元素之鍵集合須恰為n,nInserted,ok
654
+ vget[9] = rl.map(genKeys)
655
+
656
+ //輸入無效時開啟者回空陣列, 未開啟者回聚合物件
657
+ vget[10] = await wo.insert(null, { returnList: true })
658
+ vget[11] = await wo.insert(null)
659
+
660
+ //單一物件輸入亦須回長度為1之陣列
661
+ await wo.delAll()
662
+ vget[12] = await wo.insert({ id: 't1' }, { returnList: true })
663
+
664
+ //全數已存在時, 各元素之nInserted皆為0且不得reject
665
+ vget[13] = await wo.insert({ id: 't1' }, { returnList: true })
666
+
667
+ //change事件之res即本次實際回傳值
668
+ let woEv = WOrm(genOpt('inslistev'))
669
+ await woEv.delAll()
670
+ let resEv = null
671
+ woEv.on('change', function(mode, data, res) {
672
+ resEv = res
673
+ })
674
+ let rEv = await woEv.insert([{ id: 'v1' }, { id: 'v2' }], { returnList: true })
675
+ vget[14] = JSON.stringify(resEv) === JSON.stringify(rEv)
676
+
677
+ //autoGenPk為false且未帶id者仍為整批reject, 不因returnList而降為逐筆
678
+ let woOff = WOrm(genOptPk('inslistpkoff', false))
679
+ await woOff.delAll()
680
+ let rtOff = null
681
+ await woOff.insert([{ id: 'w1' }, { name: 'no-id' }], { returnList: true })
682
+ .then(function(msg) {
683
+ rtOff = `不應resolve: ${JSON.stringify(msg)}`
684
+ })
685
+ .catch(function(msg) {
686
+ rtOff = msg.toString()
687
+ })
688
+ vget[15] = rtOff
689
+ vget[16] = await woOff.selectByPk('w1')
690
+
691
+ })
692
+
693
+ vans[1] = { n: 2, nInserted: 2, ok: 1 }
694
+ it(`should get ${JSON.stringify(vans[1])} for insert without option`, async function() {
695
+ assert.strict.deepStrictEqual(vget[1], vans[1])
696
+ })
697
+
698
+ vans[2] = { n: 1, nInserted: 1, ok: 1 }
699
+ it(`should get ${JSON.stringify(vans[2])} for insert with returnList=false`, async function() {
700
+ assert.strict.deepStrictEqual(vget[2], vans[2])
701
+ })
702
+
703
+ vans[3] = [
704
+ { n: 1, nInserted: 1, ok: 1 },
705
+ { n: 1, nInserted: 1, ok: 1 },
706
+ { n: 1, nInserted: 1, ok: 1 }
707
+ ]
708
+ it(`should get ${JSON.stringify(vans[3])} for insert with returnList=true`, async function() {
709
+ assert.strict.deepStrictEqual(vget[3], vans[3])
710
+ })
711
+
712
+ vans[4] = [
713
+ { n: 1, nInserted: 1, ok: 1 },
714
+ { n: 1, nInserted: 0, ok: 1 },
715
+ { n: 1, nInserted: 1, ok: 1 }
716
+ ]
717
+ it(`should get ${JSON.stringify(vans[4])} for keeping order and position in returnList`, async function() {
718
+ assert.strict.deepStrictEqual(vget[4], vans[4])
719
+ })
720
+
721
+ vans[5] = [
722
+ { n: 1, nInserted: 1, ok: 1 },
723
+ { n: 1, nInserted: 0, ok: 1 },
724
+ { n: 1, nInserted: 0, ok: 1 }
725
+ ]
726
+ it(`should get ${JSON.stringify(vans[5])} for only first one inserted by duplicated id in same batch`, async function() {
727
+ assert.strict.deepStrictEqual(vget[5], vans[5])
728
+ })
729
+
730
+ vans[6] = 2
731
+ it(`should get ${JSON.stringify(vans[6])} for count of nInserted===1 in returnList`, async function() {
732
+ assert.strict.deepStrictEqual(vget[6], vans[6])
733
+ })
734
+
735
+ vans[7] = 2
736
+ it(`should get ${JSON.stringify(vans[7])} for nInserted in aggregated mode`, async function() {
737
+ assert.strict.deepStrictEqual(vget[7], vans[7])
738
+ })
739
+
740
+ vans[8] = true
741
+ it(`should get ${JSON.stringify(vans[8])} for same count between returnList and aggregated mode`, async function() {
742
+ assert.strict.deepStrictEqual(vget[8], vans[8])
743
+ })
744
+
745
+ vans[9] = ['n,nInserted,ok', 'n,nInserted,ok', 'n,nInserted,ok', 'n,nInserted,ok']
746
+ it(`should get ${JSON.stringify(vans[9])} for key set of returnList element`, async function() {
747
+ assert.strict.deepStrictEqual(vget[9], vans[9])
748
+ })
749
+
750
+ vans[10] = []
751
+ it(`should get ${JSON.stringify(vans[10])} for invalid data with returnList=true`, async function() {
752
+ assert.strict.deepStrictEqual(vget[10], vans[10])
753
+ })
754
+
755
+ vans[11] = { n: 0, nInserted: 0, ok: 1 }
756
+ it(`should get ${JSON.stringify(vans[11])} for invalid data with returnList=false`, async function() {
757
+ assert.strict.deepStrictEqual(vget[11], vans[11])
758
+ })
759
+
760
+ vans[12] = [{ n: 1, nInserted: 1, ok: 1 }]
761
+ it(`should get ${JSON.stringify(vans[12])} for single object with returnList=true`, async function() {
762
+ assert.strict.deepStrictEqual(vget[12], vans[12])
763
+ })
764
+
765
+ vans[13] = [{ n: 1, nInserted: 0, ok: 1 }]
766
+ it(`should get ${JSON.stringify(vans[13])} for all existed with returnList=true`, async function() {
767
+ assert.strict.deepStrictEqual(vget[13], vans[13])
768
+ })
769
+
770
+ vans[14] = true
771
+ it(`should get ${JSON.stringify(vans[14])} for res of change event being actual return value`, async function() {
772
+ assert.strict.deepStrictEqual(vget[14], vans[14])
773
+ })
774
+
775
+ vans[15] = 'Error: invalid data[1].id, autoGenPk is false'
776
+ it(`should get ${JSON.stringify(vans[15])} for autoGenPk=false with returnList=true`, async function() {
777
+ assert.strict.deepStrictEqual(vget[15], vans[15])
778
+ })
779
+
780
+ vans[16] = null
781
+ it(`should get ${JSON.stringify(vans[16])} for not writing valid records in rejected returnList batch`, async function() {
782
+ assert.strict.deepStrictEqual(vget[16], vans[16])
783
+ })
784
+
785
+ })
786
+
787
+
602
788
  //insertBulk之語義與insert不同: insert跳過已存在者而整批ok為1, insertBulk則整批reject且不寫入任何一筆
603
789
  //本檔之容器為standalone, 無交易可用, 故走補償動作路徑; 交易路徑另見api-insertbulk-rs.test.mjs
604
790
 
@@ -7,7 +7,7 @@ let fdTar = './dist'
7
7
 
8
8
 
9
9
  rollupFiles({
10
- fns: getFiles(fdSrc),
10
+ fns: 'WOrmMongodb.mjs',
11
11
  fdSrc,
12
12
  fdTar,
13
13
  nameDistType: 'kebabCase',