w-orm-mongodb 1.1.41 → 1.1.43
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/README.md +46 -4
- package/dist/w-orm-mongodb.umd.js +2 -2
- package/dist/w-orm-mongodb.umd.js.map +1 -1
- package/docs/WOrmMongodb.html +181 -13
- package/docs/WOrmMongodb.mjs.html +213 -5
- package/docs/index.html +2 -2
- package/g-basic.mjs +7 -1
- package/g-gfs.mjs +7 -1
- package/package.json +1 -1
- package/src/WOrmMongodb.mjs +211 -3
- package/test/api-basic.test.mjs +200 -0
- package/test/api-insertbulk-rs.test.mjs +290 -0
- package/test/lib/api-setup.mjs +93 -12
package/docs/WOrmMongodb.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#.selectByPk">selectByPk</a></li><li data-type='method'><a href="WOrmMongodb.html#.selectByPkGfs">selectByPkGfs</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#.insertBulk">insertBulk</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
|
|
|
@@ -83,7 +83,7 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
|
|
|
83
83
|
|
|
84
84
|
<dt class="tag-source">Source:</dt>
|
|
85
85
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
86
|
-
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#
|
|
86
|
+
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line37">line 37</a>
|
|
87
87
|
</li></ul></dd>
|
|
88
88
|
|
|
89
89
|
|
|
@@ -474,7 +474,7 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
|
|
|
474
474
|
|
|
475
475
|
<dt class="tag-source">Source:</dt>
|
|
476
476
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
477
|
-
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#
|
|
477
|
+
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line936">line 936</a>
|
|
478
478
|
</li></ul></dd>
|
|
479
479
|
|
|
480
480
|
|
|
@@ -636,7 +636,7 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
|
|
|
636
636
|
|
|
637
637
|
<dt class="tag-source">Source:</dt>
|
|
638
638
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
639
|
-
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#
|
|
639
|
+
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1064">line 1064</a>
|
|
640
640
|
</li></ul></dd>
|
|
641
641
|
|
|
642
642
|
|
|
@@ -816,7 +816,7 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
|
|
|
816
816
|
|
|
817
817
|
<dt class="tag-source">Source:</dt>
|
|
818
818
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
819
|
-
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#
|
|
819
|
+
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1518">line 1518</a>
|
|
820
820
|
</li></ul></dd>
|
|
821
821
|
|
|
822
822
|
|
|
@@ -996,7 +996,7 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
|
|
|
996
996
|
|
|
997
997
|
<dt class="tag-source">Source:</dt>
|
|
998
998
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
999
|
-
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#
|
|
999
|
+
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1374">line 1374</a>
|
|
1000
1000
|
</li></ul></dd>
|
|
1001
1001
|
|
|
1002
1002
|
|
|
@@ -1160,7 +1160,175 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
|
|
|
1160
1160
|
|
|
1161
1161
|
<dt class="tag-source">Source:</dt>
|
|
1162
1162
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
1163
|
-
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#
|
|
1163
|
+
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line426">line 426</a>
|
|
1164
|
+
</li></ul></dd>
|
|
1165
|
+
|
|
1166
|
+
|
|
1167
|
+
|
|
1168
|
+
|
|
1169
|
+
|
|
1170
|
+
|
|
1171
|
+
|
|
1172
|
+
|
|
1173
|
+
|
|
1174
|
+
|
|
1175
|
+
|
|
1176
|
+
|
|
1177
|
+
|
|
1178
|
+
|
|
1179
|
+
|
|
1180
|
+
|
|
1181
|
+
|
|
1182
|
+
|
|
1183
|
+
|
|
1184
|
+
|
|
1185
|
+
|
|
1186
|
+
|
|
1187
|
+
|
|
1188
|
+
|
|
1189
|
+
|
|
1190
|
+
|
|
1191
|
+
|
|
1192
|
+
|
|
1193
|
+
|
|
1194
|
+
|
|
1195
|
+
|
|
1196
|
+
</dl>
|
|
1197
|
+
|
|
1198
|
+
|
|
1199
|
+
|
|
1200
|
+
|
|
1201
|
+
|
|
1202
|
+
|
|
1203
|
+
|
|
1204
|
+
|
|
1205
|
+
|
|
1206
|
+
|
|
1207
|
+
|
|
1208
|
+
|
|
1209
|
+
|
|
1210
|
+
<h5 class="h5-parameters">Parameters:</h5>
|
|
1211
|
+
|
|
1212
|
+
|
|
1213
|
+
<table class="params">
|
|
1214
|
+
<thead>
|
|
1215
|
+
<tr>
|
|
1216
|
+
|
|
1217
|
+
<th>Name</th>
|
|
1218
|
+
|
|
1219
|
+
|
|
1220
|
+
<th>Type</th>
|
|
1221
|
+
|
|
1222
|
+
|
|
1223
|
+
|
|
1224
|
+
|
|
1225
|
+
|
|
1226
|
+
<th class="last">Description</th>
|
|
1227
|
+
</tr>
|
|
1228
|
+
</thead>
|
|
1229
|
+
|
|
1230
|
+
<tbody>
|
|
1231
|
+
|
|
1232
|
+
|
|
1233
|
+
<tr>
|
|
1234
|
+
|
|
1235
|
+
<td class="name"><code>data</code></td>
|
|
1236
|
+
|
|
1237
|
+
|
|
1238
|
+
<td class="type">
|
|
1239
|
+
|
|
1240
|
+
|
|
1241
|
+
<span class="param-type">Object</span>
|
|
1242
|
+
|
|
|
1243
|
+
|
|
1244
|
+
<span class="param-type">Array</span>
|
|
1245
|
+
|
|
1246
|
+
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
</td>
|
|
1250
|
+
|
|
1251
|
+
|
|
1252
|
+
|
|
1253
|
+
|
|
1254
|
+
|
|
1255
|
+
<td class="description last"><p>輸入數據物件或陣列</p></td>
|
|
1256
|
+
</tr>
|
|
1257
|
+
|
|
1258
|
+
|
|
1259
|
+
</tbody>
|
|
1260
|
+
</table>
|
|
1261
|
+
|
|
1262
|
+
|
|
1263
|
+
|
|
1264
|
+
|
|
1265
|
+
|
|
1266
|
+
|
|
1267
|
+
|
|
1268
|
+
|
|
1269
|
+
|
|
1270
|
+
|
|
1271
|
+
|
|
1272
|
+
|
|
1273
|
+
|
|
1274
|
+
|
|
1275
|
+
|
|
1276
|
+
|
|
1277
|
+
<h5 class="h5-returns">Returns:</h5>
|
|
1278
|
+
|
|
1279
|
+
|
|
1280
|
+
<div class="param-desc">
|
|
1281
|
+
<p>回傳Promise,resolve回傳插入結果,reject回傳錯誤訊息</p>
|
|
1282
|
+
</div>
|
|
1283
|
+
|
|
1284
|
+
|
|
1285
|
+
|
|
1286
|
+
<dl class="param-type">
|
|
1287
|
+
<dt>
|
|
1288
|
+
Type
|
|
1289
|
+
</dt>
|
|
1290
|
+
<dd>
|
|
1291
|
+
|
|
1292
|
+
<span class="param-type">Promise</span>
|
|
1293
|
+
|
|
1294
|
+
|
|
1295
|
+
|
|
1296
|
+
</dd>
|
|
1297
|
+
</dl>
|
|
1298
|
+
|
|
1299
|
+
|
|
1300
|
+
|
|
1301
|
+
|
|
1302
|
+
|
|
1303
|
+
|
|
1304
|
+
|
|
1305
|
+
|
|
1306
|
+
|
|
1307
|
+
|
|
1308
|
+
<h4 class="name" id=".insertBulk"><span class="type-signature type-signature-async, static">(async, static) </span>insertBulk<span class="signature">(data)</span><span class="type-signature"> → {Promise}</span></h4>
|
|
1309
|
+
|
|
1310
|
+
|
|
1311
|
+
|
|
1312
|
+
|
|
1313
|
+
|
|
1314
|
+
|
|
1315
|
+
<dl class="details">
|
|
1316
|
+
|
|
1317
|
+
<dt class="tag-description">Description:</dt>
|
|
1318
|
+
<dd class="tag-description"><ul class="dummy"><li><p>批次插入數據,全批視為一個單位:全部插入成功,或一筆都不寫入
|
|
1319
|
+
任一筆之id已存在(含同批含重複id)即以reject拋出整批性錯誤,且不寫入任何一筆
|
|
1320
|
+
註: 本函數非insert之加速版,兩者衝突政策不同——insert跳過已存在者而整批ok為1,
|
|
1321
|
+
本函數則整批reject;需要逐筆處置者改用insert
|
|
1322
|
+
註: n為輸入筆數,nInserted於成功時恆等於n;不提供逐筆結果,故不出現ok為0與err
|
|
1323
|
+
註: opt.autoGenPk為true(預設)時未帶有效id者自動產生,為false時未帶有效id即reject且同批皆不寫入
|
|
1324
|
+
註: 全有全無之達成方式依部署而異——具replica set或分片叢集者以交易包覆,
|
|
1325
|
+
standalone無交易可用而以補償動作達成,其限制詳見_insertBulkByCompensate</p></li></ul></dd>
|
|
1326
|
+
|
|
1327
|
+
|
|
1328
|
+
|
|
1329
|
+
<dt class="tag-source">Source:</dt>
|
|
1330
|
+
<dd class="tag-source"><ul class="dummy"><li>
|
|
1331
|
+
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line638">line 638</a>
|
|
1164
1332
|
</li></ul></dd>
|
|
1165
1333
|
|
|
1166
1334
|
|
|
@@ -1324,7 +1492,7 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
|
|
|
1324
1492
|
|
|
1325
1493
|
<dt class="tag-source">Source:</dt>
|
|
1326
1494
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
1327
|
-
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#
|
|
1495
|
+
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1128">line 1128</a>
|
|
1328
1496
|
</li></ul></dd>
|
|
1329
1497
|
|
|
1330
1498
|
|
|
@@ -1488,7 +1656,7 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
|
|
|
1488
1656
|
|
|
1489
1657
|
<dt class="tag-source">Source:</dt>
|
|
1490
1658
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
1491
|
-
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#
|
|
1659
|
+
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line856">line 856</a>
|
|
1492
1660
|
</li></ul></dd>
|
|
1493
1661
|
|
|
1494
1662
|
|
|
@@ -1775,7 +1943,7 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
|
|
|
1775
1943
|
|
|
1776
1944
|
<dt class="tag-source">Source:</dt>
|
|
1777
1945
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
1778
|
-
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#
|
|
1946
|
+
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line305">line 305</a>
|
|
1779
1947
|
</li></ul></dd>
|
|
1780
1948
|
|
|
1781
1949
|
|
|
@@ -1955,7 +2123,7 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
|
|
|
1955
2123
|
|
|
1956
2124
|
<dt class="tag-source">Source:</dt>
|
|
1957
2125
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
1958
|
-
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#
|
|
2126
|
+
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line362">line 362</a>
|
|
1959
2127
|
</li></ul></dd>
|
|
1960
2128
|
|
|
1961
2129
|
|
|
@@ -2116,7 +2284,7 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
|
|
|
2116
2284
|
|
|
2117
2285
|
<dt class="tag-source">Source:</dt>
|
|
2118
2286
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
2119
|
-
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#
|
|
2287
|
+
<a href="WOrmMongodb.mjs.html">WOrmMongodb.mjs</a>, <a href="WOrmMongodb.mjs.html#line1238">line 1238</a>
|
|
2120
2288
|
</li></ul></dd>
|
|
2121
2289
|
|
|
2122
2290
|
|
|
@@ -2273,7 +2441,7 @@ opt.autoGenPk為false時套件一律不產生id,未帶有效id者以reject拋
|
|
|
2273
2441
|
<br class="clear">
|
|
2274
2442
|
|
|
2275
2443
|
<footer>
|
|
2276
|
-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026
|
|
2444
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 11:28:35 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
2277
2445
|
</footer>
|
|
2278
2446
|
|
|
2279
2447
|
<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#.selectByPk">selectByPk</a></li><li data-type='method'><a href="WOrmMongodb.html#.selectByPkGfs">selectByPkGfs</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#.insertBulk">insertBulk</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
|
|
|
@@ -49,13 +49,14 @@
|
|
|
49
49
|
import stream from 'stream'
|
|
50
50
|
import cloneDeep from 'lodash-es/cloneDeep.js'
|
|
51
51
|
import every from 'lodash-es/every.js'
|
|
52
|
+
import filter from 'lodash-es/filter.js'
|
|
52
53
|
import get from 'lodash-es/get.js'
|
|
53
54
|
import map from 'lodash-es/map.js'
|
|
54
55
|
import omit from 'lodash-es/omit.js'
|
|
55
56
|
import size from 'lodash-es/size.js'
|
|
56
57
|
import evem from 'wsemi/src/evem.mjs'
|
|
57
58
|
import genPm from 'wsemi/src/genPm.mjs'
|
|
58
|
-
import
|
|
59
|
+
import genIDSeq from 'wsemi/src/genIDSeq.mjs'
|
|
59
60
|
import isbol from 'wsemi/src/isbol.mjs'
|
|
60
61
|
import isestr from 'wsemi/src/isestr.mjs'
|
|
61
62
|
import isarr from 'wsemi/src/isarr.mjs'
|
|
@@ -110,6 +111,10 @@ function WOrmMongodb(opt = {}) {
|
|
|
110
111
|
let _indexGfsReady = false
|
|
111
112
|
|
|
112
113
|
|
|
114
|
+
//_canUseTx, 拓樸判定只須一次, 以旗標記錄避免每次操作皆多一次round-trip
|
|
115
|
+
let _canUseTx = null
|
|
116
|
+
|
|
117
|
+
|
|
113
118
|
//ee, 採wsemi之evem(即eventemitter3), 其於'error'無監聽者時僅回傳false而不拋出,
|
|
114
119
|
//故本套件之操作行為不因呼叫端有無註冊監聽而改變; Node內建之EventEmitter具該拋出語義, 不可用
|
|
115
120
|
let ee = evem()
|
|
@@ -201,8 +206,8 @@ function WOrmMongodb(opt = {}) {
|
|
|
201
206
|
throw new Error(`invalid data[${k}].id, autoGenPk is false`)
|
|
202
207
|
}
|
|
203
208
|
|
|
204
|
-
//
|
|
205
|
-
v.id =
|
|
209
|
+
//genIDSeq
|
|
210
|
+
v.id = genIDSeq()
|
|
206
211
|
|
|
207
212
|
}
|
|
208
213
|
|
|
@@ -561,6 +566,208 @@ function WOrmMongodb(opt = {}) {
|
|
|
561
566
|
}
|
|
562
567
|
|
|
563
568
|
|
|
569
|
+
/**
|
|
570
|
+
* 判定本次部署是否支援交易
|
|
571
|
+
* 註: MongoDB之交易須replica set或分片叢集,standalone不支援
|
|
572
|
+
* 判定依據為hello回應:replica set帶setName,連至mongos者msg為isdbgrid,standalone兩者皆無
|
|
573
|
+
*
|
|
574
|
+
* @ignore
|
|
575
|
+
* @param {Object} client 輸入MongoClient物件
|
|
576
|
+
* @returns {Promise} 回傳Promise,resolve回傳是否支援交易布林值,reject回傳錯誤訊息
|
|
577
|
+
*/
|
|
578
|
+
async function canUseTx(client) {
|
|
579
|
+
|
|
580
|
+
//check
|
|
581
|
+
if (isbol(_canUseTx)) {
|
|
582
|
+
return _canUseTx
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
//hello
|
|
586
|
+
let hello = await client.db(opt.db).admin().command({ hello: 1 })
|
|
587
|
+
|
|
588
|
+
//_canUseTx
|
|
589
|
+
_canUseTx = isestr(get(hello, 'setName')) || get(hello, 'msg') === 'isdbgrid'
|
|
590
|
+
|
|
591
|
+
return _canUseTx
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* 以交易達成insertBulk之全有全無
|
|
597
|
+
* 交易內任一筆失敗即整批回滾,故不會寫入任何一筆,無須補償動作
|
|
598
|
+
*
|
|
599
|
+
* @ignore
|
|
600
|
+
* @param {Object} client 輸入MongoClient物件
|
|
601
|
+
* @param {Object} collection 輸入資料表物件
|
|
602
|
+
* @param {Array} data 輸入數據陣列
|
|
603
|
+
* @returns {Promise} 回傳Promise,resolve回傳undefined,reject回傳錯誤訊息
|
|
604
|
+
*/
|
|
605
|
+
async function _insertBulkByTx(client, collection, data) {
|
|
606
|
+
|
|
607
|
+
//session
|
|
608
|
+
let session = client.startSession()
|
|
609
|
+
|
|
610
|
+
try {
|
|
611
|
+
|
|
612
|
+
//withTransaction, ordered:true令首筆衝突即停止, 由交易回滾保證未寫入任何一筆
|
|
613
|
+
await session.withTransaction(async () => {
|
|
614
|
+
await collection.insertMany(data, { session, ordered: true })
|
|
615
|
+
})
|
|
616
|
+
|
|
617
|
+
}
|
|
618
|
+
finally {
|
|
619
|
+
await session.endSession()
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* 以補償動作達成insertBulk之全有全無,供無交易可用之standalone使用
|
|
627
|
+
* 註: 補償係刪除本次呼叫已寫入者,其_id由驅動於送出前在用戶端產生,
|
|
628
|
+
* 與呼叫前既有數據之_id必不相同,故縱使該筆之id已存在亦不會誤刪既有數據;
|
|
629
|
+
* 刪除未寫入者為無操作,故不須精確得知哪幾筆已寫入
|
|
630
|
+
* 註: 行程若於補償途中中止(斷電、強制終止),已寫入之部份可能殘留,此為無交易可用之固有限制
|
|
631
|
+
*
|
|
632
|
+
* @ignore
|
|
633
|
+
* @param {Object} collection 輸入資料表物件
|
|
634
|
+
* @param {Array} data 輸入數據陣列
|
|
635
|
+
* @returns {Promise} 回傳Promise,resolve回傳undefined,reject回傳錯誤訊息
|
|
636
|
+
*/
|
|
637
|
+
async function _insertBulkByCompensate(collection, data) {
|
|
638
|
+
|
|
639
|
+
//errIns
|
|
640
|
+
let errIns = null
|
|
641
|
+
|
|
642
|
+
try {
|
|
643
|
+
await collection.insertMany(data, { ordered: false })
|
|
644
|
+
}
|
|
645
|
+
catch (err) {
|
|
646
|
+
errIns = err
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
//check, 全數插入成功即無須補償
|
|
650
|
+
if (errIns === null) {
|
|
651
|
+
return
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
//idDels, 取本次由驅動所產生之全部_id
|
|
655
|
+
let idDels = map(data, function(v) {
|
|
656
|
+
return v._id
|
|
657
|
+
})
|
|
658
|
+
idDels = filter(idDels, function(v) {
|
|
659
|
+
return v !== undefined && v !== null
|
|
660
|
+
})
|
|
661
|
+
|
|
662
|
+
//補償, 刪除本次已寫入者
|
|
663
|
+
if (size(idDels) > 0) {
|
|
664
|
+
await collection.deleteMany({ _id: { $in: idDels } })
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
throw errIns
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* 批次插入數據,全批視為一個單位:全部插入成功,或一筆都不寫入
|
|
673
|
+
* 任一筆之id已存在(含同批含重複id)即以reject拋出整批性錯誤,且不寫入任何一筆
|
|
674
|
+
* 註: 本函數非insert之加速版,兩者衝突政策不同——insert跳過已存在者而整批ok為1,
|
|
675
|
+
* 本函數則整批reject;需要逐筆處置者改用insert
|
|
676
|
+
* 註: n為輸入筆數,nInserted於成功時恆等於n;不提供逐筆結果,故不出現ok為0與err
|
|
677
|
+
* 註: opt.autoGenPk為true(預設)時未帶有效id者自動產生,為false時未帶有效id即reject且同批皆不寫入
|
|
678
|
+
* 註: 全有全無之達成方式依部署而異——具replica set或分片叢集者以交易包覆,
|
|
679
|
+
* standalone無交易可用而以補償動作達成,其限制詳見_insertBulkByCompensate
|
|
680
|
+
*
|
|
681
|
+
* @memberOf WOrmMongodb
|
|
682
|
+
* @param {Object|Array} data 輸入數據物件或陣列
|
|
683
|
+
* @returns {Promise} 回傳Promise,resolve回傳插入結果,reject回傳錯誤訊息
|
|
684
|
+
*/
|
|
685
|
+
async function insertBulk(data) {
|
|
686
|
+
let isErr = false
|
|
687
|
+
|
|
688
|
+
//check
|
|
689
|
+
if (!iseobj(data) && !isearr(data)) {
|
|
690
|
+
return {
|
|
691
|
+
n: 0,
|
|
692
|
+
nInserted: 0,
|
|
693
|
+
ok: 1,
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
//cloneDeep
|
|
698
|
+
data = cloneDeep(data)
|
|
699
|
+
|
|
700
|
+
//client
|
|
701
|
+
let client = new MongoClient(opt.url)
|
|
702
|
+
|
|
703
|
+
//res
|
|
704
|
+
let res = null
|
|
705
|
+
try {
|
|
706
|
+
|
|
707
|
+
//connect, 令連線失敗於寫入前即拋出, 屬影響全部數據之整批性錯誤
|
|
708
|
+
await client.connect()
|
|
709
|
+
|
|
710
|
+
//database, collection
|
|
711
|
+
let database = client.db(opt.db)
|
|
712
|
+
let collection = database.collection(opt.cl)
|
|
713
|
+
|
|
714
|
+
//check
|
|
715
|
+
if (!isarr(data)) {
|
|
716
|
+
data = [data]
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
//check id, 須於任何寫入之前一次完成
|
|
720
|
+
data = map(data, procPk)
|
|
721
|
+
|
|
722
|
+
//ensureIndex
|
|
723
|
+
await ensureIndex(collection)
|
|
724
|
+
|
|
725
|
+
//nAll, n之基準為輸入筆數
|
|
726
|
+
let nAll = size(data)
|
|
727
|
+
|
|
728
|
+
//check, 有交易可用者一律優先採用交易, 其回滾即為全有全無之保證
|
|
729
|
+
let bTx = await canUseTx(client)
|
|
730
|
+
if (bTx) {
|
|
731
|
+
await _insertBulkByTx(client, collection, data)
|
|
732
|
+
}
|
|
733
|
+
else {
|
|
734
|
+
await _insertBulkByCompensate(collection, data)
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
//res, 執行至此表全數插入成功, 故nInserted恆等於n
|
|
738
|
+
res = {
|
|
739
|
+
n: nAll,
|
|
740
|
+
nInserted: nAll,
|
|
741
|
+
ok: 1,
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
}
|
|
745
|
+
catch (err) {
|
|
746
|
+
isErr = true
|
|
747
|
+
res = err
|
|
748
|
+
}
|
|
749
|
+
finally {
|
|
750
|
+
await client.close()
|
|
751
|
+
client = null
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
//emit
|
|
755
|
+
if (!isErr) {
|
|
756
|
+
emitChange('insertBulk', data, res)
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
//check
|
|
760
|
+
if (isErr) {
|
|
761
|
+
|
|
762
|
+
//emit, 整批性錯誤須於reject之前發出
|
|
763
|
+
emitError('insertBulk', data, res)
|
|
764
|
+
|
|
765
|
+
return Promise.reject(res)
|
|
766
|
+
}
|
|
767
|
+
return res
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
|
|
564
771
|
/**
|
|
565
772
|
* 儲存單筆數據,由MongoDB於單一updateOne內原子完成[查找id]與[更新或插入]
|
|
566
773
|
* 註: MongoDB單一文件操作本即為原子,故不須開啟transaction(transaction另須replica set,standalone不支援)
|
|
@@ -1427,6 +1634,7 @@ function WOrmMongodb(opt = {}) {
|
|
|
1427
1634
|
ee.select = select
|
|
1428
1635
|
ee.selectByPk = selectByPk
|
|
1429
1636
|
ee.insert = insert
|
|
1637
|
+
ee.insertBulk = insertBulk
|
|
1430
1638
|
ee.save = save
|
|
1431
1639
|
ee.del = del
|
|
1432
1640
|
ee.delAll = delAll
|
|
@@ -1455,7 +1663,7 @@ export default WOrmMongodb
|
|
|
1455
1663
|
<br class="clear">
|
|
1456
1664
|
|
|
1457
1665
|
<footer>
|
|
1458
|
-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026
|
|
1666
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 11:28:35 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
1459
1667
|
</footer>
|
|
1460
1668
|
|
|
1461
1669
|
<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#.selectByPk">selectByPk</a></li><li data-type='method'><a href="WOrmMongodb.html#.selectByPkGfs">selectByPkGfs</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#.insertBulk">insertBulk</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 Mon Aug 17 2026
|
|
74
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 11:28:35 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
|
@@ -46,11 +46,17 @@ async function test() {
|
|
|
46
46
|
//wo
|
|
47
47
|
let wo = WOrm(opt)
|
|
48
48
|
|
|
49
|
-
//on
|
|
49
|
+
//on change, 資料實際異動成功後發出
|
|
50
50
|
wo.on('change', function(mode, data, res) {
|
|
51
51
|
console.log('change', mode)
|
|
52
52
|
})
|
|
53
53
|
|
|
54
|
+
//on error, 操作發生錯誤時發出, 整批性錯誤於reject前、逐筆失敗於該筆定案後
|
|
55
|
+
//註: 事件僅為附加通知, 錯誤仍可由reject或逐筆之err欄位取得; 正常結果不會發出
|
|
56
|
+
wo.on('error', function(mode, data, err) {
|
|
57
|
+
console.log('error', mode, err)
|
|
58
|
+
})
|
|
59
|
+
|
|
54
60
|
//delAll
|
|
55
61
|
await wo.delAll()
|
|
56
62
|
.then(function(msg) {
|
package/g-gfs.mjs
CHANGED
|
@@ -24,11 +24,17 @@ async function test() {
|
|
|
24
24
|
//wo
|
|
25
25
|
let wo = WOrm(opt)
|
|
26
26
|
|
|
27
|
-
//on
|
|
27
|
+
//on change, 資料實際異動成功後發出
|
|
28
28
|
wo.on('change', function(mode, data, res) {
|
|
29
29
|
console.log('change', mode)
|
|
30
30
|
})
|
|
31
31
|
|
|
32
|
+
//on error, 操作發生錯誤時發出, 整批性錯誤於reject前、逐筆失敗於該筆定案後
|
|
33
|
+
//註: 事件僅為附加通知, 錯誤仍可由reject或逐筆之err欄位取得; 正常結果不會發出
|
|
34
|
+
wo.on('error', function(mode, data, err) {
|
|
35
|
+
console.log('error', mode, err)
|
|
36
|
+
})
|
|
37
|
+
|
|
32
38
|
//u8a, 亦可為瀏覽器或nodejs取得之任何Uint8Array
|
|
33
39
|
let u8a = genU8a(1000)
|
|
34
40
|
|