w-orm-postgresql 1.0.1 → 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.
@@ -79,10 +79,13 @@ 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
- //url
85
- let url = get(opt, 'url')
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
90
  url = 'postgresql://user:password@127.0.0.1:5432'
88
91
  }
@@ -187,10 +190,43 @@ function WOrmPostgresql(opt = {}) {
187
190
  //ee
188
191
  let ee = new events.EventEmitter()
189
192
 
190
- //PgClient
191
- let PgClient = pg.Client
192
- // console.log('PgClient', PgClient)
193
-
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
+
194
230
  /**
195
231
  * 創建資料表
196
232
  *
@@ -262,12 +298,15 @@ function WOrmPostgresql(opt = {}) {
262
298
  finally {
263
299
  await client.end()
264
300
  client = null
265
- }
266
- // console.log('res', res)
267
-
268
- //check
269
- if (isErr) {
270
- return Promise.reject(res)
301
+ }
302
+ // console.log('res', res)
303
+
304
+ //update
305
+ clearCache()
306
+
307
+ //check
308
+ if (isErr) {
309
+ return Promise.reject(res)
271
310
  }
272
311
 
273
312
  return res
@@ -281,12 +320,18 @@ function WOrmPostgresql(opt = {}) {
281
320
  * @param {Object} [order={}] 輸入排序條件物件
282
321
  * @returns {Promise} 回傳Promise,resolve回傳數據,reject回傳錯誤訊息
283
322
  */
284
- async function select(find = {}, order = {}) {
285
- let isErr = false
286
- let res = null
287
-
288
- //client
289
- let client = new PgClient({ connectionString })
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 })
290
335
 
291
336
  //connect
292
337
  try {
@@ -328,12 +373,17 @@ function WOrmPostgresql(opt = {}) {
328
373
  res = get(r, 'rows')
329
374
 
330
375
  //check
331
- if (!isarr(res)) {
332
- isErr = true
333
- res = `can not select by find[${JSON.stringify(find)}]`
334
- }
335
-
336
- }
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
+ }
337
387
  catch (err) {
338
388
  isErr = true
339
389
  res = err
@@ -430,20 +480,17 @@ function WOrmPostgresql(opt = {}) {
430
480
  .then(() => {
431
481
 
432
482
  //res
433
- res = {
434
- n: nAll,
435
- nInserted: nInsert,
436
- ok: 1,
437
- }
438
-
439
- //emit
440
- ee.emit('change', 'insert', data, res)
441
-
442
- })
443
- .catch((err) => {
444
- isErr = true
445
- res = err.message
446
- })
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
+ })
447
494
 
448
495
  }
449
496
  catch (err) {
@@ -451,13 +498,26 @@ function WOrmPostgresql(opt = {}) {
451
498
  res = err
452
499
  }
453
500
  finally {
454
- await client.end()
455
- client = null
456
- }
457
-
458
- //check
459
- if (isErr) {
460
- return Promise.reject(res)
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)
461
521
  }
462
522
 
463
523
  return res
@@ -685,25 +745,35 @@ function WOrmPostgresql(opt = {}) {
685
745
 
686
746
  }
687
747
 
688
- return rest
689
- })
690
-
691
- //emit
692
- ee.emit('change', 'save', data, res)
693
-
694
- }
695
- catch (err) {
696
- isErr = true
697
- res = err
698
- }
748
+ return rest
749
+ })
750
+
751
+ }
752
+ catch (err) {
753
+ isErr = true
754
+ res = err
755
+ }
699
756
  finally {
700
- await client.end()
701
- client = null
702
- }
703
-
704
- //check
705
- if (isErr) {
706
- return Promise.reject(res)
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)
707
777
  }
708
778
 
709
779
  return res
@@ -809,26 +879,36 @@ function WOrmPostgresql(opt = {}) {
809
879
  }
810
880
 
811
881
  })
812
-
813
- return rest
814
- })
815
-
816
- //emit
817
- ee.emit('change', 'del', data, res)
818
-
819
- }
820
- catch (err) {
821
- isErr = true
822
- res = err
823
- }
882
+
883
+ return rest
884
+ })
885
+
886
+ }
887
+ catch (err) {
888
+ isErr = true
889
+ res = err
890
+ }
824
891
  finally {
825
- await client.end()
826
- client = null
827
- }
828
-
829
- //check
830
- if (isErr) {
831
- return Promise.reject(res)
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)
832
912
  }
833
913
 
834
914
  return res
