w-orm-postgresql 1.0.2 → 1.0.4
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 +148 -65
- 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 +216 -196
- package/docs/index.html +1 -1
- package/g-basic.mjs +148 -65
- package/package.json +3 -3
- package/src/WOrmPostgresql.mjs +215 -195
- package/test/basic.test.mjs +666 -455
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,15 +31,16 @@ 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
|
-
function WOrmPostgresql(opt = {}) {
|
|
36
|
-
|
|
37
|
-
//_cache
|
|
38
|
-
let _cache = null
|
|
39
|
-
|
|
40
|
-
//url
|
|
41
|
-
let url = get(opt, 'url')
|
|
37
|
+
function WOrmPostgresql(opt = {}) {
|
|
38
|
+
|
|
39
|
+
//_cache
|
|
40
|
+
let _cache = null
|
|
41
|
+
|
|
42
|
+
//url
|
|
43
|
+
let url = get(opt, 'url')
|
|
42
44
|
if (!isestr(url)) {
|
|
43
45
|
url = 'postgresql://user:password@127.0.0.1:5432'
|
|
44
46
|
}
|
|
@@ -55,6 +57,12 @@ function WOrmPostgresql(opt = {}) {
|
|
|
55
57
|
cl = 'test'
|
|
56
58
|
}
|
|
57
59
|
|
|
60
|
+
//useCache
|
|
61
|
+
let useCache = get(opt, 'useCache')
|
|
62
|
+
if (!isbol(useCache)) {
|
|
63
|
+
useCache = false
|
|
64
|
+
}
|
|
65
|
+
|
|
58
66
|
//getValueType
|
|
59
67
|
function getValueType(value) {
|
|
60
68
|
if (isstr(value)) {
|
|
@@ -143,43 +151,43 @@ function WOrmPostgresql(opt = {}) {
|
|
|
143
151
|
//ee
|
|
144
152
|
let ee = new events.EventEmitter()
|
|
145
153
|
|
|
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 (
|
|
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
|
-
|
|
154
|
+
//PgClient
|
|
155
|
+
let PgClient = pg.Client
|
|
156
|
+
// console.log('PgClient', PgClient)
|
|
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
|
+
|
|
183
191
|
/**
|
|
184
192
|
* 創建資料表
|
|
185
193
|
*
|
|
@@ -251,15 +259,17 @@ function WOrmPostgresql(opt = {}) {
|
|
|
251
259
|
finally {
|
|
252
260
|
await client.end()
|
|
253
261
|
client = null
|
|
254
|
-
}
|
|
255
|
-
// console.log('res', res)
|
|
256
|
-
|
|
257
|
-
//update
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
262
|
+
}
|
|
263
|
+
// console.log('res', res)
|
|
264
|
+
|
|
265
|
+
//update
|
|
266
|
+
if (useCache) {
|
|
267
|
+
clearCache()
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
//check
|
|
271
|
+
if (isErr) {
|
|
272
|
+
return Promise.reject(res)
|
|
263
273
|
}
|
|
264
274
|
|
|
265
275
|
return res
|
|
@@ -273,18 +283,20 @@ function WOrmPostgresql(opt = {}) {
|
|
|
273
283
|
* @param {Object} [order={}] 輸入排序條件物件
|
|
274
284
|
* @returns {Promise} 回傳Promise,resolve回傳數據,reject回傳錯誤訊息
|
|
275
285
|
*/
|
|
276
|
-
async function select(find = {}, order = {}) {
|
|
277
|
-
let isErr = false
|
|
278
|
-
let res = null
|
|
279
|
-
|
|
280
|
-
//cache
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
286
|
+
async function select(find = {}, order = {}) {
|
|
287
|
+
let isErr = false
|
|
288
|
+
let res = null
|
|
289
|
+
|
|
290
|
+
//cache
|
|
291
|
+
if (useCache) {
|
|
292
|
+
let cache = getCache(find, order)
|
|
293
|
+
if (isarr(cache)) {
|
|
294
|
+
return cache
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
//client
|
|
299
|
+
let client = new PgClient({ connectionString })
|
|
288
300
|
|
|
289
301
|
//connect
|
|
290
302
|
try {
|
|
@@ -326,17 +338,17 @@ function WOrmPostgresql(opt = {}) {
|
|
|
326
338
|
res = get(r, 'rows')
|
|
327
339
|
|
|
328
340
|
//check
|
|
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
|
-
}
|
|
341
|
+
if (!isarr(res)) {
|
|
342
|
+
isErr = true
|
|
343
|
+
res = `can not select by find[${JSON.stringify(find)}]`
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
//cache
|
|
347
|
+
if (useCache && !isErr) {
|
|
348
|
+
setCache(find, order, res)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
}
|
|
340
352
|
catch (err) {
|
|
341
353
|
isErr = true
|
|
342
354
|
res = err
|
|
@@ -433,17 +445,17 @@ function WOrmPostgresql(opt = {}) {
|
|
|
433
445
|
.then(() => {
|
|
434
446
|
|
|
435
447
|
//res
|
|
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
|
-
})
|
|
448
|
+
res = {
|
|
449
|
+
n: nAll,
|
|
450
|
+
nInserted: nInsert,
|
|
451
|
+
ok: 1,
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
})
|
|
455
|
+
.catch((err) => {
|
|
456
|
+
isErr = true
|
|
457
|
+
res = err.message
|
|
458
|
+
})
|
|
447
459
|
|
|
448
460
|
}
|
|
449
461
|
catch (err) {
|
|
@@ -451,26 +463,28 @@ function WOrmPostgresql(opt = {}) {
|
|
|
451
463
|
res = err
|
|
452
464
|
}
|
|
453
465
|
finally {
|
|
454
|
-
await client.end()
|
|
455
|
-
client = null
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
//update
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
466
|
+
await client.end()
|
|
467
|
+
client = null
|
|
468
|
+
}
|
|
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
|
+
|
|
485
|
+
//check
|
|
486
|
+
if (isErr) {
|
|
487
|
+
return Promise.reject(res)
|
|
474
488
|
}
|
|
475
489
|
|
|
476
490
|
return res
|
|
@@ -698,35 +712,37 @@ function WOrmPostgresql(opt = {}) {
|
|
|
698
712
|
|
|
699
713
|
}
|
|
700
714
|
|
|
701
|
-
return rest
|
|
702
|
-
})
|
|
703
|
-
|
|
704
|
-
}
|
|
705
|
-
catch (err) {
|
|
706
|
-
isErr = true
|
|
707
|
-
res = err
|
|
708
|
-
}
|
|
715
|
+
return rest
|
|
716
|
+
})
|
|
717
|
+
|
|
718
|
+
}
|
|
719
|
+
catch (err) {
|
|
720
|
+
isErr = true
|
|
721
|
+
res = err
|
|
722
|
+
}
|
|
709
723
|
finally {
|
|
710
|
-
await client.end()
|
|
711
|
-
client = null
|
|
712
|
-
}
|
|
713
|
-
|
|
714
|
-
//update
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
724
|
+
await client.end()
|
|
725
|
+
client = null
|
|
726
|
+
}
|
|
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
|
+
|
|
743
|
+
//check
|
|
744
|
+
if (isErr) {
|
|
745
|
+
return Promise.reject(res)
|
|
730
746
|
}
|
|
731
747
|
|
|
732
748
|
return res
|
|
@@ -832,36 +848,38 @@ function WOrmPostgresql(opt = {}) {
|
|
|
832
848
|
}
|
|
833
849
|
|
|
834
850
|
})
|
|
835
|
-
|
|
836
|
-
return rest
|
|
837
|
-
})
|
|
838
|
-
|
|
839
|
-
}
|
|
840
|
-
catch (err) {
|
|
841
|
-
isErr = true
|
|
842
|
-
res = err
|
|
843
|
-
}
|
|
851
|
+
|
|
852
|
+
return rest
|
|
853
|
+
})
|
|
854
|
+
|
|
855
|
+
}
|
|
856
|
+
catch (err) {
|
|
857
|
+
isErr = true
|
|
858
|
+
res = err
|
|
859
|
+
}
|
|
844
860
|
finally {
|
|
845
|
-
await client.end()
|
|
846
|
-
client = null
|
|
847
|
-
}
|
|
848
|
-
|
|
849
|
-
//update
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
861
|
+
await client.end()
|
|
862
|
+
client = null
|
|
863
|
+
}
|
|
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
|
+
|
|
880
|
+
//check
|
|
881
|
+
if (isErr) {
|
|
882
|
+
return Promise.reject(res)
|
|
865
883
|
}
|
|
866
884
|
|
|
867
885
|
return res
|
|
@@ -930,17 +948,17 @@ function WOrmPostgresql(opt = {}) {
|
|
|
930
948
|
.then((r) => {
|
|
931
949
|
|
|
932
950
|
//res
|
|
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
|
-
})
|
|
951
|
+
res = {
|
|
952
|
+
n: r.rowCount,
|
|
953
|
+
nDeleted: r.rowCount,
|
|
954
|
+
ok: 1,
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
})
|
|
958
|
+
.catch((err) => {
|
|
959
|
+
isErr = true
|
|
960
|
+
res = err.message
|
|
961
|
+
})
|
|
944
962
|
|
|
945
963
|
}
|
|
946
964
|
catch (err) {
|
|
@@ -948,26 +966,28 @@ function WOrmPostgresql(opt = {}) {
|
|
|
948
966
|
res = err
|
|
949
967
|
}
|
|
950
968
|
finally {
|
|
951
|
-
await client.end()
|
|
952
|
-
client = null
|
|
953
|
-
}
|
|
954
|
-
|
|
955
|
-
//update
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
969
|
+
await client.end()
|
|
970
|
+
client = null
|
|
971
|
+
}
|
|
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
|
+
|
|
988
|
+
//check
|
|
989
|
+
if (isErr) {
|
|
990
|
+
return Promise.reject(res)
|
|
971
991
|
}
|
|
972
992
|
|
|
973
993
|
return res
|