w-orm-mongodb 1.1.38 → 1.1.40

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.
@@ -29,7 +29,7 @@
29
29
  <nav >
30
30
 
31
31
 
32
- <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="WOrmMongodb.html">WOrmMongodb</a><ul class='methods'><li data-type='method'><a href="WOrmMongodb.html#.del">del</a></li><li data-type='method'><a href="WOrmMongodb.html#.delAll">delAll</a></li><li data-type='method'><a href="WOrmMongodb.html#.delAllGfs">delAllGfs</a></li><li data-type='method'><a href="WOrmMongodb.html#.delGfs">delGfs</a></li><li data-type='method'><a href="WOrmMongodb.html#.insert">insert</a></li><li data-type='method'><a href="WOrmMongodb.html#.insertGfs">insertGfs</a></li><li data-type='method'><a href="WOrmMongodb.html#.save">save</a></li><li data-type='method'><a href="WOrmMongodb.html#.select">select</a></li><li data-type='method'><a href="WOrmMongodb.html#.selectById">selectById</a></li><li data-type='method'><a href="WOrmMongodb.html#.selectByIdGfs">selectByIdGfs</a></li></ul></li></ul>
32
+ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="WOrmMongodb.html">WOrmMongodb</a><ul class='methods'><li data-type='method'><a href="WOrmMongodb.html#.del">del</a></li><li data-type='method'><a href="WOrmMongodb.html#.delAll">delAll</a></li><li data-type='method'><a href="WOrmMongodb.html#.delAllGfs">delAllGfs</a></li><li data-type='method'><a href="WOrmMongodb.html#.delGfs">delGfs</a></li><li data-type='method'><a href="WOrmMongodb.html#.insert">insert</a></li><li data-type='method'><a href="WOrmMongodb.html#.insertGfs">insertGfs</a></li><li data-type='method'><a href="WOrmMongodb.html#.save">save</a></li><li data-type='method'><a href="WOrmMongodb.html#.select">select</a></li><li data-type='method'><a href="WOrmMongodb.html#.selectByPk">selectByPk</a></li><li data-type='method'><a href="WOrmMongodb.html#.selectByPkGfs">selectByPkGfs</a></li></ul></li></ul>
33
33
 
34
34
  </nav>
35
35
 
@@ -74,13 +74,16 @@
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>操作資料庫(MongoDB)</p></li></ul></dd>
77
+ <dd class="tag-description"><ul class="dummy"><li><p>操作資料庫(MongoDB)</p>
78
+ <p>本套件之主鍵欄位固定為id,尚未支援由呼叫端指定。id為無業務語義之識別碼。
79
+ opt.autoGenPk預設為true,insert、save與insertGfs於輸入未帶有效id時自動產生;del於任一設定下皆不補值。
80
+ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋出,且id之唯一性與格式皆由呼叫端自負。</p></li></ul></dd>
78
81
 
79
82
 
80
83
 
81
84
  <dt class="tag-source">Source:</dt>
82
85
  <dd class="tag-source"><ul class="dummy"><li>
83
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line30">line 30</a>
86
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line36">line 36</a>
84
87
  </li></ul></dd>
85
88
 
86
89
 
@@ -336,6 +339,46 @@
336
339
  </tr>
337
340
 
338
341
 
342
+
343
+ <tr>
344
+
345
+ <td class="name"><code>autoGenPk</code></td>
346
+
347
+
348
+ <td class="type">
349
+
350
+
351
+ <span class="param-type">Boolean</span>
352
+
353
+
354
+
355
+
356
+ </td>
357
+
358
+
359
+ <td class="attributes">
360
+
361
+ &lt;optional><br>
362
+
363
+
364
+
365
+
366
+
367
+ </td>
368
+
369
+
370
+
371
+ <td class="default">
372
+
373
+ <code>true</code>
374
+
375
+ </td>
376
+
377
+
378
+ <td class="description last"><p>輸入是否於輸入未帶有效主鍵時自動產生主鍵值,預設true。為false時主鍵須由呼叫端自備,屬依賴注入之定位,主鍵之唯一性、格式與是否與既有資料衝突皆由呼叫端自負。本設定為建構層設定,不得於insert與save之option逐次覆寫</p></td>
379
+ </tr>
380
+
381
+
339
382
  </tbody>
340
383
  </table>
341
384
 
@@ -431,7 +474,7 @@
431
474
 
432
475
  <dt class="tag-source">Source:</dt>
433
476
  <dd class="tag-source"><ul class="dummy"><li>
434
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line644">line 644</a>
477
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line681">line 681</a>
435
478
  </li></ul></dd>
436
479
 
437
480
 
@@ -593,7 +636,7 @@
593
636
 
594
637
  <dt class="tag-source">Source:</dt>