@@ -897,20 +977,17 @@ function WOrmPostgresql(opt = {}) {
897
977
  .then((r) => {
898
978
 
899
979
  //res
900
- res = {
901
- n: r.rowCount,
902
- nDeleted: r.rowCount,
903
- ok: 1,
904
- }
905
-
906
- //emit
907
- ee.emit('change', 'delAll', null, res)
908
-
909
- })
910
- .catch((err) => {
911
- isErr = true
912
- res = err.message
913
- })
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
+ })
914
991
 
915
992
  }
916
993
  catch (err) {
@@ -918,13 +995,26 @@ function WOrmPostgresql(opt = {}) {
918
995
  res = err
919
996
  }
920
997
  finally {
921
- await client.end()
922
- client = null
923
- }
924
-
925
- //check
926
- if (isErr) {
927
- return Promise.reject(res)
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)
928
1018
  }
929
1019
 
930
1020
  return res
@@ -957,7 +1047,7 @@ export default WOrmPostgresql
957
1047
  <br class="clear">
958
1048
 
959
1049
  <footer>
960
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.4</a> on Sun May 11 2025 15:23:24 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
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.
961
1051
  </footer>
962
1052
 
963
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.4</a> on Sun May 11 2025 15:23:24 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 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>
package/g-basic.mjs CHANGED
@@ -76,7 +76,7 @@ let rs = [
76
76
  },
77
77
  ]
78
78
 
