w-orm-postgresql 1.0.2 → 1.0.3

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.
@@ -66,6 +66,7 @@ import isnum from 'wsemi/src/isnum.mjs'
66
66
  import isint from 'wsemi/src/isint.mjs'
67
67
  import isbol from 'wsemi/src/isbol.mjs'
68
68
  import isDate from 'wsemi/src/isDate.mjs'
69
+ import haskey from 'wsemi/src/haskey.mjs'
69
70
  import pmSeries from 'wsemi/src/pmSeries.mjs'
70
71
 
71
72
 
@@ -77,15 +78,16 @@ import pmSeries from 'wsemi/src/pmSeries.mjs'
77
78
  * @param {String} [opt.url='postgresql://127.0.0.1:5432'] 輸入連接資料庫字串,預設'postgresql://127.0.0.1:5432'
78
79
  * @param {String} [opt.db='worm'] 輸入使用資料庫名稱字串,預設'worm'
79
80
  * @param {String} [opt.cl='test'] 輸入使用資料表名稱字串,預設'test'
81
+ * @param {Boolean} [opt.useCache=false] 輸入是否使用select快取,適用於單程序操作,預設false
80
82
  * @returns {Object} 回傳操作資料庫物件,各事件功能詳見說明
81
83
  */
82
- function WOrmPostgresql(opt = {}) {
83
-
84
- //_cache
85
- let _cache = null
86
-
87
- //url
88
- let url = get(opt, 'url')
84
+ function WOrmPostgresql(opt = {}) {
85
+
86
+ //_cache
87
+ let _cache = null
88
+
89
+ //url
90
+ let url = get(opt, 'url')
89
91
  if (!isestr(url)) {
90
92
  url = 'postgresql://user:password@127.0.0.1:5432'
91
93
  }
@@ -102,6 +104,12 @@ function WOrmPostgresql(opt = {}) {
102
104
  cl = 'test'
103
105
  }
104
106
 
107
+ //useCache
108
+ let useCache = get(opt, 'useCache')
109
+ if (!isbol(useCache)) {
110
+ useCache = false
111
+ }
112
+
105
113
  //getValueType
106
114
  function getValueType(value) {
107
115
  if (isstr(value)) {
@@ -190,43 +198,43 @@ function WOrmPostgresql(opt = {}) {
190
198
  //ee
191
199
  let ee = new events.EventEmitter()
192
200
 
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
-
201
+ //PgClient
202
+ let PgClient = pg.Client
203
+ // console.log('PgClient', PgClient)
204
+
205
+ //clearCache
206
+ function clearCache() {
207
+ _cache = null
208
+ }
209
+
210
+ //getCacheKey
211
+ function getCacheKey(find = {}, order = {}) {
212
+ return JSON.stringify({
213
+ find,
214
+ order,
215
+ })
216
+ }
217
+
218
+ //getCache
219
+ function getCache(find = {}, order = {}) {
220
+ if (iseobj(_cache)) {
221
+ let key = getCacheKey(find, order)
222
+ if (haskey(_cache, key)) {
223
+ return cloneDeep(_cache[key]) //與外部使用數據脫勾
224
+ }
225
+ }
226
+ return null
227
+ }
228
+
229
+ //setCache
230
+ function setCache(find = {}, order = {}, data = []) {
231
+ if (!iseobj(_cache)) {
232
+ _cache = {}
233
+ }
234
+ let key = getCacheKey(find, order)
235
+ _cache[key] = cloneDeep(data) //與外部使用數據脫勾
236
+ }
237
+
230
238
  /**
231
239
  * 創建資料表
232
240
  *
@@ -298,15 +306,17 @@ function WOrmPostgresql(opt = {}) {
298
306
  finally {
299
307
  await client.end()
300
308
  client = null
301
- }
302
- // console.log('res', res)
303
-
304
- //update
305
- clearCache()
306
-
307
- //check
308
- if (isErr) {
309
- return Promise.reject(res)
309
+ }
310
+ // console.log('res', res)
311
+
312
+ //update
313
+ if (useCache) {
314
+ clearCache()
315
+ }
316
+
317
+ //check
318
+ if (isErr) {
319
+ return Promise.reject(res)
310
320
  }
311
321
 
312
322
  return res
@@ -320,18 +330,20 @@ function WOrmPostgresql(opt = {}) {
320
330
  * @param {Object} [order={}] 輸入排序條件物件
321
331
  * @returns {Promise} 回傳Promise,resolve回傳數據,reject回傳錯誤訊息
322
332
  */
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 })
333
+ async function select(find = {}, order = {}) {
334
+ let isErr = false
335
+ let res = null
336
+
337
+ //cache
338
+ if (useCache) {
339
+ let cache = getCache(find, order)
340
+ if (isarr(cache)) {
341
+ return cache
342
+ }
343
+ }
344
+
345
+ //client
346
+ let client = new PgClient({ connectionString })
335
347
 
336
348
  //connect
337
349
  try {
@@ -373,17 +385,17 @@ function WOrmPostgresql(opt = {}) {
373
385
  res = get(r, 'rows')
374
386
 
375
387
  //check
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
- }
388
+ if (!isarr(res)) {
389
+ isErr = true
390
+ res = `can not select by find[${JSON.stringify(find)}]`
391
+ }
392
+
393
+ //cache
394
+ if (useCache && !isErr) {
395
+ setCache(find, order, res)
396
+ }
397
+
398
+ }
387
399
  catch (err) {
388
400
  isErr = true
389
401
  res = err
@@ -480,17 +492,17 @@ function WOrmPostgresql(opt = {}) {
480
492
  .then(() => {
481
493
 
482
494
  //res
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
- })
495
+ res = {
496
+ n: nAll,
497
+ nInserted: nInsert,
498
+ ok: 1,
499
+ }
500
+
501
+ })
502
+ .catch((err) => {
503
+ isErr = true
504
+ res = err.message
505
+ })
494
506
 
495
507
  }
496
508
  catch (err) {
@@ -498,26 +510,28 @@ function WOrmPostgresql(opt = {}) {
498
510
  res = err
499
511
  }
500
512
  finally {
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)
513
+ await client.end()
514
+ client = null
515
+ }
516
+
517
+ //update
518
+ if (useCache) {
519
+ clearCache()
520
+ }
521
+
522
+ //emit
523
+ if (!isErr) {
524
+ try {
525
+ ee.emit('change', 'insert', data, res)
526
+ }
527
+ catch (err) {
528
+ console.log(err)
529
+ }
530
+ }
531
+
532
+ //check
533
+ if (isErr) {
534
+ return Promise.reject(res)
521
535
  }
522
536
 
523
537
  return res
@@ -745,35 +759,37 @@ function WOrmPostgresql(opt = {}) {
745
759
 
746
760
  }
747
761
 
748
- return rest
749
- })
750
-
751
- }
752
- catch (err) {
753
- isErr = true
754
- res = err
755
- }
762
+ return rest
763
+ })
764
+
765
+ }
766
+ catch (err) {
767
+ isErr = true
768
+ res = err
769
+ }
756
770
  finally {
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)
771
+ await client.end()
772
+ client = null
773
+ }
774
+
775
+ //update
776
+ if (useCache) {
777
+ clearCache()
778
+ }
779
+
780
+ //emit
781
+ if (!isErr) {
782
+ try {
783
+ ee.emit('change', 'save', data, res)
784
+ }
785
+ catch (err) {
786
+ console.log(err)
787
+ }
788
+ }
789
+
790
+ //check
791
+ if (isErr) {
792
+ return Promise.reject(res)
777
793
  }
