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.
- package/README.md +67 -47
- 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 +196 -106
- package/docs/index.html +1 -1
- package/g-basic.mjs +65 -47
- package/package.json +5 -5
- package/script.txt +0 -1
- package/src/WOrmPostgresql.mjs +195 -105
- package/test/basic.test.mjs +147 -105
package/src/WOrmPostgresql.mjs
CHANGED
|
@@ -32,10 +32,13 @@ import pmSeries from 'wsemi/src/pmSeries.mjs'
|
|
|
32
32
|
* @param {String} [opt.cl='test'] 輸入使用資料表名稱字串,預設'test'
|
|
33
33
|
* @returns {Object} 回傳操作資料庫物件,各事件功能詳見說明
|
|
34
34
|
*/
|
|
35
|
-
function WOrmPostgresql(opt = {}) {
|
|
36
|
-
|
|
37
|
-
//
|
|
38
|
-
let
|
|
35
|
+
function WOrmPostgresql(opt = {}) {
|
|
36
|
+
|
|
37
|
+
//_cache
|
|
38
|
+
let _cache = null
|
|
39
|
+
|
|
40
|
+
//url
|
|
41
|
+
let url = get(opt, 'url')
|
|
39
42
|
if (!isestr(url)) {
|
|
40
43
|
url = 'postgresql://user:password@127.0.0.1:5432'
|
|
41
44
|
}
|
|
@@ -140,10 +143,43 @@ function WOrmPostgresql(opt = {}) {
|
|
|
140
143
|
//ee
|
|
141
144
|
let ee = new events.EventEmitter()
|
|
142
145
|
|
|
143
|
-
//PgClient
|
|
144
|
-
let PgClient = pg.Client
|
|
145
|
-
// console.log('PgClient', PgClient)
|
|
146
|
-
|
|
146
|
+
//PgClient
|
|
147
|
+
let PgClient = pg.Client
|
|
148
|
+
// console.log('PgClient', PgClient)
|
|
149
|
+
|
|
150
|
+
//clearCache
|
|
151
|
+
function clearCache() {
|
|
152
|
+
_cache = null
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
//getCacheKey
|
|
156
|
+
function getCacheKey(find = {}, order = {}) {
|
|
157
|
+
return JSON.stringify({
|
|
158
|
+
find,
|
|
159
|
+
order,
|
|
160
|
+
})
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
//getCache
|
|
164
|
+
function getCache(find = {}, order = {}) {
|
|
165
|
+
if (iseobj(_cache)) {
|
|
166
|
+
let key = getCacheKey(find, order)
|
|
167
|
+
if (Object.prototype.hasOwnProperty.call(_cache, key)) {
|
|
168
|
+
return cloneDeep(_cache[key]) //與外部使用數據脫勾
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return null
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
//setCache
|
|
175
|
+
function setCache(find = {}, order = {}, data = []) {
|
|
176
|
+
if (!iseobj(_cache)) {
|
|
177
|
+
_cache = {}
|
|
178
|
+
}
|
|
179
|
+
let key = getCacheKey(find, order)
|
|
180
|
+
_cache[key] = cloneDeep(data) //與外部使用數據脫勾
|
|
181
|
+
}
|
|
182
|
+
|
|
147
183
|
/**
|
|
148
184
|
* 創建資料表
|
|
149
185
|
*
|
|
@@ -215,12 +251,15 @@ function WOrmPostgresql(opt = {}) {
|
|
|
215
251
|
finally {
|
|
216
252
|
await client.end()
|
|
217
253
|
client = null
|
|
218
|
-
}
|
|
219
|
-
// console.log('res', res)
|
|
220
|
-
|
|
221
|
-
//
|
|
222
|
-
|
|
223
|
-
|
|
254
|
+
}
|
|
255
|
+
// console.log('res', res)
|
|
256
|
+
|
|
257
|
+
//update
|
|
258
|
+
clearCache()
|
|
259
|
+
|
|
260
|
+
//check
|
|
261
|
+
if (isErr) {
|
|
262
|
+
return Promise.reject(res)
|
|
224
263
|
}
|
|
225
264
|
|
|
226
265
|
return res
|
|
@@ -234,12 +273,18 @@ function WOrmPostgresql(opt = {}) {
|
|
|
234
273
|
* @param {Object} [order={}] 輸入排序條件物件
|
|
235
274
|
* @returns {Promise} 回傳Promise,resolve回傳數據,reject回傳錯誤訊息
|
|
236
275
|
*/
|
|
237
|
-
async function select(find = {}, order = {}) {
|
|
238
|
-
let isErr = false
|
|
239
|
-
let res = null
|
|
240
|
-
|
|
241
|
-
//
|
|
242
|
-
let
|
|
276
|
+
async function select(find = {}, order = {}) {
|
|
277
|
+
let isErr = false
|
|
278
|
+
let res = null
|
|
279
|
+
|
|
280
|
+
//cache
|
|
281
|
+
let cache = getCache(find, order)
|
|
282
|
+
if (isarr(cache)) {
|
|
283
|
+
return cache
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
//client
|
|
287
|
+
let client = new PgClient({ connectionString })
|
|
243
288
|
|
|
244
289
|
//connect
|
|
245
290
|
try {
|
|
@@ -281,12 +326,17 @@ function WOrmPostgresql(opt = {}) {
|
|
|
281
326
|
res = get(r, 'rows')
|
|
282
327
|
|
|
283
328
|
//check
|
|
284
|
-
if (!isarr(res)) {
|
|
285
|
-
isErr = true
|
|
286
|
-
res = `can not select by find[${JSON.stringify(find)}]`
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
|
|
329
|
+
if (!isarr(res)) {
|
|
330
|
+
isErr = true
|
|
331
|
+
res = `can not select by find[${JSON.stringify(find)}]`
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
//cache
|
|
335
|
+
if (!isErr) {
|
|
336
|
+
setCache(find, order, res)
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
}
|
|
290
340
|
catch (err) {
|
|
291
341
|
isErr = true
|
|
292
342
|
res = err
|
|
@@ -383,20 +433,17 @@ function WOrmPostgresql(opt = {}) {
|
|
|
383
433
|
.then(() => {
|
|
384
434
|
|
|
385
435
|
//res
|
|
386
|
-
res = {
|
|
387
|
-
n: nAll,
|
|
388
|
-
nInserted: nInsert,
|
|
389
|
-
ok: 1,
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
isErr = true
|
|
398
|
-
res = err.message
|
|
399
|
-
})
|
|
436
|
+
res = {
|
|
437
|
+
n: nAll,
|
|
438
|
+
nInserted: nInsert,
|
|
439
|
+
ok: 1,
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
})
|
|
443
|
+
.catch((err) => {
|
|
444
|
+
isErr = true
|
|
445
|
+
res = err.message
|
|
446
|
+
})
|
|
400
447
|
|
|
401
448
|
}
|
|
402
449
|
catch (err) {
|
|
@@ -404,13 +451,26 @@ function WOrmPostgresql(opt = {}) {
|
|
|
404
451
|
res = err
|
|
405
452
|
}
|
|
406
453
|
finally {
|
|
407
|
-
await client.end()
|
|
408
|
-
client = null
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
//
|
|
412
|
-
|
|
413
|
-
|
|
454
|
+
await client.end()
|
|
455
|
+
client = null
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
//update
|
|
459
|
+
clearCache()
|
|
460
|
+
|
|
461
|
+
//emit
|
|
462
|
+
if (!isErr) {
|
|
463
|
+
try {
|
|
464
|
+
ee.emit('change', 'insert', data, res)
|
|
465
|
+
}
|
|
466
|
+
catch (err) {
|
|
467
|
+
console.log(err)
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
//check
|
|
472
|
+
if (isErr) {
|
|
473
|
+
return Promise.reject(res)
|
|
414
474
|
}
|
|
415
475
|
|
|
416
476
|
return res
|
|
@@ -638,25 +698,35 @@ function WOrmPostgresql(opt = {}) {
|
|
|
638
698
|
|
|
639
699
|
}
|
|
640
700
|
|
|
641
|
-
return rest
|
|
642
|
-
})
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
isErr = true
|
|
650
|
-
res = err
|
|
651
|
-
}
|
|
701
|
+
return rest
|
|
702
|
+
})
|
|
703
|
+
|
|
704
|
+
}
|
|
705
|
+
catch (err) {
|
|
706
|
+
isErr = true
|
|
707
|
+
res = err
|
|
708
|
+
}
|
|
652
709
|
finally {
|
|
653
|
-
await client.end()
|
|
654
|
-
client = null
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
//
|
|
658
|
-
|
|
659
|
-
|
|
710
|
+
await client.end()
|
|
711
|
+
client = null
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
//update
|
|
715
|
+
clearCache()
|
|
716
|
+
|
|
717
|
+
//emit
|
|
718
|
+
if (!isErr) {
|
|
719
|
+
try {
|
|
720
|
+
ee.emit('change', 'save', data, res)
|
|
721
|
+
}
|
|
722
|
+
catch (err) {
|
|
723
|
+
console.log(err)
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
//check
|
|
728
|
+
if (isErr) {
|
|
729
|
+
return Promise.reject(res)
|
|
660
730
|
}
|
|
661
731
|
|
|
662
732
|
return res
|
|
@@ -762,26 +832,36 @@ function WOrmPostgresql(opt = {}) {
|
|
|
762
832
|
}
|
|
763
833
|
|
|
764
834
|
})
|
|
765
|
-
|
|
766
|
-
return rest
|
|
767
|
-
})
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
isErr = true
|
|
775
|
-
res = err
|
|
776
|
-
}
|
|
835
|
+
|
|
836
|
+
return rest
|
|
837
|
+
})
|
|
838
|
+
|
|
839
|
+
}
|
|
840
|
+
catch (err) {
|
|
841
|
+
isErr = true
|
|
842
|
+
res = err
|
|
843
|
+
}
|
|
777
844
|
finally {
|
|
778
|
-
await client.end()
|
|
779
|
-
client = null
|
|
780
|
-
}
|
|
781
|
-
|
|
782
|
-
//
|
|
783
|
-
|
|
784
|
-
|
|
845
|
+
await client.end()
|
|
846
|
+
client = null
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
//update
|
|
850
|
+
clearCache()
|
|
851
|
+
|
|
852
|
+
//emit
|
|
853
|
+
if (!isErr) {
|
|
854
|
+
try {
|
|
855
|
+
ee.emit('change', 'del', data, res)
|
|
856
|
+
}
|
|
857
|
+
catch (err) {
|
|
858
|
+
console.log(err)
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
//check
|
|
863
|
+
if (isErr) {
|
|
864
|
+
return Promise.reject(res)
|
|
785
865
|
}
|
|
786
866
|
|
|
787
867
|
return res
|
|
@@ -850,20 +930,17 @@ function WOrmPostgresql(opt = {}) {
|
|
|
850
930
|
.then((r) => {
|
|
851
931
|
|
|
852
932
|
//res
|
|
853
|
-
res = {
|
|
854
|
-
n: r.rowCount,
|
|
855
|
-
nDeleted: r.rowCount,
|
|
856
|
-
ok: 1,
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
isErr = true
|
|
865
|
-
res = err.message
|
|
866
|
-
})
|
|
933
|
+
res = {
|
|
934
|
+
n: r.rowCount,
|
|
935
|
+
nDeleted: r.rowCount,
|
|
936
|
+
ok: 1,
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
})
|
|
940
|
+
.catch((err) => {
|
|
941
|
+
isErr = true
|
|
942
|
+
res = err.message
|
|
943
|
+
})
|
|
867
944
|
|
|
868
945
|
}
|
|
869
946
|
catch (err) {
|
|
@@ -871,13 +948,26 @@ function WOrmPostgresql(opt = {}) {
|
|
|
871
948
|
res = err
|
|
872
949
|
}
|
|
873
950
|
finally {
|
|
874
|
-
await client.end()
|
|
875
|
-
client = null
|
|
876
|
-
}
|
|
877
|
-
|
|
878
|
-
//
|
|
879
|
-
|
|
880
|
-
|
|
951
|
+
await client.end()
|
|
952
|
+
client = null
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
//update
|
|
956
|
+
clearCache()
|
|
957
|
+
|
|
958
|
+
//emit
|
|
959
|
+
if (!isErr) {
|
|
960
|
+
try {
|
|
961
|
+
ee.emit('change', 'delAll', null, res)
|
|
962
|
+
}
|
|
963
|
+
catch (err) {
|
|
964
|
+
console.log(err)
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
//check
|
|
969
|
+
if (isErr) {
|
|
970
|
+
return Promise.reject(res)
|
|
881
971
|
}
|
|
882
972
|
|
|
883
973
|
return res
|