w-orm-postgresql 1.0.1 → 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.
- package/README.md +108 -5
- 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 +48 -8
- package/docs/WOrmPostgresql.mjs.html +123 -13
- package/docs/index.html +1 -1
- package/g-basic.mjs +106 -5
- package/package.json +5 -5
- package/script.txt +0 -1
- package/src/WOrmPostgresql.mjs +122 -12
- package/test/basic.test.mjs +681 -428
|
@@ -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,10 +78,14 @@ 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
84
|
function WOrmPostgresql(opt = {}) {
|
|
83
85
|
|
|
86
|
+
//_cache
|
|
87
|
+
let _cache = null
|
|
88
|
+
|
|
84
89
|
//url
|
|
85
90
|
let url = get(opt, 'url')
|
|
86
91
|
if (!isestr(url)) {
|
|
@@ -99,6 +104,12 @@ function WOrmPostgresql(opt = {}) {
|
|
|
99
104
|
cl = 'test'
|
|
100
105
|
}
|
|
101
106
|
|
|
107
|
+
//useCache
|
|
108
|
+
let useCache = get(opt, 'useCache')
|
|
109
|
+
if (!isbol(useCache)) {
|
|
110
|
+
useCache = false
|
|
111
|
+
}
|
|
112
|
+
|
|
102
113
|
//getValueType
|
|
103
114
|
function getValueType(value) {
|
|
104
115
|
if (isstr(value)) {
|
|
@@ -191,6 +202,39 @@ function WOrmPostgresql(opt = {}) {
|
|
|
191
202
|
let PgClient = pg.Client
|
|
192
203
|
// console.log('PgClient', PgClient)
|
|
193
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
|
+
|
|
194
238
|
/**
|
|
195
239
|
* 創建資料表
|
|
196
240
|
*
|
|
@@ -265,6 +309,11 @@ function WOrmPostgresql(opt = {}) {
|
|
|
265
309
|
}
|
|
266
310
|
// console.log('res', res)
|
|
267
311
|
|
|
312
|
+
//update
|
|
313
|
+
if (useCache) {
|
|
314
|
+
clearCache()
|
|
315
|
+
}
|
|
316
|
+
|
|
268
317
|
//check
|
|
269
318
|
if (isErr) {
|
|
270
319
|
return Promise.reject(res)
|
|
@@ -285,6 +334,14 @@ function WOrmPostgresql(opt = {}) {
|
|
|
285
334
|
let isErr = false
|
|
286
335
|
let res = null
|
|
287
336
|
|
|
337
|
+
//cache
|
|
338
|
+
if (useCache) {
|
|
339
|
+
let cache = getCache(find, order)
|
|
340
|
+
if (isarr(cache)) {
|
|
341
|
+
return cache
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
288
345
|
//client
|
|
289
346
|
let client = new PgClient({ connectionString })
|
|
290
347
|
|
|
@@ -333,6 +390,11 @@ function WOrmPostgresql(opt = {}) {
|
|
|
333
390
|
res = `can not select by find[${JSON.stringify(find)}]`
|
|
334
391
|
}
|
|
335
392
|
|
|
393
|
+
//cache
|
|
394
|
+
if (useCache && !isErr) {
|
|
395
|
+
setCache(find, order, res)
|
|
396
|
+
}
|
|
397
|
+
|
|
336
398
|
}
|
|
337
399
|
catch (err) {
|
|
338
400
|
isErr = true
|
|
@@ -436,9 +498,6 @@ function WOrmPostgresql(opt = {}) {
|
|
|
436
498
|
ok: 1,
|
|
437
499
|
}
|
|
438
500
|
|
|
439
|
-
//emit
|
|
440
|
-
ee.emit('change', 'insert', data, res)
|
|
441
|
-
|
|
442
501
|
})
|
|
443
502
|
.catch((err) => {
|
|
444
503
|
isErr = true
|
|
@@ -455,6 +514,21 @@ function WOrmPostgresql(opt = {}) {
|
|
|
455
514
|
client = null
|
|
456
515
|
}
|
|
457
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
|
+
|
|
458
532
|
//check
|
|
459
533
|
if (isErr) {
|
|
460
534
|
return Promise.reject(res)
|
|
@@ -688,9 +762,6 @@ function WOrmPostgresql(opt = {}) {
|
|
|
688
762
|
return rest
|
|
689
763
|
})
|
|
690
764
|
|
|
691
|
-
//emit
|
|
692
|
-
ee.emit('change', 'save', data, res)
|
|
693
|
-
|
|
694
765
|
}
|
|
695
766
|
catch (err) {
|
|
696
767
|
isErr = true
|
|
@@ -701,6 +772,21 @@ function WOrmPostgresql(opt = {}) {
|
|
|
701
772
|
client = null
|
|
702
773
|
}
|
|
703
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
|
+
|
|
704
790
|
//check
|
|
705
791
|
if (isErr) {
|
|
706
792
|
return Promise.reject(res)
|
|
@@ -813,9 +899,6 @@ function WOrmPostgresql(opt = {}) {
|
|
|
813
899
|
return rest
|
|
814
900
|
})
|
|
815
901
|
|
|
816
|
-
//emit
|
|
817
|
-
ee.emit('change', 'del', data, res)
|
|
818
|
-
|
|
819
902
|
}
|
|
820
903
|
catch (err) {
|
|
821
904
|
isErr = true
|
|
@@ -826,6 +909,21 @@ function WOrmPostgresql(opt = {}) {
|
|
|
826
909
|
client = null
|
|
827
910
|
}
|
|
828
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
|
+
|
|
829
927
|
//check
|
|
830
928
|
if (isErr) {
|
|
831
929
|
return Promise.reject(res)
|
|
@@ -903,9 +1001,6 @@ function WOrmPostgresql(opt = {}) {
|
|
|
903
1001
|
ok: 1,
|
|
904
1002
|
}
|
|
905
1003
|
|
|
906
|
-
//emit
|
|
907
|
-
ee.emit('change', 'delAll', null, res)
|
|
908
|
-
|
|
909
1004
|
})
|
|
910
1005
|
.catch((err) => {
|
|
911
1006
|
isErr = true
|
|
@@ -922,6 +1017,21 @@ function WOrmPostgresql(opt = {}) {
|
|
|
922
1017
|
client = null
|
|
923
1018
|
}
|
|
924
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
|
+
|
|
925
1035
|
//check
|
|
926
1036
|
if (isErr) {
|
|
927
1037
|
return Promise.reject(res)
|
|
@@ -957,7 +1067,7 @@ export default WOrmPostgresql
|
|
|
957
1067
|
<br class="clear">
|
|
958
1068
|
|
|
959
1069
|
<footer>
|
|
960
|
-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.
|
|
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.
|
|
961
1071
|
</footer>
|
|
962
1072
|
|
|
963
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.
|
|
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>
|
package/g-basic.mjs
CHANGED
|
@@ -103,6 +103,13 @@ let rsm = [
|
|
|
103
103
|
},
|
|
104
104
|
]
|
|
105
105
|
|
|
106
|
+
let rsa = [
|
|
107
|
+
{
|
|
108
|
+
time: '2025-01-01T00:13:00Z',
|
|
109
|
+
name: 'sandler',
|
|
110
|
+
},
|
|
111
|
+
]
|
|
112
|
+
|
|
106
113
|
async function test() {
|
|
107
114
|
|
|
108
115
|
//wo
|
|
@@ -145,7 +152,7 @@ async function test() {
|
|
|
145
152
|
})
|
|
146
153
|
|
|
147
154
|
//save
|
|
148
|
-
await wo.save(rsm, { autoInsert:
|
|
155
|
+
await wo.save(rsm, { autoInsert: false })
|
|
149
156
|
.then(function(msg) {
|
|
150
157
|
console.log('save then', msg)
|
|
151
158
|
})
|
|
@@ -177,6 +184,15 @@ async function test() {
|
|
|
177
184
|
// let sr = await wo.select({ name: { $regex: 'PeT', $options: '$i' } })
|
|
178
185
|
// console.log('selectReg', sr)
|
|
179
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
|
+
|
|
180
196
|
//del
|
|
181
197
|
let d = {
|
|
182
198
|
time: '2024-01-01T00:00:00Z',
|
|
@@ -190,7 +206,8 @@ async function test() {
|
|
|
190
206
|
})
|
|
191
207
|
|
|
192
208
|
//del
|
|
193
|
-
let
|
|
209
|
+
let ssDel = await wo.select()
|
|
210
|
+
let ds = ssDel.filter(function(v) {
|
|
194
211
|
return v.name.indexOf('peter') >= 0 || v.name.indexOf('kettle') >= 0 || v.name.indexOf('sandler') >= 0
|
|
195
212
|
})
|
|
196
213
|
await wo.del(ds)
|
|
@@ -205,6 +222,68 @@ async function test() {
|
|
|
205
222
|
let ss2 = await wo.select()
|
|
206
223
|
console.log('select all final', ss2)
|
|
207
224
|
|
|
225
|
+
//清除數據, 後續cache測試共用users表
|
|
226
|
+
await wo.delAll()
|
|
227
|
+
|
|
228
|
+
//woc, 啟用useCache
|
|
229
|
+
let woc = WOrm({
|
|
230
|
+
...opt,
|
|
231
|
+
useCache: true,
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
let rsc = [
|
|
235
|
+
{
|
|
236
|
+
time: '2025-01-01T00:00:00Z',
|
|
237
|
+
name: 'peter',
|
|
238
|
+
value: 123,
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
time: '2025-01-01T00:01:00Z',
|
|
242
|
+
name: 'rosemary',
|
|
243
|
+
value: 123.456,
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
time: '2025-01-01T00:02:00Z',
|
|
247
|
+
name: 'kettle',
|
|
248
|
+
value: 456,
|
|
249
|
+
},
|
|
250
|
+
]
|
|
251
|
+
|
|
252
|
+
let rscm = [
|
|
253
|
+
{
|
|
254
|
+
time: '2025-01-01T00:01:00Z',
|
|
255
|
+
name: 'rosemary(modify)',
|
|
256
|
+
value: 654.321,
|
|
257
|
+
},
|
|
258
|
+
]
|
|
259
|
+
|
|
260
|
+
//insert
|
|
261
|
+
await woc.insert(rsc)
|
|
262
|
+
|
|
263
|
+
//select all (1st time, 從DB讀取並填入快取)
|
|
264
|
+
let sc1 = await woc.select()
|
|
265
|
+
sc1 = sc1.sort((a, b) => a.time - b.time)
|
|
266
|
+
console.log('select all 1st (fill cache)', sc1)
|
|
267
|
+
|
|
268
|
+
//select all (2nd time, 命中快取, 內容須與第一次相同)
|
|
269
|
+
let sc2 = await woc.select()
|
|
270
|
+
sc2 = sc2.sort((a, b) => a.time - b.time)
|
|
271
|
+
console.log('select all 2nd (cache hit)', sc2)
|
|
272
|
+
|
|
273
|
+
//save (更新rosemary, 觸發快取重設)
|
|
274
|
+
await woc.save(rscm, { autoInsert: false })
|
|
275
|
+
.then(function(msg) {
|
|
276
|
+
console.log('save (invalidate cache) then', msg)
|
|
277
|
+
})
|
|
278
|
+
.catch(function(msg) {
|
|
279
|
+
console.log('save (invalidate cache) catch', msg)
|
|
280
|
+
})
|
|
281
|
+
|
|
282
|
+
//select all (3rd time, 快取已重設, 從DB重新讀取, 須反映rosemary更新)
|
|
283
|
+
let sc3 = await woc.select()
|
|
284
|
+
sc3 = sc3.sort((a, b) => a.time - b.time)
|
|
285
|
+
console.log('select all 3rd (reload after save)', sc3)
|
|
286
|
+
|
|
208
287
|
}
|
|
209
288
|
test()
|
|
210
289
|
// change delAll
|
|
@@ -217,7 +296,7 @@ test()
|
|
|
217
296
|
// { n: 1, nModified: 1, ok: 1 },
|
|
218
297
|
// { n: 1, nModified: 1, ok: 1 },
|
|
219
298
|
// { n: 1, nModified: 1, ok: 1 },
|
|
220
|
-
// { n:
|
|
299
|
+
// { n: 0, nModified: 0, ok: 1 }
|
|
221
300
|
// ]
|
|
222
301
|
// select all [
|
|
223
302
|
// { time: 2025-01-01T00:00:00.000Z, name: 'peter', value: 123 },
|
|
@@ -240,8 +319,7 @@ test()
|
|
|
240
319
|
// name: 'kettle(modify)',
|
|
241
320
|
// value: 447
|
|
242
321
|
// },
|
|
243
|
-
// { time: 2025-01-01T00:12:00.000Z, name: 'peter', value: 99 }
|
|
244
|
-
// { time: 2025-01-01T00:13:00.000Z, name: 'sandler', value: null }
|
|
322
|
+
// { time: 2025-01-01T00:12:00.000Z, name: 'peter', value: 99 }
|
|
245
323
|
// ]
|
|
246
324
|
// select by name [
|
|
247
325
|
// { time: 2025-01-01T00:01:00.000Z, name: 'rosemary', value: 123.456 },
|
|
@@ -277,6 +355,8 @@ test()
|
|
|
277
355
|
// value: 447
|
|
278
356
|
// }
|
|
279
357
|
// ]
|
|
358
|
+
// change save
|
|
359
|
+
// save then [ { n: 1, nInserted: 1, ok: 1 } ]
|
|
280
360
|
// change del
|
|
281
361
|
// del then [ { n: 1, nDeleted: 0, ok: 1 } ]
|
|
282
362
|
// change del
|
|
@@ -302,5 +382,26 @@ test()
|
|
|
302
382
|
// value: 113.98
|
|
303
383
|
// }
|
|
304
384
|
// ]
|
|
385
|
+
// change delAll
|
|
386
|
+
// select all 1st (fill cache) [
|
|
387
|
+
// { time: 2025-01-01T00:00:00.000Z, name: 'peter', value: 123 },
|
|
388
|
+
// { time: 2025-01-01T00:01:00.000Z, name: 'rosemary', value: 123.456 },
|
|
389
|
+
// { time: 2025-01-01T00:02:00.000Z, name: 'kettle', value: 456 }
|
|
390
|
+
// ]
|
|
391
|
+
// select all 2nd (cache hit) [
|
|
392
|
+
// { time: 2025-01-01T00:00:00.000Z, name: 'peter', value: 123 },
|
|
393
|
+
// { time: 2025-01-01T00:01:00.000Z, name: 'rosemary', value: 123.456 },
|
|
394
|
+
// { time: 2025-01-01T00:02:00.000Z, name: 'kettle', value: 456 }
|
|
395
|
+
// ]
|
|
396
|
+
// save (invalidate cache) then [ { n: 1, nModified: 1, ok: 1 } ]
|
|
397
|
+
// select all 3rd (reload after save) [
|
|
398
|
+
// { time: 2025-01-01T00:00:00.000Z, name: 'peter', value: 123 },
|
|
399
|
+
// {
|
|
400
|
+
// time: 2025-01-01T00:01:00.000Z,
|
|
401
|
+
// name: 'rosemary(modify)',
|
|
402
|
+
// value: 654.321
|
|
403
|
+
// },
|
|
404
|
+
// { time: 2025-01-01T00:02:00.000Z, name: 'kettle', value: 456 }
|
|
405
|
+
// ]
|
|
305
406
|
|
|
306
407
|
//node g-basic.mjs
|
package/package.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "w-orm-postgresql",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"main": "dist/w-orm-postgresql.umd.js",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"mongo-sql": "^6.2.0",
|
|
7
|
-
"pg": "^8.
|
|
8
|
-
"lodash-es": "^4.
|
|
9
|
-
"wsemi": "^1.
|
|
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.
|
|
12
|
+
"w-package-tools": "^1.1.5"
|
|
13
13
|
},
|
|
14
14
|
"scripts": {
|
|
15
15
|
"test": "mocha --parallel --timeout 60000",
|
package/script.txt
CHANGED
package/src/WOrmPostgresql.mjs
CHANGED
|
@@ -19,6 +19,7 @@ import isnum from 'wsemi/src/isnum.mjs'
|
|
|
19
19
|
import isint from 'wsemi/src/isint.mjs'
|
|
20
20
|
import isbol from 'wsemi/src/isbol.mjs'
|
|
21
21
|
import isDate from 'wsemi/src/isDate.mjs'
|
|
22
|
+
import haskey from 'wsemi/src/haskey.mjs'
|
|
22
23
|
import pmSeries from 'wsemi/src/pmSeries.mjs'
|
|
23
24
|
|
|
24
25
|
|
|
@@ -30,10 +31,14 @@ import pmSeries from 'wsemi/src/pmSeries.mjs'
|
|
|
30
31
|
* @param {String} [opt.url='postgresql://127.0.0.1:5432'] 輸入連接資料庫字串,預設'postgresql://127.0.0.1:5432'
|
|
31
32
|
* @param {String} [opt.db='worm'] 輸入使用資料庫名稱字串,預設'worm'
|
|
32
33
|
* @param {String} [opt.cl='test'] 輸入使用資料表名稱字串,預設'test'
|
|
34
|
+
* @param {Boolean} [opt.useCache=false] 輸入是否使用select快取,適用於單程序操作,預設false
|
|
33
35
|
* @returns {Object} 回傳操作資料庫物件,各事件功能詳見說明
|
|
34
36
|
*/
|
|
35
37
|
function WOrmPostgresql(opt = {}) {
|
|
36
38
|
|
|
39
|
+
//_cache
|
|
40
|
+
let _cache = null
|
|
41
|
+
|
|
37
42
|
//url
|
|
38
43
|
let url = get(opt, 'url')
|
|
39
44
|
if (!isestr(url)) {
|
|
@@ -52,6 +57,12 @@ function WOrmPostgresql(opt = {}) {
|
|
|
52
57
|
cl = 'test'
|
|
53
58
|
}
|
|
54
59
|
|
|
60
|
+
//useCache
|
|
61
|
+
let useCache = get(opt, 'useCache')
|
|
62
|
+
if (!isbol(useCache)) {
|
|
63
|
+
useCache = false
|
|
64
|
+
}
|
|
65
|
+
|
|
55
66
|
//getValueType
|
|
56
67
|
function getValueType(value) {
|
|
57
68
|
if (isstr(value)) {
|
|
@@ -144,6 +155,39 @@ function WOrmPostgresql(opt = {}) {
|
|
|
144
155
|
let PgClient = pg.Client
|
|
145
156
|
// console.log('PgClient', PgClient)
|
|
146
157
|
|
|
158
|
+
//clearCache
|
|
159
|
+
function clearCache() {
|
|
160
|
+
_cache = null
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
//getCacheKey
|
|
164
|
+
function getCacheKey(find = {}, order = {}) {
|
|
165
|
+
return JSON.stringify({
|
|
166
|
+
find,
|
|
167
|
+
order,
|
|
168
|
+
})
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
//getCache
|
|
172
|
+
function getCache(find = {}, order = {}) {
|
|
173
|
+
if (iseobj(_cache)) {
|
|
174
|
+
let key = getCacheKey(find, order)
|
|
175
|
+
if (haskey(_cache, key)) {
|
|
176
|
+
return cloneDeep(_cache[key]) //與外部使用數據脫勾
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return null
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
//setCache
|
|
183
|
+
function setCache(find = {}, order = {}, data = []) {
|
|
184
|
+
if (!iseobj(_cache)) {
|
|
185
|
+
_cache = {}
|
|
186
|
+
}
|
|
187
|
+
let key = getCacheKey(find, order)
|
|
188
|
+
_cache[key] = cloneDeep(data) //與外部使用數據脫勾
|
|
189
|
+
}
|
|
190
|
+
|
|
147
191
|
/**
|
|
148
192
|
* 創建資料表
|
|
149
193
|
*
|
|
@@ -218,6 +262,11 @@ function WOrmPostgresql(opt = {}) {
|
|
|
218
262
|
}
|
|
219
263
|
// console.log('res', res)
|
|
220
264
|
|
|
265
|
+
//update
|
|
266
|
+
if (useCache) {
|
|
267
|
+
clearCache()
|
|
268
|
+
}
|
|
269
|
+
|
|
221
270
|
//check
|
|
222
271
|
if (isErr) {
|
|
223
272
|
return Promise.reject(res)
|
|
@@ -238,6 +287,14 @@ function WOrmPostgresql(opt = {}) {
|
|
|
238
287
|
let isErr = false
|
|
239
288
|
let res = null
|
|
240
289
|
|
|
290
|
+
//cache
|
|
291
|
+
if (useCache) {
|
|
292
|
+
let cache = getCache(find, order)
|
|
293
|
+
if (isarr(cache)) {
|
|
294
|
+
return cache
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
241
298
|
//client
|
|
242
299
|
let client = new PgClient({ connectionString })
|
|
243
300
|
|
|
@@ -286,6 +343,11 @@ function WOrmPostgresql(opt = {}) {
|
|
|
286
343
|
res = `can not select by find[${JSON.stringify(find)}]`
|
|
287
344
|
}
|
|
288
345
|
|
|
346
|
+
//cache
|
|
347
|
+
if (useCache && !isErr) {
|
|
348
|
+
setCache(find, order, res)
|
|
349
|
+
}
|
|
350
|
+
|
|
289
351
|
}
|
|
290
352
|
catch (err) {
|
|
291
353
|
isErr = true
|
|
@@ -389,9 +451,6 @@ function WOrmPostgresql(opt = {}) {
|
|
|
389
451
|
ok: 1,
|
|
390
452
|
}
|
|
391
453
|
|
|
392
|
-
//emit
|
|
393
|
-
ee.emit('change', 'insert', data, res)
|
|
394
|
-
|
|
395
454
|
})
|
|
396
455
|
.catch((err) => {
|
|
397
456
|
isErr = true
|
|
@@ -408,6 +467,21 @@ function WOrmPostgresql(opt = {}) {
|
|
|
408
467
|
client = null
|
|
409
468
|
}
|
|
410
469
|
|
|
470
|
+
//update
|
|
471
|
+
if (useCache) {
|
|
472
|
+
clearCache()
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
//emit
|
|
476
|
+
if (!isErr) {
|
|
477
|
+
try {
|
|
478
|
+
ee.emit('change', 'insert', data, res)
|
|
479
|
+
}
|
|
480
|
+
catch (err) {
|
|
481
|
+
console.log(err)
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
411
485
|
//check
|
|
412
486
|
if (isErr) {
|
|
413
487
|
return Promise.reject(res)
|
|
@@ -641,9 +715,6 @@ function WOrmPostgresql(opt = {}) {
|
|
|
641
715
|
return rest
|
|
642
716
|
})
|
|
643
717
|
|
|
644
|
-
//emit
|
|
645
|
-
ee.emit('change', 'save', data, res)
|
|
646
|
-
|
|
647
718
|
}
|
|
648
719
|
catch (err) {
|
|
649
720
|
isErr = true
|
|
@@ -654,6 +725,21 @@ function WOrmPostgresql(opt = {}) {
|
|
|
654
725
|
client = null
|
|
655
726
|
}
|
|
656
727
|
|
|
728
|
+
//update
|
|
729
|
+
if (useCache) {
|
|
730
|
+
clearCache()
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
//emit
|
|
734
|
+
if (!isErr) {
|
|
735
|
+
try {
|
|
736
|
+
ee.emit('change', 'save', data, res)
|
|
737
|
+
}
|
|
738
|
+
catch (err) {
|
|
739
|
+
console.log(err)
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
|
|
657
743
|
//check
|
|
658
744
|
if (isErr) {
|
|
659
745
|
return Promise.reject(res)
|
|
@@ -766,9 +852,6 @@ function WOrmPostgresql(opt = {}) {
|
|
|
766
852
|
return rest
|
|
767
853
|
})
|
|
768
854
|
|
|
769
|
-
//emit
|
|
770
|
-
ee.emit('change', 'del', data, res)
|
|
771
|
-
|
|
772
855
|
}
|
|
773
856
|
catch (err) {
|
|
774
857
|
isErr = true
|
|
@@ -779,6 +862,21 @@ function WOrmPostgresql(opt = {}) {
|
|
|
779
862
|
client = null
|
|
780
863
|
}
|
|
781
864
|
|
|
865
|
+
//update
|
|
866
|
+
if (useCache) {
|
|
867
|
+
clearCache()
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
//emit
|
|
871
|
+
if (!isErr) {
|
|
872
|
+
try {
|
|
873
|
+
ee.emit('change', 'del', data, res)
|
|
874
|
+
}
|
|
875
|
+
catch (err) {
|
|
876
|
+
console.log(err)
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
|
|
782
880
|
//check
|
|
783
881
|
if (isErr) {
|
|
784
882
|
return Promise.reject(res)
|
|
@@ -856,9 +954,6 @@ function WOrmPostgresql(opt = {}) {
|
|
|
856
954
|
ok: 1,
|
|
857
955
|
}
|
|
858
956
|
|
|
859
|
-
//emit
|
|
860
|
-
ee.emit('change', 'delAll', null, res)
|
|
861
|
-
|
|
862
957
|
})
|
|
863
958
|
.catch((err) => {
|
|
864
959
|
isErr = true
|
|
@@ -875,6 +970,21 @@ function WOrmPostgresql(opt = {}) {
|
|
|
875
970
|
client = null
|
|
876
971
|
}
|
|
877
972
|
|
|
973
|
+
//update
|
|
974
|
+
if (useCache) {
|
|
975
|
+
clearCache()
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
//emit
|
|
979
|
+
if (!isErr) {
|
|
980
|
+
try {
|
|
981
|
+
ee.emit('change', 'delAll', null, res)
|
|
982
|
+
}
|
|
983
|
+
catch (err) {
|
|
984
|
+
console.log(err)
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
|
|
878
988
|
//check
|
|
879
989
|
if (isErr) {
|
|
880
990
|
return Promise.reject(res)
|