595
638
  <dd class="tag-source"><ul class="dummy"><li>
596
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line756">line 756</a>
639
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line793">line 793</a>
597
640
  </li></ul></dd>
598
641
 
599
642
 
@@ -773,7 +816,7 @@
773
816
 
774
817
  <dt class="tag-source">Source:</dt>
775
818
  <dd class="tag-source"><ul class="dummy"><li>
776
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1178">line 1178</a>
819
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1215">line 1215</a>
777
820
  </li></ul></dd>
778
821
 
779
822
 
@@ -953,7 +996,7 @@
953
996
 
954
997
  <dt class="tag-source">Source:</dt>
955
998
  <dd class="tag-source"><ul class="dummy"><li>
956
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1050">line 1050</a>
999
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1087">line 1087</a>
957
1000
  </li></ul></dd>
958
1001
 
959
1002
 
@@ -1110,13 +1153,14 @@
1110
1153
  <dt class="tag-description">Description:</dt>
1111
1154
  <dd class="tag-description"><ul class="dummy"><li><p>插入數據,僅於id不存在時寫入,已存在者跳過且不覆寫
1112
1155
  由MongoDB於唯一索引上原子完成[檢查id未存在]與[寫入],併發時同一id僅有一次成功
1113
- 註: n為輸入筆數,nInserted為實際插入筆數,全數已存在而nInserted為0屬正常結果</p></li></ul></dd>
1156
+ 註: n為輸入筆數,nInserted為實際插入筆數,全數已存在而nInserted為0屬正常結果
1157
+ 註: opt.autoGenPk為true(預設)時未帶有效id者自動產生,為false時未帶有效id即reject且同批皆不寫入</p></li></ul></dd>
1114
1158
 
1115
1159
 
1116
1160
 
1117
1161
  <dt class="tag-source">Source:</dt>
1118
1162
  <dd class="tag-source"><ul class="dummy"><li>
1119
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line342">line 342</a>
1163
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line388">line 388</a>
1120
1164
  </li></ul></dd>
1121
1165
 
1122
1166
 
@@ -1272,14 +1316,15 @@
1272
1316
 
1273
1317
  <dt class="tag-description">Description:</dt>
1274
1318
  <dd class="tag-description"><ul class="dummy"><li><p>使用GridFS,插入數據,僅於id不存在時寫入,已存在者跳過且不覆寫
1275
- 數據物件形狀為{ id, u8a },id未給時自動產生,u8a須為Uint8Array
1276
- 註: n為輸入筆數,nInserted為實際插入筆數,全數已存在而nInserted為0屬正常結果</p></li></ul></dd>
1319
+ 數據物件形狀為{ id, u8a },u8a須為Uint8Array
1320
+ 註: n為輸入筆數,nInserted為實際插入筆數,全數已存在而nInserted為0屬正常結果
1321
+ 註: opt.autoGenPk為true(預設)時未帶有效id者自動產生,為false時未帶有效id即reject且同批皆不寫入</p></li></ul></dd>
1277
1322
 
1278
1323
 
1279
1324
 
1280
1325
  <dt class="tag-source">Source:</dt>
1281
1326
  <dd class="tag-source"><ul class="dummy"><li>
1282
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line814">line 814</a>
1327
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line852">line 852</a>
1283
1328
  </li></ul></dd>
1284
1329
 
1285
1330
 
@@ -1436,13 +1481,14 @@
1436
1481
  <dt class="tag-description">Description:</dt>
1437
1482
  <dd class="tag-description"><ul class="dummy"><li><p>儲存數據,以id為準更新既有數據,未給之欄位會保留;id不存在且option.autoInsert為true時改為插入
1438
1483
  註: n為id命中筆數,命中或經插入而產生皆為1;[內容相同]之判定基準為將待儲存物件合併進現值後結果與現值相同,
1439
- 相同者不寫入而nModified為0;本筆失敗不中斷整批,該筆以ok為0並附err回報</p></li></ul></dd>
1484
+ 相同者不寫入而nModified為0;本筆失敗不中斷整批,該筆以ok為0並附err回報
1485
+ 註: opt.autoGenPk為true(預設)時未帶有效id者自動產生,為false時未帶有效id即reject且同批皆不寫入</p></li></ul></dd>
1440
1486
 
1441
1487
 
1442
1488
 
1443
1489
  <dt class="tag-source">Source:</dt>
1444
1490
  <dd class="tag-source"><ul class="dummy"><li>
1445
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line564">line 564</a>
1491
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line606">line 606</a>
1446
1492
  </li></ul></dd>
1447
1493
 
1448
1494
 
@@ -1729,7 +1775,7 @@
1729
1775
 
1730
1776
  <dt class="tag-source">Source:</dt>
1731
1777
  <dd class="tag-source"><ul class="dummy"><li>
