w-orm-lowdb 1.0.7 → 1.0.9
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 +19 -0
- package/dist/w-orm-lowdb.umd.js +2 -2
- package/dist/w-orm-lowdb.umd.js.map +1 -1
- package/docs/WOrmLowdb.html +47 -7
- package/docs/WOrmLowdb.mjs.html +45 -33
- package/docs/index.html +1 -1
- package/g-basic.mjs +19 -0
- package/package.json +1 -1
- package/src/WOrmLowdb.mjs +44 -32
- package/test/basic.test.mjs +402 -400
- package/test/cache.test.mjs +172 -0
package/docs/WOrmLowdb.html
CHANGED
|
@@ -80,7 +80,7 @@
|
|
|
80
80
|
|
|
81
81
|
<dt class="tag-source">Source:</dt>
|
|
82
82
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
83
|
-
<a href="WOrmLowdb.mjs.html">WOrmLowdb.mjs</a>, <a href="WOrmLowdb.mjs.html#
|
|
83
|
+
<a href="WOrmLowdb.mjs.html">WOrmLowdb.mjs</a>, <a href="WOrmLowdb.mjs.html#line40">line 40</a>
|
|
84
84
|
</li></ul></dd>
|
|
85
85
|
|
|
86
86
|
|
|
@@ -336,6 +336,46 @@
|
|
|
336
336
|
</tr>
|
|
337
337
|
|
|
338
338
|
|
|
339
|
+
|
|
340
|
+
<tr>
|
|
341
|
+
|
|
342
|
+
<td class="name"><code>useCache</code></td>
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
<td class="type">
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
<span class="param-type">Boolean</span>
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
</td>
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
<td class="attributes">
|
|
357
|
+
|
|
358
|
+
<optional><br>
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
</td>
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
<td class="default">
|
|
369
|
+
|
|
370
|
+
<code>false</code>
|
|
371
|
+
|
|
372
|
+
</td>
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
<td class="description last"><p>輸入是否使用select快取,適用於單程序操作,預設false</p></td>
|
|
376
|
+
</tr>
|
|
377
|
+
|
|
378
|
+
|
|
339
379
|
</tbody>
|
|
340
380
|
</table>
|
|
341
381
|
|
|
@@ -429,7 +469,7 @@
|
|
|
429
469
|
|
|
430
470
|
<dt class="tag-source">Source:</dt>
|
|
431
471
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
432
|
-
<a href="WOrmLowdb.mjs.html">WOrmLowdb.mjs</a>, <a href="WOrmLowdb.mjs.html#
|
|
472
|
+
<a href="WOrmLowdb.mjs.html">WOrmLowdb.mjs</a>, <a href="WOrmLowdb.mjs.html#line635">line 635</a>
|
|
433
473
|
</li></ul></dd>
|
|
434
474
|
|
|
435
475
|
|
|
@@ -607,7 +647,7 @@
|
|
|
607
647
|
|
|
608
648
|
<dt class="tag-source">Source:</dt>
|
|
609
649
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
610
|
-
<a href="WOrmLowdb.mjs.html">WOrmLowdb.mjs</a>, <a href="WOrmLowdb.mjs.html#
|
|
650
|
+
<a href="WOrmLowdb.mjs.html">WOrmLowdb.mjs</a>, <a href="WOrmLowdb.mjs.html#line482">line 482</a>
|
|
611
651
|
</li></ul></dd>
|
|
612
652
|
|
|
613
653
|
|
|
@@ -768,7 +808,7 @@
|
|
|
768
808
|
|
|
769
809
|
<dt class="tag-source">Source:</dt>
|
|
770
810
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
771
|
-
<a href="WOrmLowdb.mjs.html">WOrmLowdb.mjs</a>, <a href="WOrmLowdb.mjs.html#
|
|
811
|
+
<a href="WOrmLowdb.mjs.html">WOrmLowdb.mjs</a>, <a href="WOrmLowdb.mjs.html#line185">line 185</a>
|
|
772
812
|
</li></ul></dd>
|
|
773
813
|
|
|
774
814
|
|
|
@@ -929,7 +969,7 @@
|
|
|
929
969
|
|
|
930
970
|
<dt class="tag-source">Source:</dt>
|
|
931
971
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
932
|
-
<a href="WOrmLowdb.mjs.html">WOrmLowdb.mjs</a>, <a href="WOrmLowdb.mjs.html#
|
|
972
|
+
<a href="WOrmLowdb.mjs.html">WOrmLowdb.mjs</a>, <a href="WOrmLowdb.mjs.html#line314">line 314</a>
|
|
933
973
|
</li></ul></dd>
|
|
934
974
|
|
|
935
975
|
|
|
@@ -1216,7 +1256,7 @@
|
|
|
1216
1256
|
|
|
1217
1257
|
<dt class="tag-source">Source:</dt>
|
|
1218
1258
|
<dd class="tag-source"><ul class="dummy"><li>
|
|
1219
|
-
<a href="WOrmLowdb.mjs.html">WOrmLowdb.mjs</a>, <a href="WOrmLowdb.mjs.html#
|
|
1259
|
+
<a href="WOrmLowdb.mjs.html">WOrmLowdb.mjs</a>, <a href="WOrmLowdb.mjs.html#line128">line 128</a>
|
|
1220
1260
|
</li></ul></dd>
|
|
1221
1261
|
|
|
1222
1262
|
|
|
@@ -1393,7 +1433,7 @@
|
|
|
1393
1433
|
<br class="clear">
|
|
1394
1434
|
|
|
1395
1435
|
<footer>
|
|
1396
|
-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on
|
|
1436
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Fri May 15 2026 13:15:06 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
1397
1437
|
</footer>
|
|
1398
1438
|
|
|
1399
1439
|
<script>prettyPrint();</script>
|
package/docs/WOrmLowdb.mjs.html
CHANGED
|
@@ -60,6 +60,7 @@ import cloneDeep from 'lodash-es/cloneDeep.js'
|
|
|
60
60
|
import genID from 'wsemi/src/genID.mjs'
|
|
61
61
|
import pmSeries from 'wsemi/src/pmSeries.mjs'
|
|
62
62
|
import isarr from 'wsemi/src/isarr.mjs'
|
|
63
|
+
import isbol from 'wsemi/src/isbol.mjs'
|
|
63
64
|
import isestr from 'wsemi/src/isestr.mjs'
|
|
64
65
|
import iseobj from 'wsemi/src/iseobj.mjs'
|
|
65
66
|
import isearr from 'wsemi/src/isearr.mjs'
|
|
@@ -80,6 +81,7 @@ let kpGlock = {}
|
|
|
80
81
|
* @param {String} [opt.url='./db.json'] 輸入資料庫位置字串,預設'./db.json'
|
|
81
82
|
* @param {String} [opt.db='worm'] 輸入使用資料庫名稱字串,預設'worm'
|
|
82
83
|
* @param {String} [opt.cl='test'] 輸入使用資料表名稱字串,預設'test'
|
|
84
|
+
* @param {Boolean} [opt.useCache=false] 輸入是否使用select快取,適用於單程序操作,預設false
|
|
83
85
|
* @returns {Object} 回傳操作資料庫物件,各事件功能詳見說明
|
|
84
86
|
*/
|
|
85
87
|
function WOrmLowdb(opt = {}) {
|
|
@@ -105,6 +107,12 @@ function WOrmLowdb(opt = {}) {
|
|
|
105
107
|
cl = 'test'
|
|
106
108
|
}
|
|
107
109
|
|
|
110
|
+
//useCache
|
|
111
|
+
let useCache = get(opt, 'useCache')
|
|
112
|
+
if (!isbol(useCache)) {
|
|
113
|
+
useCache = false
|
|
114
|
+
}
|
|
115
|
+
|
|
108
116
|
//key
|
|
109
117
|
let key = `${db}:${cl}`
|
|
110
118
|
|
|
@@ -135,7 +143,7 @@ function WOrmLowdb(opt = {}) {
|
|
|
135
143
|
let getData = async() => {
|
|
136
144
|
|
|
137
145
|
//check
|
|
138
|
-
if (isarr(_cache)) {
|
|
146
|
+
if (useCache && isarr(_cache)) {
|
|
139
147
|
return cloneDeep(_cache) //與外部使用數據脫勾
|
|
140
148
|
}
|
|
141
149
|
|
|
@@ -147,10 +155,14 @@ function WOrmLowdb(opt = {}) {
|
|
|
147
155
|
lowdb.data[key] = []
|
|
148
156
|
}
|
|
149
157
|
|
|
150
|
-
//
|
|
151
|
-
|
|
158
|
+
//ltdt
|
|
159
|
+
let ltdt = cloneDeep(lowdb.data[key]) //數據為lowdb內部的記憶體狀態本體, 須cloneDeep與外部使用數據脫勾
|
|
152
160
|
|
|
153
|
-
|
|
161
|
+
if (useCache) {
|
|
162
|
+
_cache = ltdt
|
|
163
|
+
return cloneDeep(ltdt) //與外部使用數據脫勾
|
|
164
|
+
}
|
|
165
|
+
return ltdt
|
|
154
166
|
}
|
|
155
167
|
|
|
156
168
|
/**
|
|
@@ -401,8 +413,11 @@ function WOrmLowdb(opt = {}) {
|
|
|
401
413
|
//查找資料表內v.id
|
|
402
414
|
let r = get(kp, v.id, null)
|
|
403
415
|
|
|
416
|
+
//existed
|
|
417
|
+
let existed = iseobj(r)
|
|
418
|
+
|
|
404
419
|
//check
|
|
405
|
-
if (
|
|
420
|
+
if (existed) {
|
|
406
421
|
//已存在v.id
|
|
407
422
|
if (isEqual(v, r.v)) {
|
|
408
423
|
//內容相同不更新
|
|
@@ -417,6 +432,9 @@ function WOrmLowdb(opt = {}) {
|
|
|
417
432
|
b = true
|
|
418
433
|
}
|
|
419
434
|
}
|
|
435
|
+
else {
|
|
436
|
+
//內容不存在
|
|
437
|
+
}
|
|
420
438
|
|
|
421
439
|
//rest
|
|
422
440
|
if (iseobj(rest)) {
|
|
@@ -433,34 +451,28 @@ function WOrmLowdb(opt = {}) {
|
|
|
433
451
|
ok: 1,
|
|
434
452
|
}
|
|
435
453
|
}
|
|
454
|
+
//rest.n === 0:
|
|
455
|
+
// 內容相同不更新, 不須update
|
|
456
|
+
// 內容不存在, 若autoInsert則須insert
|
|
436
457
|
|
|
437
|
-
//autoInsert
|
|
438
|
-
if (autoInsert && rest.n === 0) {
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
n: 1,
|
|
456
|
-
nInserted: 0,
|
|
457
|
-
ok: 1,
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
return rest
|
|
463
|
-
})
|
|
458
|
+
//autoInsert
|
|
459
|
+
if (autoInsert && rest.n === 0 && !existed) {
|
|
460
|
+
let k = size(lowdb.data[key])
|
|
461
|
+
lowdb.data[key].push(v)
|
|
462
|
+
kp[v.id] = {
|
|
463
|
+
k,
|
|
464
|
+
v: lowdb.data[key][k],
|
|
465
|
+
}
|
|
466
|
+
b = true
|
|
467
|
+
rest = {
|
|
468
|
+
n: 1,
|
|
469
|
+
nInserted: 1,
|
|
470
|
+
ok: 1,
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
return rest
|
|
475
|
+
})
|
|
464
476
|
|
|
465
477
|
//write
|
|
466
478
|
if (b) {
|
|
@@ -824,7 +836,7 @@ export default WOrmLowdb
|
|
|
824
836
|
<br class="clear">
|
|
825
837
|
|
|
826
838
|
<footer>
|
|
827
|
-
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on
|
|
839
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Fri May 15 2026 13:15:06 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
|
|
828
840
|
</footer>
|
|
829
841
|
|
|
830
842
|
<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.5</a> on
|
|
74
|
+
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.5</a> on Fri May 15 2026 13:15:06 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
|
@@ -44,6 +44,14 @@ let rsm = [
|
|
|
44
44
|
},
|
|
45
45
|
]
|
|
46
46
|
|
|
47
|
+
let rsa = [
|
|
48
|
+
{
|
|
49
|
+
id: 'id-rosemary',
|
|
50
|
+
name: 'rosemary',
|
|
51
|
+
value: 654.321,
|
|
52
|
+
},
|
|
53
|
+
]
|
|
54
|
+
|
|
47
55
|
async function test() {
|
|
48
56
|
|
|
49
57
|
//wo
|
|
@@ -105,6 +113,15 @@ async function test() {
|
|
|
105
113
|
// let sr = await wo.select({ name: { $regex: 'PeT', $options: '$i' } })
|
|
106
114
|
// console.log('selectReg', sr)
|
|
107
115
|
|
|
116
|
+
//save
|
|
117
|
+
await wo.save(rsa, { autoInsert: true })
|
|
118
|
+
.then(function(msg) {
|
|
119
|
+
console.log('save then', msg)
|
|
120
|
+
})
|
|
121
|
+
.catch(function(msg) {
|
|
122
|
+
console.log('save catch', msg)
|
|
123
|
+
})
|
|
124
|
+
|
|
108
125
|
//del
|
|
109
126
|
let d = ss.filter(function(v) {
|
|
110
127
|
return v.name === 'kettle'
|
|
@@ -159,6 +176,8 @@ test()
|
|
|
159
176
|
// value: 456
|
|
160
177
|
// }
|
|
161
178
|
// ]
|
|
179
|
+
// change save
|
|
180
|
+
// save then [ { n: 1, nModified: 1, ok: 1 } ]
|
|
162
181
|
// change del
|
|
163
182
|
// del then [ { n: 1, nDeleted: 1, ok: 1 } ]
|
|
164
183
|
|
package/package.json
CHANGED
package/src/WOrmLowdb.mjs
CHANGED
|
@@ -13,6 +13,7 @@ import cloneDeep from 'lodash-es/cloneDeep.js'
|
|
|
13
13
|
import genID from 'wsemi/src/genID.mjs'
|
|
14
14
|
import pmSeries from 'wsemi/src/pmSeries.mjs'
|
|
15
15
|
import isarr from 'wsemi/src/isarr.mjs'
|
|
16
|
+
import isbol from 'wsemi/src/isbol.mjs'
|
|
16
17
|
import isestr from 'wsemi/src/isestr.mjs'
|
|
17
18
|
import iseobj from 'wsemi/src/iseobj.mjs'
|
|
18
19
|
import isearr from 'wsemi/src/isearr.mjs'
|
|
@@ -33,6 +34,7 @@ let kpGlock = {}
|
|
|
33
34
|
* @param {String} [opt.url='./db.json'] 輸入資料庫位置字串,預設'./db.json'
|
|
34
35
|
* @param {String} [opt.db='worm'] 輸入使用資料庫名稱字串,預設'worm'
|
|
35
36
|
* @param {String} [opt.cl='test'] 輸入使用資料表名稱字串,預設'test'
|
|
37
|
+
* @param {Boolean} [opt.useCache=false] 輸入是否使用select快取,適用於單程序操作,預設false
|
|
36
38
|
* @returns {Object} 回傳操作資料庫物件,各事件功能詳見說明
|
|
37
39
|
*/
|
|
38
40
|
function WOrmLowdb(opt = {}) {
|
|
@@ -58,6 +60,12 @@ function WOrmLowdb(opt = {}) {
|
|
|
58
60
|
cl = 'test'
|
|
59
61
|
}
|
|
60
62
|
|
|
63
|
+
//useCache
|
|
64
|
+
let useCache = get(opt, 'useCache')
|
|
65
|
+
if (!isbol(useCache)) {
|
|
66
|
+
useCache = false
|
|
67
|
+
}
|
|
68
|
+
|
|
61
69
|
//key
|
|
62
70
|
let key = `${db}:${cl}`
|
|
63
71
|
|
|
@@ -88,7 +96,7 @@ function WOrmLowdb(opt = {}) {
|
|
|
88
96
|
let getData = async() => {
|
|
89
97
|
|
|
90
98
|
//check
|
|
91
|
-
if (isarr(_cache)) {
|
|
99
|
+
if (useCache && isarr(_cache)) {
|
|
92
100
|
return cloneDeep(_cache) //與外部使用數據脫勾
|
|
93
101
|
}
|
|
94
102
|
|
|
@@ -100,10 +108,14 @@ function WOrmLowdb(opt = {}) {
|
|
|
100
108
|
lowdb.data[key] = []
|
|
101
109
|
}
|
|
102
110
|
|
|
103
|
-
//
|
|
104
|
-
|
|
111
|
+
//ltdt
|
|
112
|
+
let ltdt = cloneDeep(lowdb.data[key]) //數據為lowdb內部的記憶體狀態本體, 須cloneDeep與外部使用數據脫勾
|
|
105
113
|
|
|
106
|
-
|
|
114
|
+
if (useCache) {
|
|
115
|
+
_cache = ltdt
|
|
116
|
+
return cloneDeep(ltdt) //與外部使用數據脫勾
|
|
117
|
+
}
|
|
118
|
+
return ltdt
|
|
107
119
|
}
|
|
108
120
|
|
|
109
121
|
/**
|
|
@@ -354,8 +366,11 @@ function WOrmLowdb(opt = {}) {
|
|
|
354
366
|
//查找資料表內v.id
|
|
355
367
|
let r = get(kp, v.id, null)
|
|
356
368
|
|
|
369
|
+
//existed
|
|
370
|
+
let existed = iseobj(r)
|
|
371
|
+
|
|
357
372
|
//check
|
|
358
|
-
if (
|
|
373
|
+
if (existed) {
|
|
359
374
|
//已存在v.id
|
|
360
375
|
if (isEqual(v, r.v)) {
|
|
361
376
|
//內容相同不更新
|
|
@@ -370,6 +385,9 @@ function WOrmLowdb(opt = {}) {
|
|
|
370
385
|
b = true
|
|
371
386
|
}
|
|
372
387
|
}
|
|
388
|
+
else {
|
|
389
|
+
//內容不存在
|
|
390
|
+
}
|
|
373
391
|
|
|
374
392
|
//rest
|
|
375
393
|
if (iseobj(rest)) {
|
|
@@ -386,34 +404,28 @@ function WOrmLowdb(opt = {}) {
|
|
|
386
404
|
ok: 1,
|
|
387
405
|
}
|
|
388
406
|
}
|
|
407
|
+
//rest.n === 0:
|
|
408
|
+
// 內容相同不更新, 不須update
|
|
409
|
+
// 內容不存在, 若autoInsert則須insert
|
|
389
410
|
|
|
390
|
-
//autoInsert
|
|
391
|
-
if (autoInsert && rest.n === 0) {
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
n: 1,
|
|
409
|
-
nInserted: 0,
|
|
410
|
-
ok: 1,
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
return rest
|
|
416
|
-
})
|
|
411
|
+
//autoInsert
|
|
412
|
+
if (autoInsert && rest.n === 0 && !existed) {
|
|
413
|
+
let k = size(lowdb.data[key])
|
|
414
|
+
lowdb.data[key].push(v)
|
|
415
|
+
kp[v.id] = {
|
|
416
|
+
k,
|
|
417
|
+
v: lowdb.data[key][k],
|
|
418
|
+
}
|
|
419
|
+
b = true
|
|
420
|
+
rest = {
|
|
421
|
+
n: 1,
|
|
422
|
+
nInserted: 1,
|
|
423
|
+
ok: 1,
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
return rest
|
|
428
|
+
})
|
|
417
429
|
|
|
418
430
|
//write
|
|
419
431
|
if (b) {
|