w-orm-lmdb 1.0.17 → 1.0.18

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.
@@ -74,13 +74,27 @@
74
74
  <dl class="details">
75
75
 
76
76
  <dt class="tag-description">Description:</dt>
77
- <dd class="tag-description"><ul class="dummy"><li><p>操作資料庫(LMDB)</p></li></ul></dd>
77
+ <dd class="tag-description"><ul class="dummy"><li><p>操作資料庫(LMDB)</p>
78
+ <p>回傳物件為EventEmitter,除各操作函數外另發出change與error兩事件,供呼叫端於單一處集中觀察資料異動與失敗。
79
+ 事件僅為附加通知,其所送出之資訊皆另有正規管道(操作結果經resolve、整批性錯誤經reject、逐筆失敗經該筆之err欄位),
80
+ 故不監聽亦能取得完整資訊,且監聽與否不改變任何操作之回傳值。</p>
81
+ <p>change事件,參數為(mode, data, res),於資料實際異動成功後發出:
82
+ mode為操作別字串,可為'insert'、'save'、'del'、'delAll';save內若逐筆走自動插入則該筆另發出mode為'insert'之事件。
83
+ data為本次操作之輸入數據,delAll固定為null。
84
+ res為本次操作之回傳結果。
85
+ 逐筆函數以整批為單位發出一次而不逐筆發出,select與selectByPk不發出本事件。</p>
86
+ <p>error事件,參數為(mode, data, err),於操作發生錯誤時發出:
87
+ mode為操作別字串,可為'select'、'selectByPk'、'insert'、'save'、'del'、'delAll'。
88
+ data為本次操作之輸入數據,無輸入數據者為null。
89
+ err為錯誤訊息字串,內容與正規管道所送出者一致。
90
+ 整批性錯誤於reject之前發出;逐筆失敗於該筆結果定案後發出,每筆一次。
91
+ 註: 逐筆失敗時整批仍resolve,故收到本事件不表示該次呼叫失敗;正常結果(如查無數據、主鍵未命中、全數已存在)不發出本事件。</p></li></ul></dd>
78
92
 
79
93
 
80
94
 
81
95
  <dt class="tag-source">Source:</dt>
82
96
  <dd class="tag-source"><ul class="dummy"><li>
83
- <a href="WOrmLmdb.mjs.html">WOrmLmdb.mjs</a>, <a href="WOrmLmdb.mjs.html#line35">line 35</a>
97
+ <a href="WOrmLmdb.mjs.html">WOrmLmdb.mjs</a>, <a href="WOrmLmdb.mjs.html#line52">line 52</a>
84
98
  </li></ul></dd>
85
99
 
86
100
 
@@ -509,7 +523,7 @@
509
523
 
510
524
  <dt class="tag-source">Source:</dt>
511
525
  <dd class="tag-source"><ul class="dummy"><li>
512
- <a href="WOrmLmdb.mjs.html">WOrmLmdb.mjs</a>, <a href="WOrmLmdb.mjs.html#line580">line 580</a>
526
+ <a href="WOrmLmdb.mjs.html">WOrmLmdb.mjs</a>, <a href="WOrmLmdb.mjs.html#line620">line 620</a>
513
527
  </li></ul></dd>
514
528
 
515
529
 
@@ -670,7 +684,7 @@
670
684
 
671
685
  <dt class="tag-source">Source:</dt>
672
686
  <dd class="tag-source"><ul class="dummy"><li>
673
- <a href="WOrmLmdb.mjs.html">WOrmLmdb.mjs</a>, <a href="WOrmLmdb.mjs.html#line704">line 704</a>
687
+ <a href="WOrmLmdb.mjs.html">WOrmLmdb.mjs</a>, <a href="WOrmLmdb.mjs.html#line751">line 751</a>
674
688
  </li></ul></dd>
675
689
 
676
690
 
@@ -848,7 +862,7 @@
848
862
 
849
863
  <dt class="tag-source">Source:</dt>