1732
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line233">line 233</a>
1778
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line277">line 277</a>
1733
1779
  </li></ul></dd>
1734
1780
 
1735
1781
 
@@ -1891,7 +1937,7 @@
1891
1937
 
1892
1938
 
1893
1939
 
1894
- <h4 class="name" id=".selectById"><span class="type-signature type-signature-async, static">(async, static) </span>selectById<span class="signature">(id)</span><span class="type-signature"> &rarr; {Promise}</span></h4>
1940
+ <h4 class="name" id=".selectByPk"><span class="type-signature type-signature-async, static">(async, static) </span>selectByPk<span class="signature">(pk)</span><span class="type-signature"> &rarr; {Promise}</span></h4>
1895
1941
 
1896
1942
 
1897
1943
 
@@ -1901,14 +1947,15 @@
1901
1947
  <dl class="details">
1902
1948
 
1903
1949
  <dt class="tag-description">Description:</dt>
1904
- <dd class="tag-description"><ul class="dummy"><li><p>由id查詢單筆數據,因由MongoDB查找且僅回傳單筆,不需如select提取全部符合數據再處理,故數據量大時效能較佳
1950
+ <dd class="tag-description"><ul class="dummy"><li><p>由主鍵查詢單筆數據,因由MongoDB查找且僅回傳單筆,不需如select提取全部符合數據再處理,故數據量大時效能較佳
1951
+ 註: 本套件之主鍵欄位固定為id,尚未支援由呼叫端指定
1905
1952
  註: 本函數不得有副作用,故不建立唯一索引</p></li></ul></dd>
1906
1953
 
1907
1954
 
1908
1955
 
1909
1956
  <dt class="tag-source">Source:</dt>
1910
1957
  <dd class="tag-source"><ul class="dummy"><li>
1911
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line284">line 284</a>
1958
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line329">line 329</a>
1912
1959
  </li></ul></dd>
1913
1960
 
1914
1961
 
@@ -1980,7 +2027,7 @@
1980
2027
 
1981
2028
  <tr>
1982
2029
 
1983
- <td class="name"><code>id</code></td>
2030
+ <td class="name"><code>pk</code></td>
1984
2031
 
1985
2032
 
1986
2033
  <td class="type">
@@ -1997,7 +2044,7 @@
1997
2044
 
1998
2045
 
1999
2046
 
2000
- <td class="description last"><p>輸入id字串</p></td>
2047
+ <td class="description last"><p>輸入主鍵值字串,即數據之id</p></td>
2001
2048
  </tr>
2002
2049
 
2003
2050
 
@@ -2023,7 +2070,7 @@
2023
2070
 
2024
2071
 
2025
2072
  <div class="param-desc">
2026
- <p>回傳Promise,resolve回傳數據物件,若無此id或id無效則回傳null,reject回傳錯誤訊息</p>
2073
+ <p>回傳Promise,resolve回傳數據物件,若無此主鍵或主鍵值無效則回傳null,reject回傳錯誤訊息</p>
2027
2074
  </div>
2028
2075
 
2029
2076
 
@@ -2050,7 +2097,7 @@
2050
2097
 
2051
2098
 
2052
2099
 
2053
- <h4 class="name" id=".selectByIdGfs"><span class="type-signature type-signature-async, static">(async, static) </span>selectByIdGfs<span class="signature">(id)</span><span class="type-signature"> &rarr; {Promise}</span></h4>
2100
+ <h4 class="name" id=".selectByPkGfs"><span class="type-signature type-signature-async, static">(async, static) </span>selectByPkGfs<span class="signature">(pk)</span><span class="type-signature"> &rarr; {Promise}</span></h4>
2054
2101
 
2055
2102
 
2056
2103
 
@@ -2060,15 +2107,16 @@
2060
2107
  <dl class="details">
2061
2108
 
2062
2109
  <dt class="tag-description">Description:</dt>
2063
- <dd class="tag-description"><ul class="dummy"><li><p>使用GridFS,由id查詢單筆數據
2064
- 註: 查無數據或id無效時回傳null,判定基準與selectById一致
2110
+ <dd class="tag-description"><ul class="dummy"><li><p>使用GridFS,由主鍵查詢單筆數據
2111
+ 註: 查無數據或主鍵值無效時回傳null,判定基準與selectByPk一致
2112
+ 註: 本套件之主鍵欄位固定為id,尚未支援由呼叫端指定
2065
2113
  本函數不得有副作用,故不建立唯一索引</p></li></ul></dd>
2066
2114
 
2067
2115
 
2068
2116
 
2069
2117
  <dt class="tag-source">Source:</dt>
2070
2118
  <dd class="tag-source"><ul class="dummy"><li>
2071
- <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line920">line 920</a>
2119
+ <a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line957">line 957</a>
2072
2120
  </li></ul></dd>
