w-orm-postgresql 1.0.0 → 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 +83 -62
- 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 +256 -148
- package/docs/index.html +1 -1
- package/g-basic.mjs +82 -64
- package/package.json +6 -6
- package/script.txt +4 -5
- package/src/WOrmPostgresql.mjs +255 -147
- package/test/basic.test.mjs +161 -120
package/src/WOrmPostgresql.mjs
CHANGED
|
@@ -32,12 +32,14 @@ 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
|
-
// url = `postgresql://${user}:${password}@${ip}:${port}`
|
|
41
43
|
url = 'postgresql://user:password@127.0.0.1:5432'
|
|
42
44
|
}
|
|
43
45
|
|
|
@@ -141,19 +143,52 @@ function WOrmPostgresql(opt = {}) {
|
|
|
141
143
|
//ee
|
|
142
144
|
let ee = new events.EventEmitter()
|
|
143
145
|
|
|
144
|
-
//PgClient
|
|
145
|
-
let PgClient = pg.Client
|
|
146
|
-
// console.log('PgClient', PgClient)
|
|
147
|
-
|
|
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
|
+
|
|
148
183
|
/**
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
184
|
+
* 創建資料表
|
|
185
|
+
*
|
|
186
|
+
* @memberOf WOrmPostgresql
|
|
187
|
+
* @param {String} cl 輸入資料表名字串
|
|
188
|
+
* @param {String} pk 輸入主鍵字串
|
|
189
|
+
* @param {Array|Object} arr 輸入數據物件陣列或數據物件
|
|
190
|
+
* @returns {Promise} 回傳Promise,resolve回傳成功訊息,reject回傳錯誤訊息
|
|
191
|
+
*/
|
|
157
192
|
async function createTable(cl, pk, arr) {
|
|
158
193
|
let isErr = false
|
|
159
194
|
let res = null
|
|
@@ -216,31 +251,40 @@ function WOrmPostgresql(opt = {}) {
|
|
|
216
251
|
finally {
|
|
217
252
|
await client.end()
|
|
218
253
|
client = null
|
|
219
|
-
}
|
|
220
|
-
// console.log('res', res)
|
|
221
|
-
|
|
222
|
-
//
|
|
223
|
-
|
|
224
|
-
|
|
254
|
+
}
|
|
255
|
+
// console.log('res', res)
|
|
256
|
+
|
|
257
|
+
//update
|
|
258
|
+
clearCache()
|
|
259
|
+
|
|
260
|
+
//check
|
|
261
|
+
if (isErr) {
|
|
262
|
+
return Promise.reject(res)
|
|
225
263
|
}
|
|
226
264
|
|
|
227
265
|
return res
|
|
228
266
|
}
|
|
229
267
|
|
|
230
268
|
/**
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
async function select(find = {}, order = {}) {
|
|
239
|
-
let isErr = false
|
|
240
|
-
let res = null
|
|
241
|
-
|
|
242
|
-
//
|
|
243
|
-
let
|
|
269
|
+
* 查詢數據
|
|
270
|
+
*
|
|
271
|
+
* @memberOf WOrmPostgresql
|
|
272
|
+
* @param {Object} [find={}] 輸入查詢條件物件
|
|
273
|
+
* @param {Object} [order={}] 輸入排序條件物件
|
|
274
|
+
* @returns {Promise} 回傳Promise,resolve回傳數據,reject回傳錯誤訊息
|
|
275
|
+
*/
|
|
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 })
|
|
244
288
|
|
|
245
289
|
//connect
|
|
246
290
|
try {
|
|
@@ -282,12 +326,17 @@ function WOrmPostgresql(opt = {}) {
|
|
|
282
326
|
res = get(r, 'rows')
|
|
283
327
|
|
|
284
328
|
//check
|
|
285
|
-
if (!isarr(res)) {
|
|
286
|
-
isErr = true
|
|
287
|
-
res = `can not select by find[${JSON.stringify(find)}]`
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
|
|
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
|
+
}
|
|
291
340
|
catch (err) {
|
|
292
341
|
isErr = true
|
|
293
342
|
res = err
|
|
@@ -307,16 +356,25 @@ function WOrmPostgresql(opt = {}) {
|
|
|
307
356
|
}
|
|
308
357
|
|
|
309
358
|
/**
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
359
|
+
* 插入數據
|
|
360
|
+
*
|
|
361
|
+
* @memberOf WOrmPostgresql
|
|
362
|
+
* @param {Object|Array} data 輸入數據物件或陣列
|
|
363
|
+
* @returns {Promise} 回傳Promise,resolve回傳插入結果,reject回傳錯誤訊息
|
|
364
|
+
*/
|
|
316
365
|
async function insert(data) {
|
|
317
366
|
let isErr = false
|
|
318
367
|
let res = null
|
|
319
368
|
|
|
369
|
+
//check
|
|
370
|
+
if (!iseobj(data) && !isearr(data)) {
|
|
371
|
+
return {
|
|
372
|
+
n: 0,
|
|
373
|
+
nInserted: 0,
|
|
374
|
+
ok: 1,
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
320
378
|
//cloneDeep
|
|
321
379
|
data = cloneDeep(data)
|
|
322
380
|
|
|
@@ -375,20 +433,17 @@ function WOrmPostgresql(opt = {}) {
|
|
|
375
433
|
.then(() => {
|
|
376
434
|
|
|
377
435
|
//res
|
|
378
|
-
res = {
|
|
379
|
-
n: nAll,
|
|
380
|
-
nInserted: nInsert,
|
|
381
|
-
ok: 1,
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
isErr = true
|
|
390
|
-
res = err.message
|
|
391
|
-
})
|
|
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
|
+
})
|
|
392
447
|
|
|
393
448
|
}
|
|
394
449
|
catch (err) {
|
|
@@ -396,31 +451,49 @@ function WOrmPostgresql(opt = {}) {
|
|
|
396
451
|
res = err
|
|
397
452
|
}
|
|
398
453
|
finally {
|
|
399
|
-
await client.end()
|
|
400
|
-
client = null
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
//
|
|
404
|
-
|
|
405
|
-
|
|
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)
|
|
406
474
|
}
|
|
407
475
|
|
|
408
476
|
return res
|
|
409
477
|
}
|
|
410
478
|
|
|
411
479
|
/**
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
480
|
+
* 儲存數據
|
|
481
|
+
*
|
|
482
|
+
* @memberOf WOrmPostgresql
|
|
483
|
+
* @param {Object|Array} data 輸入數據物件或陣列
|
|
484
|
+
* @param {Object} [option={}] 輸入設定物件,預設為{}
|
|
485
|
+
* @param {boolean} [option.autoInsert=true] 輸入是否於儲存時發現原本無數據,則自動改以插入處理,預設為true
|
|
486
|
+
* @returns {Promise} 回傳Promise,resolve回傳儲存結果,reject回傳錯誤訊息
|
|
487
|
+
*/
|
|
420
488
|
async function save(data, option = {}) {
|
|
421
489
|
let isErr = false
|
|
422
490
|
let res = null
|
|
423
491
|
|
|
492
|
+
//check
|
|
493
|
+
if (!iseobj(data) && !isearr(data)) {
|
|
494
|
+
return []
|
|
495
|
+
}
|
|
496
|
+
|
|
424
497
|
//cloneDeep
|
|
425
498
|
data = cloneDeep(data)
|
|
426
499
|
|
|
@@ -625,41 +698,56 @@ function WOrmPostgresql(opt = {}) {
|
|
|
625
698
|
|
|
626
699
|
}
|
|
627
700
|
|
|
628
|
-
return rest
|
|
629
|
-
})
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
isErr = true
|
|
637
|
-
res = err
|
|
638
|
-
}
|
|
701
|
+
return rest
|
|
702
|
+
})
|
|
703
|
+
|
|
704
|
+
}
|
|
705
|
+
catch (err) {
|
|
706
|
+
isErr = true
|
|
707
|
+
res = err
|
|
708
|
+
}
|
|
639
709
|
finally {
|
|
640
|
-
await client.end()
|
|
641
|
-
client = null
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
//
|
|
645
|
-
|
|
646
|
-
|
|
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)
|
|
647
730
|
}
|
|
648
731
|
|
|
649
732
|
return res
|
|
650
733
|
}
|
|
651
734
|
|
|
652
735
|
/**
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
736
|
+
* 刪除數據
|
|
737
|
+
*
|
|
738
|
+
* @memberOf WOrmPostgresql
|
|
739
|
+
* @param {Object|Array} data 輸入數據物件或陣列
|
|
740
|
+
* @returns {Promise} 回傳Promise,resolve回傳刪除結果,reject回傳錯誤訊息
|
|
741
|
+
*/
|
|
659
742
|
async function del(data) {
|
|
660
743
|
let isErr = false
|
|
661
744
|
let res = null
|
|
662
745
|
|
|
746
|
+
//check
|
|
747
|
+
if (!iseobj(data) && !isearr(data)) {
|
|
748
|
+
return []
|
|
749
|
+
}
|
|
750
|
+
|
|
663
751
|
//cloneDeep
|
|
664
752
|
data = cloneDeep(data)
|
|
665
753
|
|
|
@@ -744,38 +832,48 @@ function WOrmPostgresql(opt = {}) {
|
|
|
744
832
|
}
|
|
745
833
|
|
|
746
834
|
})
|
|
747
|
-
|
|
748
|
-
return rest
|
|
749
|
-
})
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
isErr = true
|
|
757
|
-
res = err
|
|
758
|
-
}
|
|
835
|
+
|
|
836
|
+
return rest
|
|
837
|
+
})
|
|
838
|
+
|
|
839
|
+
}
|
|
840
|
+
catch (err) {
|
|
841
|
+
isErr = true
|
|
842
|
+
res = err
|
|
843
|
+
}
|
|
759
844
|
finally {
|
|
760
|
-
await client.end()
|
|
761
|
-
client = null
|
|
762
|
-
}
|
|
763
|
-
|
|
764
|
-
//
|
|
765
|
-
|
|
766
|
-
|
|
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)
|
|
767
865
|
}
|
|
768
866
|
|
|
769
867
|
return res
|
|
770
868
|
}
|
|
771
869
|
|
|
772
870
|
/**
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
871
|
+
* 刪除全部數據,需與del分開,避免未傳數據導致直接刪除全表
|
|
872
|
+
*
|
|
873
|
+
* @memberOf WOrmPostgresql
|
|
874
|
+
* @param {Object} [find={}] 輸入刪除條件物件
|
|
875
|
+
* @returns {Promise} 回傳Promise,resolve回傳刪除結果,reject回傳錯誤訊息
|
|
876
|
+
*/
|
|
779
877
|
async function delAll(find = {}) {
|
|
780
878
|
let isErr = false
|
|
781
879
|
let res = null
|
|
@@ -832,20 +930,17 @@ function WOrmPostgresql(opt = {}) {
|
|
|
832
930
|
.then((r) => {
|
|
833
931
|
|
|
834
932
|
//res
|
|
835
|
-
res = {
|
|
836
|
-
n: r.rowCount,
|
|
837
|
-
nDeleted: r.rowCount,
|
|
838
|
-
ok: 1,
|
|
839
|
-
}
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
isErr = true
|
|
847
|
-
res = err.message
|
|
848
|
-
})
|
|
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
|
+
})
|
|
849
944
|
|
|
850
945
|
}
|
|
851
946
|
catch (err) {
|
|
@@ -853,13 +948,26 @@ function WOrmPostgresql(opt = {}) {
|
|
|
853
948
|
res = err
|
|
854
949
|
}
|
|
855
950
|
finally {
|
|
856
|
-
await client.end()
|
|
857
|
-
client = null
|
|
858
|
-
}
|
|
859
|
-
|
|
860
|
-
//
|
|
861
|
-
|
|
862
|
-
|
|
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)
|
|
863
971
|
}
|
|
864
972
|
|
|
865
973
|
return res
|