850
864
  <dd class="tag-source"><ul class="dummy"><li>
851
- <a href="WOrmLmdb.mjs.html">WOrmLmdb.mjs</a>, <a href="WOrmLmdb.mjs.html#line277">line 277</a>
865
+ <a href="WOrmLmdb.mjs.html">WOrmLmdb.mjs</a>, <a href="WOrmLmdb.mjs.html#line325">line 325</a>
852
866
  </li></ul></dd>
853
867
 
854
868
 
@@ -1009,7 +1023,7 @@
1009
1023
 
1010
1024
  <dt class="tag-source">Source:</dt>
1011
1025
  <dd class="tag-source"><ul class="dummy"><li>
1012
- <a href="WOrmLmdb.mjs.html">WOrmLmdb.mjs</a>, <a href="WOrmLmdb.mjs.html#line375">line 375</a>
1026
+ <a href="WOrmLmdb.mjs.html">WOrmLmdb.mjs</a>, <a href="WOrmLmdb.mjs.html#line419">line 419</a>
1013
1027
  </li></ul></dd>
1014
1028
 
1015
1029
 
@@ -1296,7 +1310,7 @@
1296
1310
 
1297
1311
  <dt class="tag-source">Source:</dt>
1298
1312
  <dd class="tag-source"><ul class="dummy"><li>
1299
- <a href="WOrmLmdb.mjs.html">WOrmLmdb.mjs</a>, <a href="WOrmLmdb.mjs.html#line174">line 174</a>
1313
+ <a href="WOrmLmdb.mjs.html">WOrmLmdb.mjs</a>, <a href="WOrmLmdb.mjs.html#line214">line 214</a>
1300
1314
  </li></ul></dd>
1301
1315
 
1302
1316
 
@@ -1475,7 +1489,7 @@
1475
1489
 
1476
1490
  <dt class="tag-source">Source:</dt>
1477
1491
  <dd class="tag-source"><ul class="dummy"><li>
1478
- <a href="WOrmLmdb.mjs.html">WOrmLmdb.mjs</a>, <a href="WOrmLmdb.mjs.html#line229">line 229</a>
1492
+ <a href="WOrmLmdb.mjs.html">WOrmLmdb.mjs</a>, <a href="WOrmLmdb.mjs.html#line273">line 273</a>
1479
1493
  </li></ul></dd>
1480
1494
 
1481
1495
 
@@ -1632,7 +1646,7 @@
1632
1646
  <br class="clear">
1633
1647
 
1634
1648
  <footer>
1635
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 07:05:30 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
1649
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 08:38:27 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
1636
1650
  </footer>
1637
1651
 
1638
1652
  <script>prettyPrint();</script>