2073
2121
 
2074
2122
 
@@ -2140,7 +2188,7 @@
2140
2188
 
2141
2189
  <tr>
2142
2190
 
2143
- <td class="name"><code>id</code></td>
2191
+ <td class="name"><code>pk</code></td>
2144
2192
 
2145
2193
 
2146
2194
  <td class="type">
@@ -2157,7 +2205,7 @@
2157
2205
 
2158
2206
 
2159
2207
 
2160
- <td class="description last"><p>輸入查詢id字串</p></td>
2208
+ <td class="description last"><p>輸入主鍵值字串,即數據之id</p></td>
2161
2209
  </tr>
2162
2210
 
2163
2211
 
@@ -2183,7 +2231,7 @@
2183
2231
 
2184
2232
 
2185
2233
  <div class="param-desc">
2186
- <p>回傳Promise,resolve回傳數據物件{ id, u8a },若無此id或id無效則回傳null,reject回傳錯誤訊息</p>
2234
+ <p>回傳Promise,resolve回傳數據物件{ id, u8a },若無此主鍵或主鍵值無效則回傳null,reject回傳錯誤訊息</p>
2187
2235
  </div>
2188
2236
 
2189
2237
 
@@ -2225,7 +2273,7 @@
2225
2273
  <br class="clear">
2226
2274
 
2227
2275
  <footer>
2228
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Sun Aug 16 2026 22:37:44 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
2276
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 07:33:48 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
2229
2277
  </footer>
2230
2278
 
2231
2279
  <script>prettyPrint();</script>
@@ -29,7 +29,7 @@
29
29
  <nav >
30
30
 
31
31
 
32
- <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="WOrmMongodb.html">WOrmMongodb</a><ul class='methods'><li data-type='method'><a href="WOrmMongodb.html#.del">del</a></li><li data-type='method'><a href="WOrmMongodb.html#.delAll">delAll</a></li><li data-type='method'><a href="WOrmMongodb.html#.delAllGfs">delAllGfs</a></li><li data-type='method'><a href="WOrmMongodb.html#.delGfs">delGfs</a></li><li data-type='method'><a href="WOrmMongodb.html#.insert">insert</a></li><li data-type='method'><a href="WOrmMongodb.html#.insertGfs">insertGfs</a></li><li data-type='method'><a href="WOrmMongodb.html#.save">save</a></li><li data-type='method'><a href="WOrmMongodb.html#.select">select</a></li><li data-type='method'><a href="WOrmMongodb.html#.selectById">selectById</a></li><li data-type='method'><a href="WOrmMongodb.html#.selectByIdGfs">selectByIdGfs</a></li></ul></li></ul>
32
+ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="WOrmMongodb.html">WOrmMongodb</a><ul class='methods'><li data-type='method'><a href="WOrmMongodb.html#.del">del</a></li><li data-type='method'><a href="WOrmMongodb.html#.delAll">delAll</a></li><li data-type='method'><a href="WOrmMongodb.html#.delAllGfs">delAllGfs</a></li><li data-type='method'><a href="WOrmMongodb.html#.delGfs">delGfs</a></li><li data-type='method'><a href="WOrmMongodb.html#.insert">insert</a></li><li data-type='method'><a href="WOrmMongodb.html#.insertGfs">insertGfs</a></li><li data-type='method'><a href="WOrmMongodb.html#.save">save</a></li><li data-type='method'><a href="WOrmMongodb.html#.select">select</a></li><li data-type='method'><a href="WOrmMongodb.html#.selectByPk">selectByPk</a></li><li data-type='method'><a href="WOrmMongodb.html#.selectByPkGfs">selectByPkGfs</a></li></ul></li></ul>
33
33
 
34
34
  </nav>
35
35
 
@@ -56,6 +56,7 @@ import omit from 'lodash-es/omit.js'
56
56
  import size from 'lodash-es/size.js'
57
57
  import genPm from 'wsemi/src/genPm.mjs'
58
58
  import genID from 'wsemi/src/genID.mjs'
59
+ import isbol from 'wsemi/src/isbol.mjs'
59
60
  import isestr from 'wsemi/src/isestr.mjs'
60
61
  import isarr from 'wsemi/src/isarr.mjs'
61
62
  import isearr from 'wsemi/src/isearr.mjs'