79
- let rsm = [
79
+ let rsm = [
80
80
  { //相同
81
81
  time: '2025-01-01T00:09:00Z',
82
82
  name: 'peter',
@@ -100,10 +100,17 @@ let rsm = [
100
100
  { //無time須新增且新增數據無value
101
101
  time: '2025-01-01T00:13:00Z',
102
102
  name: 'sandler',
103
- },
104
- ]
105
-
106
- async function test() {
103
+ },
104
+ ]
105
+
106
+ let rsa = [
107
+ {
108
+ time: '2025-01-01T00:13:00Z',
109
+ name: 'sandler',
110
+ },
111
+ ]
112
+
113
+ async function test() {
107
114
 
108
115
  //wo
109
116
  let wo = WOrm(opt)
@@ -144,18 +151,18 @@ async function test() {
144
151
  console.log('insert catch', msg)
145
152
  })
146
153
 
147
- //save
148
- await wo.save(rsm, { autoInsert: true })
149
- .then(function(msg) {
150
- console.log('save then', msg)
151
- })
152
- .catch(function(msg) {
153
- console.log('save catch', msg)
154
- })
155
-
156
- //select all
157
- let ss = await wo.select()
158
- console.log('select all', ss)
154
+ //save
155
+ await wo.save(rsm, { autoInsert: false })
156
+ .then(function(msg) {
157
+ console.log('save then', msg)
158
+ })
159
+ .catch(function(msg) {
160
+ console.log('save catch', msg)
161
+ })
162
+
163
+ //select all
164
+ let ss = await wo.select()
165
+ console.log('select all', ss)
159
166
 
160
167
  //select
161
168
  let so = await wo.select({ name: 'rosemary' })
@@ -173,13 +180,22 @@ async function test() {
173
180
  let spc = await wo.select({ '$or': [{ '$and': [{ value: { '$ne': 123 } }, { value: { '$in': [123, 321, 123.456, 456] } }, { value: { '$nin': [456, 654] } }] }, { '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 400 } }] }] })
174
181
  console.log('select by $or, $and, $ne, $in, $nin', spc)
175
182
 
176
- // //select by regex
177
- // let sr = await wo.select({ name: { $regex: 'PeT', $options: '$i' } })
178
- // console.log('selectReg', sr)
179
-
180
- //del
181
- let d = {
182
- time: '2024-01-01T00:00:00Z',
183
+ // //select by regex
184
+ // let sr = await wo.select({ name: { $regex: 'PeT', $options: '$i' } })
185
+ // console.log('selectReg', sr)
186
+
187
+ //save
188
+ await wo.save(rsa, { autoInsert: true })
189
+ .then(function(msg) {
190
+ console.log('save then', msg)
191
+ })
192
+ .catch(function(msg) {
193
+ console.log('save catch', msg)
194
+ })
195
+
196
+ //del
197
+ let d = {
198
+ time: '2024-01-01T00:00:00Z',
183
199
  }
184
200
  await wo.del(d)
185
201
  .then(function(msg) {
@@ -187,12 +203,13 @@ async function test() {
187
203
  })
188
204
  .catch(function(msg) {
189
205
  console.log('del catch', msg)
190
- })
191
-
192
- //del
193
- let ds = ss.filter(function(v) {
194
- return v.name.indexOf('peter') >= 0 || v.name.indexOf('kettle') >= 0 || v.name.indexOf('sandler') >= 0
195
- })
206
+ })
207
+
208
+ //del
209
+ let ssDel = await wo.select()
210
+ let ds = ssDel.filter(function(v) {
211
+ return v.name.indexOf('peter') >= 0 || v.name.indexOf('kettle') >= 0 || v.name.indexOf('sandler') >= 0
212
+ })
196
213
  await wo.del(ds)
197
214
  .then(function(msg) {
198
215
  console.log('del then', msg)
@@ -213,13 +230,13 @@ test()
213
230
  // insert then { n: 13, nInserted: 13, ok: 1 }
214
231
  // change save
215
232
  // save then [
216
- // { n: 1, nModified: 0, ok: 1 },
217
- // { n: 1, nModified: 1, ok: 1 },
218
- // { n: 1, nModified: 1, ok: 1 },
219
- // { n: 1, nModified: 1, ok: 1 },
220
- // { n: 1, nInserted: 1, ok: 1 }
221
- // ]
222
- // select all [
233
+ // { n: 1, nModified: 0, ok: 1 },
234
+ // { n: 1, nModified: 1, ok: 1 },
235
+ // { n: 1, nModified: 1, ok: 1 },
236
+ // { n: 1, nModified: 1, ok: 1 },
237
+ // { n: 0, nModified: 0, ok: 1 }
238
+ // ]
239
+ // select all [
223
240
  // { time: 2025-01-01T00:00:00.000Z, name: 'peter', value: 123 },
224
241
  // { time: 2025-01-01T00:01:00.000Z, name: 'rosemary', value: 123.456 },
225
242
  // { time: 2025-01-01T00:02:00.000Z, name: 'kettle', value: 456 },
@@ -238,11 +255,10 @@ test()
238
255
  // {
239
256
  // time: 2025-01-01T00:11:00.000Z,
240
257
  // name: 'kettle(modify)',
241
- // value: 447
242
- // },
243
- // { time: 2025-01-01T00:12:00.000Z, name: 'peter', value: 99 },
244
- // { time: 2025-01-01T00:13:00.000Z, name: 'sandler', value: null }
245
- // ]
258
+ // value: 447
259
+ // },
260
+ // { time: 2025-01-01T00:12:00.000Z, name: 'peter', value: 99 }
261
+ // ]
246
262
  // select by name [
247
263
  // { time: 2025-01-01T00:01:00.000Z, name: 'rosemary', value: 123.456 },
248
264
  // { time: 2025-01-01T00:04:00.000Z, name: 'rosemary', value: 123.1236 },
@@ -274,11 +290,13 @@ test()
274
290
  // {
275
291
  // time: 2025-01-01T00:11:00.000Z,
276
292
  // name: 'kettle(modify)',
277
- // value: 447
278
- // }
279
- // ]
280
- // change del
281
- // del then [ { n: 1, nDeleted: 0, ok: 1 } ]
293
+ // value: 447
294
+ // }
295
+ // ]
296
+ // change save
297
+ // save then [ { n: 1, nInserted: 1, ok: 1 } ]
298
+ // change del
299
+ // del then [ { n: 1, nDeleted: 0, ok: 1 } ]
282
300
  // change del
283
301
  // del then [
284
302
  // { n: 1, nDeleted: 1, ok: 1 },
package/package.json CHANGED
@@ -1,15 +1,15 @@
1
1
  {
2
2
  "name": "w-orm-postgresql",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "main": "dist/w-orm-postgresql.umd.js",
5
5
  "dependencies": {
6
6
  "mongo-sql": "^6.2.0",
7
- "pg": "^8.15.6",
8
- "lodash-es": "^4.17.21",
9
- "wsemi": "^1.7.90"
7
+ "pg": "^8.20.0",
8
+ "lodash-es": "^4.18.1",
9
+ "wsemi": "^1.8.52"
10
10
  },
11
11
  "devDependencies": {
12
- "w-package-tools": "^1.0.90"
12
+ "w-package-tools": "^1.1.5"
13
13
  },
14
14
  "scripts": {
15
15
  "test": "mocha --parallel --timeout 60000",
package/script.txt CHANGED
@@ -15,4 +15,3 @@ npm run deploy
15
15
  #npm test
16
16
 
17
17
  #npm publish
18
-