w-orm-postgresql 1.0.12 → 1.0.13
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 +27 -0
- package/dist/w-orm-postgresql.umd.js +2 -2
- package/dist/w-orm-postgresql.umd.js.map +1 -1
- package/docs/WOrmPostgresql.html +145 -14
- package/docs/WOrmPostgresql.mjs.html +96 -14
- package/docs/index.html +1 -1
- package/g-basic.mjs +27 -0
- package/package.json +1 -1
- package/src/WOrmPostgresql.mjs +95 -13
- package/test/api-postgresql.test.mjs +192 -0
- package/toolg/gDistRollup.mjs +1 -1
package/docs/WOrmPostgresql.html
CHANGED
|
@@ -514,7 +514,7 @@
|
|
|
514
514
|
|
|
515
515
|
<dt class="tag-source">Source:</dt>
|
|
516
516
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
517
|
-
<a href="WOrmPostgresql.mjs.html">WOrmPostgresql.mjs</a>, <a href="WOrmPostgresql.mjs.html#
|
|
517
|
+
<a href="WOrmPostgresql.mjs.html">WOrmPostgresql.mjs</a>, <a href="WOrmPostgresql.mjs.html#line387">line 387</a>
|
|
518
518
|
</li></ul></dd>
|
|
519
519
|
|
|
520
520
|
|
|
@@ -771,7 +771,7 @@
|
|
|
771
771
|
|
|
772
772
|
<dt class="tag-source">Source:</dt>
|
|
773
773
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
774
|
-
<a href="WOrmPostgresql.mjs.html">WOrmPostgresql.mjs</a>, <a href="WOrmPostgresql.mjs.html#
|
|
774
|
+
<a href="WOrmPostgresql.mjs.html">WOrmPostgresql.mjs</a>, <a href="WOrmPostgresql.mjs.html#line1344">line 1344</a>
|
|
775
775
|
</li></ul></dd>
|
|
776
776
|
|
|
777
777
|
|
|
@@ -934,7 +934,7 @@
|
|
|
934
934
|
|
|
935
935
|
<dt class="tag-source">Source:</dt>
|
|
936
936
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
937
|
-
<a href="WOrmPostgresql.mjs.html">WOrmPostgresql.mjs</a>, <a href="WOrmPostgresql.mjs.html#
|
|
937
|
+
<a href="WOrmPostgresql.mjs.html">WOrmPostgresql.mjs</a>, <a href="WOrmPostgresql.mjs.html#line1505">line 1505</a>
|
|
938
938
|
</li></ul></dd>
|
|
939
939
|
|
|
940
940
|
|
|
@@ -1096,7 +1096,7 @@
|
|
|
1096
1096
|
|
|
1097
1097
|
|
|
1098
1098
|
|
|
1099
|
-
<h4 class="name" id=".insert"><span class="type-signature type-signature-async, static">(async, static) </span>insert<span class="signature">(data)</span><span class="type-signature"> → {Promise}</span></h4>
|
|
1099
|
+
<h4 class="name" id=".insert"><span class="type-signature type-signature-async, static">(async, static) </span>insert<span class="signature">(data, option<span class="signature-attributes">opt</span>)</span><span class="type-signature"> → {Promise}</span></h4>
|
|
1100
1100
|
|
|
1101
1101
|
|
|
1102
1102
|
|
|
@@ -1107,15 +1107,19 @@
|
|
|
1107
1107
|
|
|
1108
1108
|
<dt class="tag-description">Description:</dt>
|
|
1109
1109
|
<dd class="tag-description"><ul class="dummy"><li><p>插入數據,僅於主鍵不存在時寫入,已存在者跳過且不覆寫</p>
|
|
1110
|
-
<p
|
|
1111
|
-
|
|
1112
|
-
因主鍵得承載業務語義,未帶有效主鍵值時不自動補值,一律以reject回報。</p
|
|
1110
|
+
<p>預設回傳單一聚合物件,n為輸入筆數即本次嘗試插入之基準;nInserted為實際插入筆數,
|
|
1111
|
+
全數已存在而nInserted為0屬正常結果,不視為錯誤。同批含重複主鍵時僅首筆計入nInserted。
|
|
1112
|
+
因主鍵得承載業務語義,未帶有效主鍵值時不自動補值,一律以reject回報。</p>
|
|
1113
|
+
<p>option.returnList為true時改回傳與輸入等長且保序之逐筆陣列,令呼叫端得知[是哪幾筆]為新數據,
|
|
1114
|
+
而非僅知[有幾筆]。各元素之n恆為1、ok恆為1,因insert之任何錯誤皆屬整批性錯誤而reject,
|
|
1115
|
+
故逐筆元素不出現ok:0與err;同批含重複主鍵者以首次出現者之nInserted為1,其餘為0。
|
|
1116
|
+
兩種取值下之不變式為陣列長度等於輸入筆數,且nInserted為1者之筆數等於聚合模式之nInserted。</p></li></ul></dd>
|
|
1113
1117
|
|
|
1114
1118
|
|
|
1115
1119
|
|
|
1116
1120
|
<dt class="tag-source">Source:</dt>
|
|
1117
1121
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
1118
|
-
<a href="WOrmPostgresql.mjs.html">WOrmPostgresql.mjs</a>, <a href="WOrmPostgresql.mjs.html#
|
|
1122
|
+
<a href="WOrmPostgresql.mjs.html">WOrmPostgresql.mjs</a>, <a href="WOrmPostgresql.mjs.html#line731">line 731</a>
|
|
1119
1123
|
</li></ul></dd>
|
|
1120
1124
|
|
|
1121
1125
|
|
|
@@ -1175,8 +1179,12 @@
|
|
|
1175
1179
|
<th>Type</th>
|
|
1176
1180
|
|
|
1177
1181
|
|
|
1182
|
+
<th>Attributes</th>
|
|
1183
|
+
|
|
1178
1184
|
|
|
1179
1185
|
|
|
1186
|
+
<th>Default</th>
|
|
1187
|
+
|
|
1180
1188
|
|
|
1181
1189
|
<th class="last">Description</th>
|
|
1182
1190
|
</tr>
|
|
@@ -1204,13 +1212,135 @@
|
|
|
1204
1212
|
</td>
|
|
1205
1213
|
|
|
1206
1214
|
|
|
1215
|
+
<td class="attributes">
|
|
1216
|
+
|
|
1217
|
+
|
|
1218
|
+
|
|
1219
|
+
|
|
1220
|
+
|
|
1221
|
+
</td>
|
|
1222
|
+
|
|
1207
1223
|
|
|
1208
1224
|
|
|
1225
|
+
<td class="default">
|
|
1226
|
+
|
|
1227
|
+
</td>
|
|
1228
|
+
|
|
1209
1229
|
|
|
1210
1230
|
<td class="description last"><p>輸入數據物件或陣列</p></td>
|
|
1211
1231
|
</tr>
|
|
1212
1232
|
|
|
1213
1233
|
|
|
1234
|
+
|
|
1235
|
+
<tr>
|
|
1236
|
+
|
|
1237
|
+
<td class="name"><code>option</code></td>
|
|
1238
|
+
|
|
1239
|
+
|
|
1240
|
+
<td class="type">
|
|
1241
|
+
|
|
1242
|
+
|
|
1243
|
+
<span class="param-type">Object</span>
|
|
1244
|
+
|
|
1245
|
+
|
|
1246
|
+
|
|
1247
|
+
|
|
1248
|
+
</td>
|
|
1249
|
+
|
|
1250
|
+
|
|
1251
|
+
<td class="attributes">
|
|
1252
|
+
|
|
1253
|
+
<optional><br>
|
|
1254
|
+
|
|
1255
|
+
|
|
1256
|
+
|
|
1257
|
+
|
|
1258
|
+
|
|
1259
|
+
</td>
|
|
1260
|
+
|
|
1261
|
+
|
|
1262
|
+
|
|
1263
|
+
<td class="default">
|
|
1264
|
+
|
|
1265
|
+
<code>{}</code>
|
|
1266
|
+
|
|
1267
|
+
</td>
|
|
1268
|
+
|
|
1269
|
+
|
|
1270
|
+
<td class="description last"><p>輸入設定物件,預設為{}</p>
|
|
1271
|
+
<h6>Properties</h6>
|
|
1272
|
+
|
|
1273
|
+
|
|
1274
|
+
<table class="params">
|
|
1275
|
+
<thead>
|
|
1276
|
+
<tr>
|
|
1277
|
+
|
|
1278
|
+
<th>Name</th>
|
|
1279
|
+
|
|
1280
|
+
|
|
1281
|
+
<th>Type</th>
|
|
1282
|
+
|
|
1283
|
+
|
|
1284
|
+
<th>Attributes</th>
|
|
1285
|
+
|
|
1286
|
+
|
|
1287
|
+
|
|
1288
|
+
<th>Default</th>
|
|
1289
|
+
|
|
1290
|
+
|
|
1291
|
+
<th class="last">Description</th>
|
|
1292
|
+
</tr>
|
|
1293
|
+
</thead>
|
|
1294
|
+
|
|
1295
|
+
<tbody>
|
|
1296
|
+
|
|
1297
|
+
|
|
1298
|
+
<tr>
|
|
1299
|
+
|
|
1300
|
+
<td class="name"><code>returnList</code></td>
|
|
1301
|
+
|
|
1302
|
+
|
|
1303
|
+
<td class="type">
|
|
1304
|
+
|
|
1305
|
+
|
|
1306
|
+
<span class="param-type">Boolean</span>
|
|
1307
|
+
|
|
1308
|
+
|
|
1309
|
+
|
|
1310
|
+
|
|
1311
|
+
</td>
|
|
1312
|
+
|
|
1313
|
+
|
|
1314
|
+
<td class="attributes">
|
|
1315
|
+
|
|
1316
|
+
<optional><br>
|
|
1317
|
+
|
|
1318
|
+
|
|
1319
|
+
|
|
1320
|
+
|
|
1321
|
+
|
|
1322
|
+
</td>
|
|
1323
|
+
|
|
1324
|
+
|
|
1325
|
+
|
|
1326
|
+
<td class="default">
|
|
1327
|
+
|
|
1328
|
+
<code>false</code>
|
|
1329
|
+
|
|
1330
|
+
</td>
|
|
1331
|
+
|
|
1332
|
+
|
|
1333
|
+
<td class="description last"><p>輸入是否改回傳逐筆結果陣列,預設為false</p></td>
|
|
1334
|
+
</tr>
|
|
1335
|
+
|
|
1336
|
+
|
|
1337
|
+
</tbody>
|
|
1338
|
+
</table>
|
|
1339
|
+
|
|
1340
|
+
</td>
|
|
1341
|
+
</tr>
|
|
1342
|
+
|
|
1343
|
+
|
|
1214
1344
|
</tbody>
|
|
1215
1345
|
</table>
|
|
1216
1346
|
|
|
@@ -1233,7 +1363,8 @@
|
|
|
1233
1363
|
|
|
1234
1364
|
|
|
1235
1365
|
<div class="param-desc">
|
|
1236
|
-
<p>回傳Promise,resolve
|
|
1366
|
+
<p>回傳Promise,resolve於returnList為false時回傳插入結果物件{n,nInserted,ok},
|
|
1367
|
+
為true時回傳逐筆結果陣列[{n,nInserted,ok}],reject回傳錯誤訊息</p>
|
|
1237
1368
|
</div>
|
|
1238
1369
|
|
|
1239
1370
|
|
|
@@ -1282,7 +1413,7 @@ n為輸入筆數,nInserted為實際插入筆數且成功時恆等於n。
|
|
|
1282
1413
|
|
|
1283
1414
|
<dt class="tag-source">Source:</dt>
|
|
1284
1415
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
1285
|
-
<a href="WOrmPostgresql.mjs.html">WOrmPostgresql.mjs</a>, <a href="WOrmPostgresql.mjs.html#
|
|
1416
|
+
<a href="WOrmPostgresql.mjs.html">WOrmPostgresql.mjs</a>, <a href="WOrmPostgresql.mjs.html#line865">line 865</a>
|
|
1286
1417
|
</li></ul></dd>
|
|
1287
1418
|
|
|
1288
1419
|
|
|
@@ -1450,7 +1581,7 @@ nInserted與nModified恆同時出現,無對應行為時填0。
|
|
|
1450
1581
|
|
|
1451
1582
|
<dt class="tag-source">Source:</dt>
|
|
1452
1583
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
1453
|
-
<a href="WOrmPostgresql.mjs.html">WOrmPostgresql.mjs</a>, <a href="WOrmPostgresql.mjs.html#
|
|
1584
|
+
<a href="WOrmPostgresql.mjs.html">WOrmPostgresql.mjs</a>, <a href="WOrmPostgresql.mjs.html#line985">line 985</a>
|
|
1454
1585
|
</li></ul></dd>
|
|
1455
1586
|
|
|
1456
1587
|
|
|
@@ -1737,7 +1868,7 @@ nInserted與nModified恆同時出現,無對應行為時填0。
|
|
|
1737
1868
|
|
|
1738
1869
|
<dt class="tag-source">Source:</dt>
|
|
1739
1870
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
1740
|
-
<a href="WOrmPostgresql.mjs.html">WOrmPostgresql.mjs</a>, <a href="WOrmPostgresql.mjs.html#
|
|
1871
|
+
<a href="WOrmPostgresql.mjs.html">WOrmPostgresql.mjs</a>, <a href="WOrmPostgresql.mjs.html#line487">line 487</a>
|
|
1741
1872
|
</li></ul></dd>
|
|
1742
1873
|
|
|
1743
1874
|
|
|
@@ -1958,7 +2089,7 @@ nInserted與nModified恆同時出現,無對應行為時填0。
|
|
|
1958
2089
|
|
|
1959
2090
|
<dt class="tag-source">Source:</dt>
|
|
1960
2091
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
1961
|
-
<a href="WOrmPostgresql.mjs.html">WOrmPostgresql.mjs</a>, <a href="WOrmPostgresql.mjs.html#
|
|
2092
|
+
<a href="WOrmPostgresql.mjs.html">WOrmPostgresql.mjs</a>, <a href="WOrmPostgresql.mjs.html#line590">line 590</a>
|
|
1962
2093
|
</li></ul></dd>
|
|
1963
2094
|
|
|
1964
2095
|
|
|
@@ -2121,7 +2252,7 @@ nInserted與nModified恆同時出現,無對應行為時填0。
|
|
|
2121
2252
|
<br class="clear">
|
|
2122
2253
|
|
|
2123
2254
|
<footer>
|
|
2124
|
-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026
|
|
2255
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 18:44:49 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
2125
2256
|
</footer>
|
|
2126
2257
|
|
|
2127
2258
|
<script>prettyPrint();</script>
|
|
@@ -299,7 +299,7 @@ function WOrmPostgresql(opt = {}) {
|
|
|
299
299
|
//兩函數之衝突政策由useConflict區分, 語句各自獨立:
|
|
300
300
|
// true : 添加ON CONFLICT DO NOTHING, 已存在主鍵者跳過而不中斷整批, 為insert之語義
|
|
301
301
|
// false: 不添加, 任一筆撞主鍵之唯一約束即整句失敗且不寫入任何一筆, 為insertBulk之全有全無語義
|
|
302
|
-
let insertBatches = async (client, data, useConflict) => {
|
|
302
|
+
let insertBatches = async (client, data, useConflict, returnPk = false) => {
|
|
303
303
|
|
|
304
304
|
//nCols, 取各筆欄位數之最大值
|
|
305
305
|
let nCols = 1
|
|
@@ -321,8 +321,9 @@ function WOrmPostgresql(opt = {}) {
|
|
|
321
321
|
|
|
322
322
|
try {
|
|
323
323
|
|
|
324
|
-
//nIns
|
|
324
|
+
//nIns, rows
|
|
325
325
|
let nIns = 0
|
|
326
|
+
let rows = []
|
|
326
327
|
|
|
327
328
|
//each batch
|
|
328
329
|
for (let dt of ltdata) {
|
|
@@ -339,6 +340,10 @@ function WOrmPostgresql(opt = {}) {
|
|
|
339
340
|
if (useConflict) {
|
|
340
341
|
sql = `${sql} ON CONFLICT (${pkName}) DO NOTHING`
|
|
341
342
|
}
|
|
343
|
+
if (returnPk) {
|
|
344
|
+
//RETURNING僅回傳實際插入之列, 供逐筆結果映回輸入序
|
|
345
|
+
sql = `${sql} RETURNING ${pkName}`
|
|
346
|
+
}
|
|
342
347
|
|
|
343
348
|
//insert
|
|
344
349
|
let r = await client.query(sql, mr.values)
|
|
@@ -346,12 +351,20 @@ function WOrmPostgresql(opt = {}) {
|
|
|
346
351
|
//nIns
|
|
347
352
|
nIns += r.rowCount
|
|
348
353
|
|
|
354
|
+
//rows, 分批時逐批收集再串接
|
|
355
|
+
if (returnPk) {
|
|
356
|
+
rows = rows.concat(get(r, 'rows', []))
|
|
357
|
+
}
|
|
358
|
+
|
|
349
359
|
}
|
|
350
360
|
|
|
351
361
|
//commit
|
|
352
362
|
await client.query('COMMIT')
|
|
353
363
|
|
|
354
|
-
return
|
|
364
|
+
return {
|
|
365
|
+
nIns,
|
|
366
|
+
rows,
|
|
367
|
+
}
|
|
355
368
|
}
|
|
356
369
|
catch (err) {
|
|
357
370
|
|
|
@@ -363,6 +376,50 @@ function WOrmPostgresql(opt = {}) {
|
|
|
363
376
|
|
|
364
377
|
}
|
|
365
378
|
|
|
379
|
+
//genInsertList, 依RETURNING所回傳之主鍵值映回輸入序, 產生與輸入等長且保序之逐筆結果
|
|
380
|
+
//後端回傳之型別未必與輸入相同(如TIMESTAMPTZ欄位輸入字串而回傳Date物件), 直接比對必然落空,
|
|
381
|
+
//故依回傳值之實際型別決定正規化方式, 且兩側共用同一函數
|
|
382
|
+
//不採[一律嘗試解析為日期]之通用寫法, 因主鍵為文字欄位而值形如'2025'與'2025-01-01'者
|
|
383
|
+
//將正規化為同值而誤判, 依實際型別分流則無此問題
|
|
384
|
+
//同批含重複主鍵時RETURNING僅回傳一次, 故以首次出現者為已插入, 其餘視為已存在而跳過
|
|
385
|
+
let genInsertList = (data, rows) => {
|
|
386
|
+
|
|
387
|
+
//isDateCol, 以回傳值之實際型別判定
|
|
388
|
+
let isDateCol = size(rows) > 0 && rows[0][pkName] instanceof Date
|
|
389
|
+
|
|
390
|
+
//normPk
|
|
391
|
+
let normPk = (v) => {
|
|
392
|
+
if (isDateCol) {
|
|
393
|
+
return new Date(v).toISOString()
|
|
394
|
+
}
|
|
395
|
+
return String(v)
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
//setIns, 實際插入之主鍵
|
|
399
|
+
let setIns = new Set()
|
|
400
|
+
rows.forEach(function(v) {
|
|
401
|
+
setIns.add(normPk(get(v, pkName)))
|
|
402
|
+
})
|
|
403
|
+
|
|
404
|
+
//ltres
|
|
405
|
+
let setSeen = new Set()
|
|
406
|
+
return map(data, function(v) {
|
|
407
|
+
|
|
408
|
+
//pkv
|
|
409
|
+
let pkv = normPk(get(v, pkName))
|
|
410
|
+
|
|
411
|
+
//inserted
|
|
412
|
+
let inserted = setIns.has(pkv) && !setSeen.has(pkv)
|
|
413
|
+
setSeen.add(pkv)
|
|
414
|
+
|
|
415
|
+
return {
|
|
416
|
+
n: 1,
|
|
417
|
+
nInserted: inserted ? 1 : 0,
|
|
418
|
+
ok: 1,
|
|
419
|
+
}
|
|
420
|
+
})
|
|
421
|
+
}
|
|
422
|
+
|
|
366
423
|
/**
|
|
367
424
|
* 創建資料表
|
|
368
425
|
* 註: pk未給時採建構時之opt.pk。因insert與save倚賴主鍵之唯一約束達成原子性,
|
|
@@ -702,20 +759,38 @@ function WOrmPostgresql(opt = {}) {
|
|
|
702
759
|
/**
|
|
703
760
|
* 插入數據,僅於主鍵不存在時寫入,已存在者跳過且不覆寫
|
|
704
761
|
*
|
|
705
|
-
* n
|
|
706
|
-
*
|
|
762
|
+
* 預設回傳單一聚合物件,n為輸入筆數即本次嘗試插入之基準;nInserted為實際插入筆數,
|
|
763
|
+
* 全數已存在而nInserted為0屬正常結果,不視為錯誤。同批含重複主鍵時僅首筆計入nInserted。
|
|
707
764
|
* 因主鍵得承載業務語義,未帶有效主鍵值時不自動補值,一律以reject回報。
|
|
708
765
|
*
|
|
766
|
+
* option.returnList為true時改回傳與輸入等長且保序之逐筆陣列,令呼叫端得知[是哪幾筆]為新數據,
|
|
767
|
+
* 而非僅知[有幾筆]。各元素之n恆為1、ok恆為1,因insert之任何錯誤皆屬整批性錯誤而reject,
|
|
768
|
+
* 故逐筆元素不出現ok:0與err;同批含重複主鍵者以首次出現者之nInserted為1,其餘為0。
|
|
769
|
+
* 兩種取值下之不變式為陣列長度等於輸入筆數,且nInserted為1者之筆數等於聚合模式之nInserted。
|
|
770
|
+
*
|
|
709
771
|
* @memberOf WOrmPostgresql
|
|
710
772
|
* @param {Object|Array} data 輸入數據物件或陣列
|
|
711
|
-
* @
|
|
773
|
+
* @param {Object} [option={}] 輸入設定物件,預設為{}
|
|
774
|
+
* @param {Boolean} [option.returnList=false] 輸入是否改回傳逐筆結果陣列,預設為false
|
|
775
|
+
* @returns {Promise} 回傳Promise,resolve於returnList為false時回傳插入結果物件{n,nInserted,ok},
|
|
776
|
+
* 為true時回傳逐筆結果陣列[{n,nInserted,ok}],reject回傳錯誤訊息
|
|
712
777
|
*/
|
|
713
|
-
async function insert(data) {
|
|
778
|
+
async function insert(data, option = {}) {
|
|
714
779
|
let isErr = false
|
|
715
780
|
let res = null
|
|
716
781
|
|
|
782
|
+
//returnList
|
|
783
|
+
let returnList = get(option, 'returnList', false)
|
|
784
|
+
if (!isbol(returnList)) {
|
|
785
|
+
returnList = false
|
|
786
|
+
}
|
|
787
|
+
|
|
717
788
|
//check
|
|
789
|
+
//輸入無效時, returnList為true者回傳空陣列以對齊save與del之規定
|
|
718
790
|
if (!iseobj(data) && !isearr(data)) {
|
|
791
|
+
if (returnList) {
|
|
792
|
+
return []
|
|
793
|
+
}
|
|
719
794
|
return {
|
|
720
795
|
n: 0,
|
|
721
796
|
nInserted: 0,
|
|
@@ -773,13 +848,19 @@ function WOrmPostgresql(opt = {}) {
|
|
|
773
848
|
//由PostgreSQL於單一語句內原子完成[檢查主鍵未存在]與[寫入], 併發時同一主鍵僅有一次成功,
|
|
774
849
|
//同批含重複主鍵時亦僅首筆成功, 故不須逐筆插入即可取得實際插入筆數
|
|
775
850
|
//筆數過多而超出綁定參數上限者由insertBatches分批送出, 主鍵認定與genConflictSQL一致
|
|
776
|
-
|
|
851
|
+
//returnList為true時另附RETURNING, 以實際插入之主鍵映回輸入序而得逐筆結果
|
|
852
|
+
let { nIns, rows } = await insertBatches(client, data, true, returnList)
|
|
777
853
|
|
|
778
854
|
//res
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
855
|
+
if (returnList) {
|
|
856
|
+
res = genInsertList(data, rows)
|
|
857
|
+
}
|
|
858
|
+
else {
|
|
859
|
+
res = {
|
|
860
|
+
n: nAll,
|
|
861
|
+
nInserted: nIns,
|
|
862
|
+
ok: 1,
|
|
863
|
+
}
|
|
783
864
|
}
|
|
784
865
|
|
|
785
866
|
}
|
|
@@ -889,7 +970,8 @@ function WOrmPostgresql(opt = {}) {
|
|
|
889
970
|
|
|
890
971
|
//insertBulk, 不添加conflict, 令任一筆撞主鍵之唯一約束即整句失敗且不寫入任何一筆,
|
|
891
972
|
//同批含重複主鍵者亦於此被偵測為衝突; 筆數過多者由insertBatches分批送出並以交易維持全有全無
|
|
892
|
-
|
|
973
|
+
//不附RETURNING, 因成功時逐筆恆為已插入而nInserted恆等於n, 取回主鍵值無資訊量
|
|
974
|
+
let { nIns } = await insertBatches(client, data, false)
|
|
893
975
|
|
|
894
976
|
//res
|
|
895
977
|
res = {
|
|
@@ -1600,7 +1682,7 @@ export default WOrmPostgresql
|
|
|
1600
1682
|
<br class="clear">
|
|
1601
1683
|
|
|
1602
1684
|
<footer>
|
|
1603
|
-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026
|
|
1685
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 18:44:49 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
1604
1686
|
</footer>
|
|
1605
1687
|
|
|
1606
1688
|
<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
|
|
74
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Mon Aug 17 2026 18:44:49 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
|
@@ -123,6 +123,19 @@ let rsb = [
|
|
|
123
123
|
},
|
|
124
124
|
]
|
|
125
125
|
|
|
126
|
+
let rsr = [
|
|
127
|
+
{ //已存在, 為前次insertBulk所插入
|
|
128
|
+
time: '2025-01-01T00:20:00Z',
|
|
129
|
+
name: 'bulk-1',
|
|
130
|
+
value: 201,
|
|
131
|
+
},
|
|
132
|
+
{ //不存在須新增
|
|
133
|
+
time: '2025-01-01T00:22:00Z',
|
|
134
|
+
name: 'ret-new',
|
|
135
|
+
value: 203,
|
|
136
|
+
},
|
|
137
|
+
]
|
|
138
|
+
|
|
126
139
|
async function test() {
|
|
127
140
|
|
|
128
141
|
//wo
|
|
@@ -185,6 +198,15 @@ async function test() {
|
|
|
185
198
|
console.log('insertBulk by pk existed catch', `${msg}`) //reject回傳Error物件, 以樣板字串取其訊息以免印出堆疊
|
|
186
199
|
})
|
|
187
200
|
|
|
201
|
+
//insert by returnList, 改回傳與輸入等長且保序之逐筆結果, 令呼叫端得知是哪幾筆為新數據
|
|
202
|
+
await wo.insert(rsr, { returnList: true })
|
|
203
|
+
.then(function(msg) {
|
|
204
|
+
console.log('insert by returnList then', msg)
|
|
205
|
+
})
|
|
206
|
+
.catch(function(msg) {
|
|
207
|
+
console.log('insert by returnList catch', `${msg}`)
|
|
208
|
+
})
|
|
209
|
+
|
|
188
210
|
//save
|
|
189
211
|
await wo.save(rsm, { autoInsert: false })
|
|
190
212
|
.then(function(msg) {
|
|
@@ -345,6 +367,8 @@ test()
|
|
|
345
367
|
// insertBulk then { n: 2, nInserted: 2, ok: 1 }
|
|
346
368
|
// error insertBulk duplicate key value violates unique constraint "users_pkey"
|
|
347
369
|
// insertBulk by pk existed catch error: duplicate key value violates unique constraint "users_pkey"
|
|
370
|
+
// change insert
|
|
371
|
+
// insert by returnList then [ { n: 1, nInserted: 0, ok: 1 }, { n: 1, nInserted: 1, ok: 1 } ]
|
|
348
372
|
// change save
|
|
349
373
|
// save then [
|
|
350
374
|
// { n: 1, nInserted: 0, nModified: 0, ok: 1 },
|
|
@@ -366,6 +390,7 @@ test()
|
|
|
366
390
|
// { time: 2025-01-01T00:09:00.000Z, name: 'peter', value: 127 },
|
|
367
391
|
// { time: 2025-01-01T00:20:00.000Z, name: 'bulk-1', value: 201 },
|
|
368
392
|
// { time: 2025-01-01T00:21:00.000Z, name: 'bulk-2', value: 202 },
|
|
393
|
+
// { time: 2025-01-01T00:22:00.000Z, name: 'ret-new', value: 203 },
|
|
369
394
|
// {
|
|
370
395
|
// time: 2025-01-01T00:10:00.000Z,
|
|
371
396
|
// name: 'rosemary(modify)',
|
|
@@ -403,6 +428,7 @@ test()
|
|
|
403
428
|
// { time: 2025-01-01T00:08:00.000Z, name: 'kettle', value: 524 },
|
|
404
429
|
// { time: 2025-01-01T00:20:00.000Z, name: 'bulk-1', value: 201 },
|
|
405
430
|
// { time: 2025-01-01T00:21:00.000Z, name: 'bulk-2', value: 202 },
|
|
431
|
+
// { time: 2025-01-01T00:22:00.000Z, name: 'ret-new', value: 203 },
|
|
406
432
|
// {
|
|
407
433
|
// time: 2025-01-01T00:11:00.000Z,
|
|
408
434
|
// name: 'kettle(modify)',
|
|
@@ -447,6 +473,7 @@ test()
|
|
|
447
473
|
// { time: 2025-01-01T00:07:00.000Z, name: 'rosemary', value: 124.76 },
|
|
448
474
|
// { time: 2025-01-01T00:20:00.000Z, name: 'bulk-1', value: 201 },
|
|
449
475
|
// { time: 2025-01-01T00:21:00.000Z, name: 'bulk-2', value: 202 },
|
|
476
|
+
// { time: 2025-01-01T00:22:00.000Z, name: 'ret-new', value: 203 },
|
|
450
477
|
// {
|
|
451
478
|
// time: 2025-01-01T00:10:00.000Z,
|
|
452
479
|
// name: 'rosemary(modify)',
|