@@ -67,11 +68,16 @@ import pmSeries from 'wsemi/src/pmSeries.mjs'
67
68
  /**
68
69
  * 操作資料庫(MongoDB)
69
70
  *
71
+ * 本套件之主鍵欄位固定為id,尚未支援由呼叫端指定。id為無業務語義之識別碼。
72
+ * opt.autoGenPk預設為true,insert、save與insertGfs於輸入未帶有效id時自動產生;del於任一設定下皆不補值。
73
+ * opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋出,且id之唯一性與格式皆由呼叫端自負。
74
+ *
70
75
  * @class
71
76
  * @param {Object} [opt={}] 輸入設定物件,預設{}
72
77
  * @param {String} [opt.url='mongodb://127.0.0.1:27017'] 輸入連接資料庫字串,預設'mongodb://127.0.0.1:27017'
73
78
  * @param {String} [opt.db='worm'] 輸入使用資料庫名稱字串,預設'worm'
74
79
  * @param {String} [opt.cl='test'] 輸入使用資料表名稱字串,預設'test'
80
+ * @param {Boolean} [opt.autoGenPk=true] 輸入是否於輸入未帶有效主鍵時自動產生主鍵值,預設true。為false時主鍵須由呼叫端自備,屬依賴注入之定位,主鍵之唯一性、格式與是否與既有資料衝突皆由呼叫端自負。本設定為建構層設定,不得於insert與save之option逐次覆寫
75
81
  * @returns {Object} 回傳操作資料庫物件,各事件功能詳見說明
76
82
  */