@@ -70,6 +70,23 @@ import waitFun from 'wsemi/src/waitFun.mjs'
70
70
  /**
71
71
  * 操作資料庫(LMDB)
72
72
  *
73
+ * 回傳物件為EventEmitter,除各操作函數外另發出change與error兩事件,供呼叫端於單一處集中觀察資料異動與失敗。
74
+ * 事件僅為附加通知,其所送出之資訊皆另有正規管道(操作結果經resolve、整批性錯誤經reject、逐筆失敗經該筆之err欄位),
75
+ * 故不監聽亦能取得完整資訊,且監聽與否不改變任何操作之回傳值。
76
+ *
77
+ * change事件,參數為(mode, data, res),於資料實際異動成功後發出:
78
+ * mode為操作別字串,可為'insert'、'save'、'del'、'delAll';save內若逐筆走自動插入則該筆另發出mode為'insert'之事件。
79
+ * data為本次操作之輸入數據,delAll固定為null。
80
+ * res為本次操作之回傳結果。
81
+ * 逐筆函數以整批為單位發出一次而不逐筆發出,select與selectByPk不發出本事件。
82
+ *
83
+ * error事件,參數為(mode, data, err),於操作發生錯誤時發出:
84
+ * mode為操作別字串,可為'select'、'selectByPk'、'insert'、'save'、'del'、'delAll'。
85
+ * data為本次操作之輸入數據,無輸入數據者為null。
86
+ * err為錯誤訊息字串,內容與正規管道所送出者一致。
87
+ * 整批性錯誤於reject之前發出;逐筆失敗於該筆結果定案後發出,每筆一次。
88
+ * 註: 逐筆失敗時整批仍resolve,故收到本事件不表示該次呼叫失敗;正常結果(如查無數據、主鍵未命中、全數已存在)不發出本事件。
89
+ *
73
90
  * @class
74
91
  * @param {Object} [opt={}] 輸入設定物件,預設{}
75
92
  * @param {String} [opt.url='_db'] 輸入資料庫用資料夾字串,預設'_db'
@@ -165,7 +182,30 @@ function WOrmLmdb(opt = {}) {
165
182
  })
166
183
  }
167
184
 
168
- //getErrMsg, 取錯誤訊息字串, 供逐筆結果之err欄位使用
185
+ //emitChange, 資料實際異動成功後發出, 事件僅為附加通知不承擔回傳義務
186
+ //一律包try/catch, 令訂閱函數自身拋錯不影響本次操作之結果
187
+ let emitChange = (mode, data, res) => {
188
+ try {
189
+ ee.emit('change', mode, data, res)
190
+ }
191
+ catch (err) {
192
+ console.log(err)
193
+ }
194
+ }
195
+
196
+ //emitError, 操作發生錯誤時發出, 錯誤訊息一律轉為字串
197
+ //此try/catch除攔訂閱函數之例外外另有必要: Node之EventEmitter於'error'無監聽者時會將錯誤直接拋出,
198
+ //若不攔則同一次操作會因呼叫端有無註冊監聽而走向不同結果
199
+ let emitError = (mode, data, err) => {
200
+ try {
201
+ ee.emit('error', mode, data, getErrMsg(err))
202
+ }
203
+ catch (errEmit) {
204
+ console.log(errEmit)
205
+ }
206
+ }
207
+
208
+ //getErrMsg, 取錯誤訊息字串, 供逐筆結果之err欄位與error事件使用
169
209
  let getErrMsg = (err) => {
170
210
  let m = get(err, 'message')
171
211
  if (isestr(m)) {
@@ -259,6 +299,10 @@ function WOrmLmdb(opt = {}) {
259
299
 
260
300
  //check
261
301
  if (isErr) {
302
+
303
+ //emit, 整批性錯誤須於reject之前發出
304
+ emitError('select', null, res)
305
+
262
306
  return Promise.reject(res)
263
307
  }
264
308
 
@@ -308,6 +352,10 @@ function WOrmLmdb(opt = {}) {
308
352
 
309
353
  //check
310
354
  if (isErr) {
355
+
356
+ //emit, 整批性錯誤須於reject之前發出
357
+ emitError('selectByPk', null, res)
358
+
311
359
  return Promise.reject(res)
312
360
  }
313
361
 
@@ -391,19 +439,15 @@ function WOrmLmdb(opt = {}) {
391
439
 
392
440
  //emit, 於change可能須使用select, 故須放在重設快取之後
393
441
  if (!isErr) {
394
- try {
395
-
396
- //emit
397
- ee.emit('change', 'insert', data, res)
398
-
399
- }
400
- catch (err) {
401
- console.log(err)
402
- }
442
+ emitChange('insert', data, res)
403
443
  }
404
444
 
405
445
  //check
406
446
  if (isErr) {
447
+
448
+ //emit, 整批性錯誤須於reject之前發出
449
+ emitError('insert', data, res)
450
+
407
451
  return Promise.reject(res)
408
452
  }
409
453
 
@@ -572,16 +616,15 @@ function WOrmLmdb(opt = {}) {
572
616
  //update, 不能保證插入多少, 一律重設快取
573
617
  _cache = null
574
618
 
575
- try {
619
+ //emit
620
+ emitChange('insert', [v], rest)
576
621
 
577
- //emit
578
- ee.emit('change', 'insert', [v], rest)
579
-
580
- }
581
- catch (err) {
582
- console.log(err)
583
- }
622
+ }
584
623
 
624
+ //emit, 逐筆失敗於該筆結果定案後發出, 每筆一次
625
+ //此事件之發出不表示整批失敗, 整批仍resolve, 該筆以ok為0回報
626
+ if (rest.ok === 0) {
627
+ emitError('save', [v], rest.err)
585
628
  }
586
629
 
587
630
  return rest
@@ -597,20 +640,17 @@ function WOrmLmdb(opt = {}) {
597
640
  _cache = null
598
641
 
599
642
  //emit, 於change可能須使用select, 故須放在重設快取之後
643
+ //逐筆失敗之error事件已於各筆定案時發出, 故必早於本整批change
600
644
  if (!isErr) {
601
- try {
602
-
603
- //emit
604
- ee.emit('change', 'save', data, res)
605
-
606
- }
607
- catch (err) {
608
- console.log(err)
609
- }
645
+ emitChange('save', data, res)
610
646
  }
611
647
 
612
648
  //check
613
649
  if (isErr) {
650
+
651
+ //emit, 整批性錯誤須於reject之前發出
652
+ emitError('save', data, res)
653
+
614
654
  return Promise.reject(res)
615
655
  }
616
656
 
@@ -659,38 +699,42 @@ function WOrmLmdb(opt = {}) {
659
699
  if (!isestr(id)) {
660
700
  //未給有效v.id視為該筆數據有問題而無法處理, 以ok為0並附err回報,
661
701
  //與[已給v.id但查無數據]之ok為1有別, 二者須可由ok分辨
662
- return {
702
+ //註: 此處不可直接return, 否則會跳過本回調末尾之error事件發出
703
+ rest = {
663
704
  n: 0,
664
705
  nDeleted: 0,
665
706
  ok: 0,
666
707
  err: `can not delete by invalid id[${id}]`,
667
708
  }
668
709
  }
710
+ else {
669
711
 
670
- //查找資料表內v.id
671
- let vv = await getValue(id) //不會有catch
712
+ //查找資料表內v.id
713
+ let vv = await getValue(id) //不會有catch
672
714
 
673
- //check
674
- if (iseobj(vv)) {
675
- //已存在v.id則須刪除
715
+ //check
716
+ if (iseobj(vv)) {
717
+ //已存在v.id則須刪除
676
718
 
677
- //del
678
- await client.del(id)
719
+ //del
720
+ await client.del(id)
721
+
722
+ //rest
723
+ rest = {
724
+ n: 1,
725
+ nDeleted: 1,
726
+ ok: 1,
727
+ }
679
728
 
680
- //rest
681
- rest = {
682
- n: 1,
683
- nDeleted: 1,
684
- ok: 1,
685
729
  }
730
+ else {
731
+ //不存在v.id則不刪除, 未命中故n為0
732
+ rest = {
733
+ n: 0,
734
+ nDeleted: 0,
735
+ ok: 1,
736
+ }
686
737
 
687
- }
688
- else {
689
- //不存在v.id則不刪除, 未命中故n為0
690
- rest = {
691
- n: 0,
692
- nDeleted: 0,
693
- ok: 1,
694
738
  }
695
739
 
696
740
  }
@@ -708,6 +752,12 @@ function WOrmLmdb(opt = {}) {
708
752
 
709
753
  }
710
754
 
755
+ //emit, 逐筆失敗於該筆結果定案後發出, 每筆一次
756
+ //此事件之發出不表示整批失敗, 整批仍resolve, 該筆以ok為0回報
757
+ if (rest.ok === 0) {
758
+ emitError('del', [v], rest.err)
759
+ }
760
+
711
761
  return rest
712
762
  })
713
763
 
@@ -721,20 +771,17 @@ function WOrmLmdb(opt = {}) {
721
771
  _cache = null
722
772
 
723
773
  //emit, 於change可能須使用select, 故須放在重設快取之後
774
+ //逐筆失敗之error事件已於各筆定案時發出, 故必早於本整批change
724
775
  if (!isErr) {
725
- try {
726
-
727
- //emit
728
- ee.emit('change', 'del', data, res)
729
-
730
- }
731
- catch (err) {
732
- console.log(err)
733
- }
776
+ emitChange('del', data, res)
734
777
  }
735
778
 
736
779
  //check
737
780
  if (isErr) {
781
+
782
+ //emit, 整批性錯誤須於reject之前發出
783
+ emitError('del', data, res)
784
+
738
785
  return Promise.reject(res)
739
786
  }
740
787
 
@@ -836,19 +883,15 @@ function WOrmLmdb(opt = {}) {
836
883
 
837
884
  //emit, 於change可能須使用select, 故須放在重設快取之後
838
885
  if (!isErr) {
839
- try {
840
-
841
- //emit
842
- ee.emit('change', 'delAll', null, res)
843
-
844
- }
845
- catch (err) {
846
- console.log(err)
847
- }
886
+ emitChange('delAll', null, res)
848
887
  }
849
888
 
850
889
  //check
851
890
  if (isErr) {
891
+
892
+ //emit, 整批性錯誤須於reject之前發出
893
+ emitError('delAll', null, res)
894
+
852
895
  return Promise.reject(res)
853
896
  }
854
897
 
@@ -890,7 +933,7 @@ export default WOrmLmdb
890
933
  <br class="clear">
891
934
 
892
935
  <footer>
893
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 07:05:30 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
936
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 08:38:27 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
894
937
  </footer>
895
938
 
896
939
  <script>prettyPrint();</script>
package/docs/index.html CHANGED
@@ -71,7 +71,7 @@
71
71
  <br class="clear">
72
72
 
73
73
  <footer>
74
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 07:05:30 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
74
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 08:38:27 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
75
75
  </footer>
76
76
 
77
77
  <script>prettyPrint();</script>
package/g-basic.mjs CHANGED
@@ -62,6 +62,9 @@ async function test() {
62
62
  wo.on('change', function(mode, data, res) {
63
63
  console.log('change', mode)
64
64
  })
65
+ wo.on('error', function(mode, data, err) {
66
+ console.log('error', mode, err)
67
+ })
65
68
 
66
69
  //delAll
67
70
  await wo.delAll()
@@ -145,6 +148,15 @@ async function test() {
145
148
  console.log('del catch', msg)
146
149
  })
147
150
 
151
+ //del by data without id, 該筆無法處理故ok為0並附err, 整批仍resolve且另發出error事件
152
+ await wo.del({ name: 'no-id' })
153
+ .then(function(msg) {
154
+ console.log('del by data without id then', msg)
155
+ })
156
+ .catch(function(msg) {
157
+ console.log('del by data without id catch', msg)
158
+ })
159
+
148
160
  }
149
161
  test()
150
162
  // change delAll
@@ -193,6 +205,9 @@ test()
193
205
  // save then [ { n: 1, nInserted: 0, nModified: 1, ok: 1 } ]
194
206
  // change del
195
207
  // del then [ { n: 1, nDeleted: 1, ok: 1 } ]
208
+ // error del can not delete by invalid id[]
209
+ // change del
210
+ // del by data without id then [ { n: 0, nDeleted: 0, ok: 0, err: 'can not delete by invalid id[]' } ]
196
211
 
197
212
 
198
213
  //node g-basic.mjs
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "w-orm-lmdb",
3
- "version": "1.0.17",
3
+ "version": "1.0.18",
4
4
  "main": "dist/w-orm-lmdb.umd.js",
5
5
  "dependencies": {
6
6
  "lmdb": "^3.5.6",