778
794
 
779
795
  return res
@@ -879,36 +895,38 @@ function WOrmPostgresql(opt = {}) {
879
895
  }
880
896
 
881
897
  })
882
-
883
- return rest
884
- })
885
-
886
- }
887
- catch (err) {
888
- isErr = true
889
- res = err
890
- }
898
+
899
+ return rest
900
+ })
901
+
902
+ }
903
+ catch (err) {
904
+ isErr = true
905
+ res = err
906
+ }
891
907
  finally {
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)
908
+ await client.end()
909
+ client = null
910
+ }
911
+
912
+ //update
913
+ if (useCache) {
914
+ clearCache()
915
+ }
916
+
917
+ //emit
918
+ if (!isErr) {
919
+ try {
920
+ ee.emit('change', 'del', data, res)
921
+ }
922
+ catch (err) {
923
+ console.log(err)
924
+ }
925
+ }
926
+
927
+ //check
928
+ if (isErr) {
929
+ return Promise.reject(res)
912
930
  }
913
931
 
914
932
  return res
@@ -977,17 +995,17 @@ function WOrmPostgresql(opt = {}) {
977
995
  .then((r) => {
978
996
 
979
997
  //res
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
- })
998
+ res = {
999
+ n: r.rowCount,
1000
+ nDeleted: r.rowCount,
1001
+ ok: 1,
1002
+ }
1003
+
1004
+ })
1005
+ .catch((err) => {
1006
+ isErr = true
1007
+ res = err.message
1008
+ })
991
1009
 
992
1010
  }
993
1011
  catch (err) {
@@ -995,26 +1013,28 @@ function WOrmPostgresql(opt = {}) {
995
1013
  res = err
996
1014
  }
997
1015
  finally {
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)
1016
+ await client.end()
1017
+ client = null
1018
+ }
1019
+
1020
+ //update
1021
+ if (useCache) {
1022
+ clearCache()
1023
+ }
1024
+
1025
+ //emit
1026
+ if (!isErr) {
1027
+ try {
1028
+ ee.emit('change', 'delAll', null, res)
1029
+ }
1030
+ catch (err) {
1031
+ console.log(err)
1032
+ }
1033
+ }
1034
+
1035
+ //check
1036
+ if (isErr) {
1037
+ return Promise.reject(res)
1018
1038
  }
1019
1039
 
1020
1040
  return res
@@ -1047,7 +1067,7 @@ export default WOrmPostgresql
1047
1067
  <br class="clear">
1048
1068
 
1049
1069
  <footer>
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.
1070
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Fri May 15 2026 13:15:32 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
1051
1071
  </footer>
1052
1072
 
1053
1073
  <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 Thu May 14 2026 20:49:12 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 Fri May 15 2026 13:15:32 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
75
75
  </footer>
76
76
 
77
77
  <script>prettyPrint();</script>