w-orm-postgresql 1.0.0 → 1.0.2
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 +83 -62
- package/babel.config.js +4 -3
- package/dist/w-orm-postgresql.umd.js +2 -2
- package/dist/w-orm-postgresql.umd.js.map +1 -1
- package/docs/WOrmPostgresql.html +7 -7
- package/docs/WOrmPostgresql.mjs.html +256 -148
- package/docs/index.html +1 -1
- package/g-basic.mjs +82 -64
- package/package.json +6 -6
- package/script.txt +4 -5
- package/src/WOrmPostgresql.mjs +255 -147
- package/test/basic.test.mjs +161 -120
|
@@ -79,12 +79,14 @@ import pmSeries from 'wsemi/src/pmSeries.mjs'
|
|
|
79
79
|
* @param {String} [opt.cl='test'] 輸入使用資料表名稱字串,預設'test'
|
|
80
80
|
* @returns {Object} 回傳操作資料庫物件,各事件功能詳見說明
|
|
81
81
|
*/
|
|
82
|
-
function WOrmPostgresql(opt = {}) {
|
|
83
|
-
|
|
84
|
-
//
|
|
85
|
-
let
|
|
82
|
+
function WOrmPostgresql(opt = {}) {
|
|
83
|
+
|
|
84
|
+
//_cache
|
|
85
|
+
let _cache = null
|
|
86
|
+
|
|
87
|
+
//url
|
|
88
|
+
let url = get(opt, 'url')
|
|
86
89
|
if (!isestr(url)) {
|
|
87
|
-
// url = `postgresql://${user}:${password}@${ip}:${port}`
|
|
88
90
|
url = 'postgresql://user:password@127.0.0.1:5432'
|
|
89
91
|
}
|
|
90
92
|
|
|
@@ -188,19 +190,52 @@ function WOrmPostgresql(opt = {}) {
|
|
|
188
190
|
//ee
|
|
189
191
|
let ee = new events.EventEmitter()
|
|
190
192
|
|
|
191
|
-
//PgClient
|
|
192
|
-
let PgClient = pg.Client
|
|
193
|
-
// console.log('PgClient', PgClient)
|
|
194
|
-
|
|
193
|
+
//PgClient
|
|
194
|
+
let PgClient = pg.Client
|
|
195
|
+
// console.log('PgClient', PgClient)
|
|
196
|
+
|
|
197
|
+
//clearCache
|
|
198
|
+
function clearCache() {
|
|
199
|
+
_cache = null
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
//getCacheKey
|
|
203
|
+
function getCacheKey(find = {}, order = {}) {
|
|
204
|
+
return JSON.stringify({
|
|
205
|
+
find,
|
|
206
|
+
order,
|
|
207
|
+
})
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
//getCache
|
|
211
|
+
function getCache(find = {}, order = {}) {
|
|
212
|
+
if (iseobj(_cache)) {
|
|
213
|
+
let key = getCacheKey(find, order)
|
|
214
|
+
if (Object.prototype.hasOwnProperty.call(_cache, key)) {
|
|
215
|
+
return cloneDeep(_cache[key]) //與外部使用數據脫勾
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return null
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
//setCache
|
|
222
|
+
function setCache(find = {}, order = {}, data = []) {
|
|
223
|
+
if (!iseobj(_cache)) {
|
|
224
|
+
_cache = {}
|
|
225
|
+
}
|
|
226
|
+
let key = getCacheKey(find, order)
|
|
227
|
+
_cache[key] = cloneDeep(data) //與外部使用數據脫勾
|
|
228
|
+
}
|
|
229
|
+
|
|
195
230
|
/**
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
231
|
+
* 創建資料表
|
|
232
|
+
*
|
|
233
|
+
* @memberOf WOrmPostgresql
|
|
234
|
+
* @param {String} cl 輸入資料表名字串
|
|
235
|
+
* @param {String} pk 輸入主鍵字串
|
|
236
|
+
* @param {Array|Object} arr 輸入數據物件陣列或數據物件
|
|
237
|
+
* @returns {Promise} 回傳Promise,resolve回傳成功訊息,reject回傳錯誤訊息
|
|
238
|
+
*/
|
|
204
239
|
async function createTable(cl, pk, arr) {
|
|
205
240
|
let isErr = false
|
|
206
241
|
let res = null
|
|
@@ -263,31 +298,40 @@ function WOrmPostgresql(opt = {}) {
|
|
|
263
298
|
finally {
|
|
264
299
|
await client.end()
|
|
265
300
|
client = null
|
|
266
|
-
}
|
|
267
|
-
// console.log('res', res)
|
|
268
|
-
|
|
269
|
-
//
|
|
270
|
-
|
|
271
|
-
|
|
301
|
+
}
|
|
302
|
+
// console.log('res', res)
|
|
303
|
+
|
|
304
|
+
//update
|
|
305
|
+
clearCache()
|
|
306
|
+
|
|
307
|
+
//check
|
|
308
|
+
if (isErr) {
|
|
309
|
+
return Promise.reject(res)
|
|
272
310
|
}
|
|
273
311
|
|
|
274
312
|
return res
|
|
275
313
|
}
|
|
276
314
|
|
|
277
315
|
/**
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
async function select(find = {}, order = {}) {
|
|
286
|
-
let isErr = false
|
|
287
|
-
let res = null
|
|
288
|
-
|
|
289
|
-
//
|
|
290
|
-
let
|
|
316
|
+
* 查詢數據
|
|
317
|
+
*
|
|
318
|
+
* @memberOf WOrmPostgresql
|
|
319
|
+
* @param {Object} [find={}] 輸入查詢條件物件
|
|
320
|
+
* @param {Object} [order={}] 輸入排序條件物件
|
|
321
|
+
* @returns {Promise} 回傳Promise,resolve回傳數據,reject回傳錯誤訊息
|
|
322
|
+
*/
|
|
323
|
+
async function select(find = {}, order = {}) {
|
|
324
|
+
let isErr = false
|
|
325
|
+
let res = null
|
|
326
|
+
|
|
327
|
+
//cache
|
|
328
|
+
let cache = getCache(find, order)
|
|
329
|
+
if (isarr(cache)) {
|
|
330
|
+
return cache
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
//client
|
|
334
|
+
let client = new PgClient({ connectionString })
|
|
291
335
|
|
|
292
336
|
//connect
|
|
293
337
|
try {
|
|
@@ -329,12 +373,17 @@ function WOrmPostgresql(opt = {}) {
|
|
|
329
373
|
res = get(r, 'rows')
|
|
330
374
|
|
|
331
375
|
//check
|
|
332
|
-
if (!isarr(res)) {
|
|
333
|
-
isErr = true
|
|
334
|
-
res = `can not select by find[${JSON.stringify(find)}]`
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
|
|
376
|
+
if (!isarr(res)) {
|
|
377
|
+
isErr = true
|
|
378
|
+
res = `can not select by find[${JSON.stringify(find)}]`
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
//cache
|
|
382
|
+
if (!isErr) {
|
|
383
|
+
setCache(find, order, res)
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
}
|
|
338
387
|
catch (err) {
|
|
339
388
|
isErr = true
|
|
340
389
|
res = err
|
|
@@ -354,16 +403,25 @@ function WOrmPostgresql(opt = {}) {
|
|
|
354
403
|
}
|
|
355
404
|
|
|
356
405
|
/**
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
406
|
+
* 插入數據
|
|
407
|
+
*
|
|
408
|
+
* @memberOf WOrmPostgresql
|
|
409
|
+
* @param {Object|Array} data 輸入數據物件或陣列
|
|
410
|
+
* @returns {Promise} 回傳Promise,resolve回傳插入結果,reject回傳錯誤訊息
|
|
411
|
+
*/
|
|
363
412
|
async function insert(data) {
|
|
364
413
|
let isErr = false
|
|
365
414
|
let res = null
|
|
366
415
|
|
|
416
|
+
//check
|
|
417
|
+
if (!iseobj(data) && !isearr(data)) {
|
|
418
|
+
return {
|
|
419
|
+
n: 0,
|
|
420
|
+
nInserted: 0,
|
|
421
|
+
ok: 1,
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
367
425
|
//cloneDeep
|
|
368
426
|
data = cloneDeep(data)
|
|
369
427
|
|
|
@@ -422,20 +480,17 @@ function WOrmPostgresql(opt = {}) {
|
|
|
422
480
|
.then(() => {
|
|
423
481
|
|
|
424
482
|
//res
|
|
425
|
-
res = {
|
|
426
|
-
n: nAll,
|
|
427
|
-
nInserted: nInsert,
|
|
428
|
-
ok: 1,
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
isErr = true
|
|
437
|
-
res = err.message
|
|
438
|
-
})
|
|
483
|
+
res = {
|
|
484
|
+
n: nAll,
|
|
485
|
+
nInserted: nInsert,
|
|
486
|
+
ok: 1,
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
})
|
|
490
|
+
.catch((err) => {
|
|
491
|
+
isErr = true
|
|
492
|
+
res = err.message
|
|
493
|
+
})
|
|
439
494
|
|
|
440
495
|
}
|
|
441
496
|
catch (err) {
|
|
@@ -443,31 +498,49 @@ function WOrmPostgresql(opt = {}) {
|
|
|
443
498
|
res = err
|
|
444
499
|
}
|
|
445
500
|
finally {
|
|
446
|
-
await client.end()
|
|
447
|
-
client = null
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
//
|
|
451
|
-
|
|
452
|
-
|
|
501
|
+
await client.end()
|
|
502
|
+
client = null
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
//update
|
|
506
|
+
clearCache()
|
|
507
|
+
|
|
508
|
+
//emit
|
|
509
|
+
if (!isErr) {
|
|
510
|
+
try {
|
|
511
|
+
ee.emit('change', 'insert', data, res)
|
|
512
|
+
}
|
|
513
|
+
catch (err) {
|
|
514
|
+
console.log(err)
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
//check
|
|
519
|
+
if (isErr) {
|
|
520
|
+
return Promise.reject(res)
|
|
453
521
|
}
|
|
454
522
|
|
|
455
523
|
return res
|
|
456
524
|
}
|
|
457
525
|
|
|
458
526
|
/**
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
527
|
+
* 儲存數據
|
|
528
|
+
*
|
|
529
|
+
* @memberOf WOrmPostgresql
|
|
530
|
+
* @param {Object|Array} data 輸入數據物件或陣列
|
|
531
|
+
* @param {Object} [option={}] 輸入設定物件,預設為{}
|
|
532
|
+
* @param {boolean} [option.autoInsert=true] 輸入是否於儲存時發現原本無數據,則自動改以插入處理,預設為true
|
|
533
|
+
* @returns {Promise} 回傳Promise,resolve回傳儲存結果,reject回傳錯誤訊息
|
|
534
|
+
*/
|
|
467
535
|
async function save(data, option = {}) {
|
|
468
536
|
let isErr = false
|
|
469
537
|
let res = null
|
|
470
538
|
|
|
539
|
+
//check
|
|
540
|
+
if (!iseobj(data) && !isearr(data)) {
|
|
541
|
+
return []
|
|
542
|
+
}
|
|
543
|
+
|
|
471
544
|
//cloneDeep
|
|
472
545
|
data = cloneDeep(data)
|
|
473
546
|
|
|
@@ -672,41 +745,56 @@ function WOrmPostgresql(opt = {}) {
|
|
|
672
745
|
|
|
673
746
|
}
|
|
674
747
|
|
|
675
|
-
return rest
|
|
676
|
-
})
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
isErr = true
|
|
684
|
-
res = err
|
|
685
|
-
}
|
|
748
|
+
return rest
|
|
749
|
+
})
|
|
750
|
+
|
|
751
|
+
}
|
|
752
|
+
catch (err) {
|
|
753
|
+
isErr = true
|
|
754
|
+
res = err
|
|
755
|
+
}
|
|
686
756
|
finally {
|
|
687
|
-
await client.end()
|
|
688
|
-
client = null
|
|
689
|
-
}
|
|
690
|
-
|
|
691
|
-
//
|
|
692
|
-
|
|
693
|
-
|
|
757
|
+
await client.end()
|
|
758
|
+
client = null
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
//update
|
|
762
|
+
clearCache()
|
|
763
|
+
|
|
764
|
+
//emit
|
|
765
|
+
if (!isErr) {
|
|
766
|
+
try {
|
|
767
|
+
ee.emit('change', 'save', data, res)
|
|
768
|
+
}
|
|
769
|
+
catch (err) {
|
|
770
|
+
console.log(err)
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
//check
|
|
775
|
+
if (isErr) {
|
|
776
|
+
return Promise.reject(res)
|
|
694
777
|
}
|
|
695
778
|
|
|
696
779
|
return res
|
|
697
780
|
}
|
|
698
781
|
|
|
699
782
|
/**
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
783
|
+
* 刪除數據
|
|
784
|
+
*
|
|
785
|
+
* @memberOf WOrmPostgresql
|
|
786
|
+
* @param {Object|Array} data 輸入數據物件或陣列
|
|
787
|
+
* @returns {Promise} 回傳Promise,resolve回傳刪除結果,reject回傳錯誤訊息
|
|
788
|
+
*/
|
|
706
789
|
async function del(data) {
|
|
707
790
|
let isErr = false
|
|
708
791
|
let res = null
|
|
709
792
|
|
|
793
|
+
//check
|
|
794
|
+
if (!iseobj(data) && !isearr(data)) {
|
|
795
|
+
return []
|
|
796
|
+
}
|
|
797
|
+
|
|
710
798
|
//cloneDeep
|
|
711
799
|
data = cloneDeep(data)
|
|
712
800
|
|
|
@@ -791,38 +879,48 @@ function WOrmPostgresql(opt = {}) {
|
|
|
791
879
|
}
|
|
792
880
|
|
|
793
881
|
})
|
|
794
|
-
|
|
795
|
-
return rest
|
|
796
|
-
})
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
isErr = true
|
|
804
|
-
res = err
|
|
805
|
-
}
|
|
882
|
+
|
|
883
|
+
return rest
|
|
884
|
+
})
|
|
885
|
+
|
|
886
|
+
}
|
|
887
|
+
catch (err) {
|
|
888
|
+
isErr = true
|
|
889
|
+
res = err
|
|
890
|
+
}
|
|
806
891
|
finally {
|
|
807
|
-
await client.end()
|
|
808
|
-
client = null
|
|
809
|
-
}
|
|
810
|
-
|
|
811
|
-
//
|
|
812
|
-
|
|
813
|
-
|
|
892
|
+
await client.end()
|
|
893
|
+
client = null
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
//update
|
|
897
|
+
clearCache()
|
|
898
|
+
|
|
899
|
+
//emit
|
|
900
|
+
if (!isErr) {
|
|
901
|
+
try {
|
|
902
|
+
ee.emit('change', 'del', data, res)
|
|
903
|
+
}
|
|
904
|
+
catch (err) {
|
|
905
|
+
console.log(err)
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
//check
|
|
910
|
+
if (isErr) {
|
|
911
|
+
return Promise.reject(res)
|
|
814
912
|
}
|
|
815
913
|
|
|
816
914
|
return res
|
|
817
915
|
}
|
|
818
916
|
|
|
819
917
|
/**
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
918
|
+
* 刪除全部數據,需與del分開,避免未傳數據導致直接刪除全表
|
|
919
|
+
*
|
|
920
|
+
* @memberOf WOrmPostgresql
|
|
921
|
+
* @param {Object} [find={}] 輸入刪除條件物件
|
|
922
|
+
* @returns {Promise} 回傳Promise,resolve回傳刪除結果,reject回傳錯誤訊息
|
|
923
|
+
*/
|
|
826
924
|
async function delAll(find = {}) {
|
|
827
925
|
let isErr = false
|
|
828
926
|
let res = null
|
|
@@ -879,20 +977,17 @@ function WOrmPostgresql(opt = {}) {
|
|
|
879
977
|
.then((r) => {
|
|
880
978
|
|
|
881
979
|
//res
|
|
882
|
-
res = {
|
|
883
|
-
n: r.rowCount,
|
|
884
|
-
nDeleted: r.rowCount,
|
|
885
|
-
ok: 1,
|
|
886
|
-
}
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
isErr = true
|
|
894
|
-
res = err.message
|
|
895
|
-
})
|
|
980
|
+
res = {
|
|
981
|
+
n: r.rowCount,
|
|
982
|
+
nDeleted: r.rowCount,
|
|
983
|
+
ok: 1,
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
})
|
|
987
|
+
.catch((err) => {
|
|
988
|
+
isErr = true
|
|
989
|
+
res = err.message
|
|
990
|
+
})
|
|
896
991
|
|
|
897
992
|
}
|
|
898
993
|
catch (err) {
|
|
@@ -900,13 +995,26 @@ function WOrmPostgresql(opt = {}) {
|
|
|
900
995
|
res = err
|
|
901
996
|
}
|
|
902
997
|
finally {
|
|
903
|
-
await client.end()
|
|
904
|
-
client = null
|
|
905
|
-
}
|
|
906
|
-
|
|
907
|
-
//
|
|
908
|
-
|
|
909
|
-
|
|
998
|
+
await client.end()
|
|
999
|
+
client = null
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
//update
|
|
1003
|
+
clearCache()
|
|
1004
|
+
|
|
1005
|
+
//emit
|
|
1006
|
+
if (!isErr) {
|
|
1007
|
+
try {
|
|
1008
|
+
ee.emit('change', 'delAll', null, res)
|
|
1009
|
+
}
|
|
1010
|
+
catch (err) {
|
|
1011
|
+
console.log(err)
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
//check
|
|
1016
|
+
if (isErr) {
|
|
1017
|
+
return Promise.reject(res)
|
|
910
1018
|
}
|
|
911
1019
|
|
|
912
1020
|
return res
|
|
@@ -939,7 +1047,7 @@ export default WOrmPostgresql
|
|
|
939
1047
|
<br class="clear">
|
|
940
1048
|
|
|
941
1049
|
<footer>
|
|
942
|
-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.
|
|
1050
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Thu May 14 2026 20:49:12 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
943
1051
|
</footer>
|
|
944
1052
|
|
|
945
1053
|
<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.
|
|
74
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Thu May 14 2026 20:49:12 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
75
75
|
</footer>
|
|
76
76
|
|
|
77
77
|
<script>prettyPrint();</script>
|