77
83
  function WOrmMongodb(opt = {}) {
@@ -89,6 +95,13 @@ function WOrmMongodb(opt = {}) {
89
95
  }
90
96
 
91
97
 
98
+ //autoGenPk, 預設開啟, 為false時主鍵一律由呼叫端自備, 套件不產生亦不補救
99
+ let autoGenPk = get(opt, 'autoGenPk')
100
+ if (!isbol(autoGenPk)) {
101
+ autoGenPk = true
102
+ }
103
+
104
+
92
105
  //_indexReady, 唯一索引只須建立一次, 以旗標記錄避免每次操作皆多一次round-trip
93
106
  let _indexReady = false
94
107
 
@@ -143,6 +156,37 @@ function WOrmMongodb(opt = {}) {
143
156
  }
144
157
 
145
158
 
159
+ /**
160
+ * 檢查並補齊單筆數據之主鍵
161
+ * autoGenPk為true時未帶有效id者自動產生,為false時往外拋
162
+ * 註: 未帶有效id屬呼叫端未履行契約而非某一筆資料本身之問題,故為整批性錯誤而不降級為該筆ok為0,
163
+ * 若降級為逐筆結果,呼叫端易於整批resolve之下漏看,使[忘了給id]靜默變成[少寫了幾筆]
164
+ * 註: 本函數須於任何寫入之前一次對全部數據完成,令拋錯時同批之有效筆數亦不會被寫入
165
+ *
166
+ * @ignore
167
+ * @param {Object} v 輸入數據物件
168
+ * @param {Number} k 輸入數據於陣列內之索引
169
+ * @returns {Object} 回傳補齊主鍵之數據物件
170
+ */
171
+ function procPk(v, k) {
172
+
173
+ //check
174
+ if (!isestr(v.id)) {
175
+
176
+ //check, autoGenPk為false時主鍵須由呼叫端自備
177
+ if (!autoGenPk) {
178
+ throw new Error(`invalid data[${k}].id, autoGenPk is false`)
179
+ }
180
+
181
+ //genID
182
+ v.id = genID()
183
+
184
+ }
185
+
186
+ return v
187
+ }
188
+
189
+
146
190
  /**
147
191
  * 判定是否為唯一索引重複鍵錯誤(11000),批次插入時須全部寫入錯誤皆為重複鍵才算
148
192
  *
@@ -321,19 +365,20 @@ function WOrmMongodb(opt = {}) {
321
365
 
322
366
 
323
367
  /**
324
- * 由id查詢單筆數據,因由MongoDB查找且僅回傳單筆,不需如select提取全部符合數據再處理,故數據量大時效能較佳
368
+ * 由主鍵查詢單筆數據,因由MongoDB查找且僅回傳單筆,不需如select提取全部符合數據再處理,故數據量大時效能較佳
369
+ * 註: 本套件之主鍵欄位固定為id,尚未支援由呼叫端指定
325
370
  * 註: 本函數不得有副作用,故不建立唯一索引
326
371
  *
327
372
  * @memberOf WOrmMongodb
328
- * @param {String} id 輸入id字串
329
- * @returns {Promise} 回傳Promise,resolve回傳數據物件,若無此id或id無效則回傳null,reject回傳錯誤訊息
373
+ * @param {String} pk 輸入主鍵值字串,即數據之id
374
+ * @returns {Promise} 回傳Promise,resolve回傳數據物件,若無此主鍵或主鍵值無效則回傳null,reject回傳錯誤訊息
330
375
  */
331
- async function selectById(id) {
376
+ async function selectByPk(pk) {
332
377
  let isErr = false
333
378
 
334
379
  //check
335
- if (!isestr(id)) {
336
- //未給有效id視為查無數據, 判定基準與insert、save、del內對id之認定一致
380
+ if (!isestr(pk)) {
381
+ //未給有效主鍵值視為查無數據, 判定基準與insert、save、del內對id之認定一致
337
382
  return null
338
383
  }
339
384
 
@@ -349,7 +394,7 @@ function WOrmMongodb(opt = {}) {
349
394
  let collection = database.collection(opt.cl)
350
395
 
351
396
  //findOne, 以投影去除_id, 令回傳形狀與select一致
352
- let v = await collection.findOne({ id }, { projection: { _id: 0 } })
397
+ let v = await collection.findOne({ id: pk }, { projection: { _id: 0 } })
353
398
 
354
399
  //check, 判定基準與insert、save、del內對既有數據之認定一致
355
400
  if (iseobj(v)) {
@@ -381,6 +426,7 @@ function WOrmMongodb(opt = {}) {
381
426
  * 插入數據,僅於id不存在時寫入,已存在者跳過且不覆寫
382
427
  * 由MongoDB於唯一索引上原子完成[檢查id未存在]與[寫入],併發時同一id僅有一次成功
383
428
  * 註: n為輸入筆數,nInserted為實際插入筆數,全數已存在而nInserted為0屬正常結果
429
+ * 註: opt.autoGenPk為true(預設)時未帶有效id者自動產生,為false時未帶有效id即reject且同批皆不寫入
384
430
  *
385
431
  * @memberOf WOrmMongodb
386
432
  * @param {Object|Array} data 輸入數據物件或陣列
@@ -420,13 +466,8 @@ function WOrmMongodb(opt = {}) {
420
466
  data = [data]
421
467
  }
422
468
 
423
- //check id
424
- data = map(data, function(v) {
425
- if (!isestr(v.id)) {
426
- v.id = genID()
427
- }
428
- return v
429
- })
469
+ //check id, 須於任何寫入之前一次完成, 令autoGenPk為false而拋錯時同批之有效筆數亦不會被寫入
470
+ data = map(data, procPk)
430
471
 
431
472
  //ensureIndex
432
473
  await ensureIndex(collection)
@@ -601,6 +642,7 @@ function WOrmMongodb(opt = {}) {
601
642
  * 儲存數據,以id為準更新既有數據,未給之欄位會保留;id不存在且option.autoInsert為true時改為插入
602
643
  * 註: n為id命中筆數,命中或經插入而產生皆為1;[內容相同]之判定基準為將待儲存物件合併進現值後結果與現值相同,
603
644
  * 相同者不寫入而nModified為0;本筆失敗不中斷整批,該筆以ok為0並附err回報
645
+ * 註: opt.autoGenPk為true(預設)時未帶有效id者自動產生,為false時未帶有效id即reject且同批皆不寫入
604
646
  *
605
647
  * @memberOf WOrmMongodb
606
648
  * @param {Object|Array} data 輸入數據物件或陣列
@@ -641,13 +683,8 @@ function WOrmMongodb(opt = {}) {
641
683
  data = [data]
642
684
  }
643
685
 
644
- //check id
645
- data = map(data, function(v) {
646
- if (!isestr(v.id)) {
647
- v.id = genID()
648
- }
649
- return v
650
- })
686
+ //check id, 須於任何寫入之前一次完成, 令autoGenPk為false而拋錯時同批之有效筆數亦不會被寫入
687
+ data = map(data, procPk)
651
688
 
652
689
  //ensureIndex
653
690
  await ensureIndex(collection)
@@ -851,8 +888,9 @@ function WOrmMongodb(opt = {}) {
851
888
 
852
889
  /**
853
890
  * 使用GridFS,插入數據,僅於id不存在時寫入,已存在者跳過且不覆寫
854
- * 數據物件形狀為{ id, u8a },id未給時自動產生,u8a須為Uint8Array
891
+ * 數據物件形狀為{ id, u8a },u8a須為Uint8Array
855
892
  * 註: n為輸入筆數,nInserted為實際插入筆數,全數已存在而nInserted為0屬正常結果
893
+ * 註: opt.autoGenPk為true(預設)時未帶有效id者自動產生,為false時未帶有效id即reject且同批皆不寫入
856
894
  *
857
895
  * @memberOf WOrmMongodb
858
896
  * @param {Object|Array} data 輸入數據物件或陣列,各數據物件形狀為{ id, u8a }
@@ -902,9 +940,7 @@ function WOrmMongodb(opt = {}) {
902
940
  //u8a無效屬呼叫端給值錯誤且整批函數無從逐筆回報, 故往外拋
903
941
  data = map(data, function(v, k) {
904
942
  v = { ...v }
905
- if (!isestr(v.id)) {
906
- v.id = genID()
907
- }
943
+ v = procPk(v, k)
908
944
  if (!isu8arr(v.u8a)) {
909
945
  throw new Error(`invalid data[${k}].u8a`)
910
946
  }
@@ -956,20 +992,21 @@ function WOrmMongodb(opt = {}) {
956
992
 
957
993
 
958
994
  /**
959
- * 使用GridFS,由id查詢單筆數據
960
- * 註: 查無數據或id無效時回傳null,判定基準與selectById一致
995
+ * 使用GridFS,由主鍵查詢單筆數據
996
+ * 註: 查無數據或主鍵值無效時回傳null,判定基準與selectByPk一致
997
+ * 註: 本套件之主鍵欄位固定為id,尚未支援由呼叫端指定
961
998
  * 本函數不得有副作用,故不建立唯一索引
962
999
  *
963
1000
  * @memberOf WOrmMongodb
964
- * @param {String} id 輸入查詢id字串
965
- * @returns {Promise} 回傳Promise,resolve回傳數據物件{ id, u8a },若無此id或id無效則回傳null,reject回傳錯誤訊息
1001
+ * @param {String} pk 輸入主鍵值字串,即數據之id
1002
+ * @returns {Promise} 回傳Promise,resolve回傳數據物件{ id, u8a },若無此主鍵或主鍵值無效則回傳null,reject回傳錯誤訊息
966
1003
  */
967
- async function selectByIdGfs(id) {
1004
+ async function selectByPkGfs(pk) {
968
1005
  let isErr = false
969
1006
 
970
1007
  //check
971
- if (!isestr(id)) {
972
- //未給有效id視為查無數據, 判定基準與selectById一致
1008
+ if (!isestr(pk)) {
1009
+ //未給有效主鍵值視為查無數據, 判定基準與selectByPk一致
973
1010
  return null
974
1011
  }
975
1012
 
@@ -1026,11 +1063,11 @@ function WOrmMongodb(opt = {}) {
1026
1063
  try {
1027
1064
 
1028
1065
  //core
1029
- let u8a = await core(id)
1066
+ let u8a = await core(pk)
1030
1067
 
1031
1068
  //res, 形狀與insertGfs所收之數據物件一致
1032
1069
  res = {
1033
- id,
1070
+ id: pk,
1034
1071
  u8a,
1035
1072
  }
1036
1073
 
@@ -1287,12 +1324,12 @@ function WOrmMongodb(opt = {}) {
1287
1324
 
1288
1325
  //bind
1289
1326
  ee.select = select
1290
- ee.selectById = selectById
1327
+ ee.selectByPk = selectByPk
1291
1328
  ee.insert = insert
1292
1329
  ee.save = save
1293
1330
  ee.del = del
1294
1331
  ee.delAll = delAll
1295
- ee.selectByIdGfs = selectByIdGfs
1332
+ ee.selectByPkGfs = selectByPkGfs
1296
1333
  ee.insertGfs = insertGfs
1297
1334
  ee.delGfs = delGfs
1298
1335
  ee.delAllGfs = delAllGfs
@@ -1317,7 +1354,7 @@ export default WOrmMongodb
1317
1354
  <br class="clear">
1318
1355
 
1319
1356
  <footer>
1320
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Sun Aug 16 2026 22:37:44 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
1357
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 07:33:48 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
1321
1358
  </footer>
1322
1359
 
1323
1360
  <script>prettyPrint();</script>
package/docs/index.html CHANGED
@@ -29,7 +29,7 @@
29
29
  <nav >
30
30
 
31
31
 
32
- <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="WOrmMongodb.html">WOrmMongodb</a><ul class='methods'><li data-type='method'><a href="WOrmMongodb.html#.del">del</a></li><li data-type='method'><a href="WOrmMongodb.html#.delAll">delAll</a></li><li data-type='method'><a href="WOrmMongodb.html#.delAllGfs">delAllGfs</a></li><li data-type='method'><a href="WOrmMongodb.html#.delGfs">delGfs</a></li><li data-type='method'><a href="WOrmMongodb.html#.insert">insert</a></li><li data-type='method'><a href="WOrmMongodb.html#.insertGfs">insertGfs</a></li><li data-type='method'><a href="WOrmMongodb.html#.save">save</a></li><li data-type='method'><a href="WOrmMongodb.html#.select">select</a></li><li data-type='method'><a href="WOrmMongodb.html#.selectById">selectById</a></li><li data-type='method'><a href="WOrmMongodb.html#.selectByIdGfs">selectByIdGfs</a></li></ul></li></ul>
32
+ <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="WOrmMongodb.html">WOrmMongodb</a><ul class='methods'><li data-type='method'><a href="WOrmMongodb.html#.del">del</a></li><li data-type='method'><a href="WOrmMongodb.html#.delAll">delAll</a></li><li data-type='method'><a href="WOrmMongodb.html#.delAllGfs">delAllGfs</a></li><li data-type='method'><a href="WOrmMongodb.html#.delGfs">delGfs</a></li><li data-type='method'><a href="WOrmMongodb.html#.insert">insert</a></li><li data-type='method'><a href="WOrmMongodb.html#.insertGfs">insertGfs</a></li><li data-type='method'><a href="WOrmMongodb.html#.save">save</a></li><li data-type='method'><a href="WOrmMongodb.html#.select">select</a></li><li data-type='method'><a href="WOrmMongodb.html#.selectByPk">selectByPk</a></li><li data-type='method'><a href="WOrmMongodb.html#.selectByPkGfs">selectByPkGfs</a></li></ul></li></ul>
33
33
 
34
34
  </nav>
35
35
 
@@ -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 Sun Aug 16 2026 22:37:44 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 07:33:48 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
@@ -102,13 +102,13 @@ async function test() {
102
102
  let sr = await wo.select({ name: { $regex: 'PeT', $options: 'i' } })
103
103
  console.log('selectReg', sr)
104
104
 
105
- //selectById, 由id直接查找單筆, 不需如select提取全部符合數據再處理
106
- let sbi = await wo.selectById('id-rosemary')
107
- console.log('selectById', sbi)
105
+ //selectByPk, 由id直接查找單筆, 不需如select提取全部符合數據再處理
106
+ let sbi = await wo.selectByPk('id-rosemary')
107
+ console.log('selectByPk', sbi)
108
108
 
109
- //selectById by id not existed
110
- let sbn = await wo.selectById('id-not-existed')
111
- console.log('selectById by id not existed', sbn)
109
+ //selectByPk by id not existed
110
+ let sbn = await wo.selectByPk('id-not-existed')
111
+ console.log('selectByPk by id not existed', sbn)
112
112
 
113
113
  //del
114
114
  let d = ss.filter(function(v) {
@@ -165,8 +165,8 @@ test()
165
165
  // }
166
166
  // ]
167
167
  // selectReg [ { id: 'id-peter', name: 'peter(modify)', value: 123 } ]
168
- // selectById { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 }
169
- // selectById by id not existed null
168
+ // selectByPk { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 }
169
+ // selectByPk by id not existed null
170
170
  // change del
171
171
  // del then [ { n: 1, nDeleted: 1, ok: 1 } ]
172
172
 
package/g-gfs.mjs CHANGED
@@ -49,15 +49,15 @@ async function test() {
49
49
  let gr = await wo.insertGfs({ id: 'id-file', u8a: genU8a(50) })
50
50
  console.log('insertGfs existed id', gr)
51
51
 
52
- //selectByIdGfs
53
- let gs = await wo.selectByIdGfs('id-file')
54
- console.log('selectByIdGfs id', gs.id)
55
- console.log('selectByIdGfs u8a.length', gs.u8a.length)
56
- console.log('selectByIdGfs u8a[0..3]', gs.u8a[0], gs.u8a[1], gs.u8a[2], gs.u8a[3])
52
+ //selectByPkGfs
53
+ let gs = await wo.selectByPkGfs('id-file')
54
+ console.log('selectByPkGfs id', gs.id)
55
+ console.log('selectByPkGfs u8a.length', gs.u8a.length)
56
+ console.log('selectByPkGfs u8a[0..3]', gs.u8a[0], gs.u8a[1], gs.u8a[2], gs.u8a[3])
57
57
 
58
- //selectByIdGfs by id not existed
59
- let gn = await wo.selectByIdGfs('id-not-existed')
60
- console.log('selectByIdGfs by id not existed', gn)
58
+ //selectByPkGfs by id not existed
59
+ let gn = await wo.selectByPkGfs('id-not-existed')
60
+ console.log('selectByPkGfs by id not existed', gn)
61
61
 
62
62
  //insertGfs, 一次插入多筆
63
63
  let gm = await wo.insertGfs([
@@ -86,10 +86,10 @@ test()
86
86
  // insertGfs { n: 1, nInserted: 1, ok: 1 }
87
87
  // change insertGfs
88
88
  // insertGfs existed id { n: 1, nInserted: 0, ok: 1 }
89
- // selectByIdGfs id id-file
90
- // selectByIdGfs u8a.length 1000
91
- // selectByIdGfs u8a[0..3] 0 1 2 3
92
- // selectByIdGfs by id not existed null
89
+ // selectByPkGfs id id-file
90
+ // selectByPkGfs u8a.length 1000
91
+ // selectByPkGfs u8a[0..3] 0 1 2 3
92
+ // selectByPkGfs by id not existed null
93
93
  // change insertGfs
94
94
  // insertGfs multi { n: 2, nInserted: 2, ok: 1 }
95
95
  // change delGfs