w-orm-lmdb 1.0.5 → 1.0.7

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.
@@ -0,0 +1,168 @@
1
+ import assert from 'assert'
2
+ import _ from 'lodash-es'
3
+ import w from 'wsemi'
4
+ import WOrm from '../src/WOrmLmdb.mjs'
5
+
6
+
7
+ describe('cache', function() {
8
+ let rt = null
9
+ let vans = {}
10
+ let vget = {}
11
+
12
+ before(async function () {
13
+
14
+ w.fsDeleteFolder('./_db_cache')
15
+
16
+ let opt = {
17
+ url: './_db_cache',
18
+ db: 'worm',
19
+ cl: 'users',
20
+ useCache: true,
21
+ }
22
+
23
+ let rs = [
24
+ {
25
+ id: 'id-peter',
26
+ name: 'peter',
27
+ value: 123,
28
+ },
29
+ {
30
+ id: 'id-rosemary',
31
+ name: 'rosemary',
32
+ value: 123.456,
33
+ },
34
+ {
35
+ id: 'id-kettle',
36
+ name: 'kettle',
37
+ value: 456,
38
+ },
39
+ ]
40
+
41
+ let rsm = [
42
+ {
43
+ id: 'id-rosemary',
44
+ name: 'rosemary(modify)',
45
+ value: 654.321,
46
+ },
47
+ ]
48
+
49
+ //wo
50
+ let wo = WOrm(opt)
51
+
52
+ //on
53
+ wo.on('change', function(mode, data, res) {
54
+ // console.log('change', mode)
55
+ })
56
+
57
+ //delAll
58
+ await wo.delAll()
59
+
60
+ //insert
61
+ await wo.insert(rs)
62
+
63
+ //select all (1st time, 從DB讀取並填入快取)
64
+ rt = null
65
+ // vans[1] = [
66
+ // { id: 'id-kettle', name: 'kettle', value: 456 },
67
+ // { id: 'id-peter', name: 'peter', value: 123 },
68
+ // { id: 'id-rosemary', name: 'rosemary', value: 123.456 },
69
+ // ]
70
+ await wo.select()
71
+ .then(function(msg) {
72
+ // console.log('select 1st then', msg)
73
+ msg = _.sortBy(msg, 'name')
74
+ rt = msg
75
+ })
76
+ .catch(function(msg) {
77
+ // console.log('select 1st catch', msg)
78
+ rt = msg.toString()
79
+ })
80
+ vget[1] = rt
81
+
82
+ //select all (2nd time, 命中快取, 內容須與第一次相同)
83
+ rt = null
84
+ // vans[2] = [
85
+ // { id: 'id-kettle', name: 'kettle', value: 456 },
86
+ // { id: 'id-peter', name: 'peter', value: 123 },
87
+ // { id: 'id-rosemary', name: 'rosemary', value: 123.456 },
88
+ // ]
89
+ await wo.select()
90
+ .then(function(msg) {
91
+ // console.log('select 2nd then', msg)
92
+ msg = _.sortBy(msg, 'name')
93
+ rt = msg
94
+ })
95
+ .catch(function(msg) {
96
+ // console.log('select 2nd catch', msg)
97
+ rt = msg.toString()
98
+ })
99
+ vget[2] = rt
100
+
101
+ //save (更新rosemary, 觸發快取重設)
102
+ rt = null
103
+ // vans[3] = [{ n: 1, nModified: 1, ok: 1 }]
104
+ await wo.save(rsm, { autoInsert: false })
105
+ .then(function(msg) {
106
+ // console.log('save then', msg)
107
+ rt = msg
108
+ })
109
+ .catch(function(msg) {
110
+ // console.log('save catch', msg)
111
+ rt = msg.toString()
112
+ })
113
+ vget[3] = rt
114
+
115
+ //select all (3rd time, 快取已重設, 從DB重新讀取, 須反映rosemary更新)
116
+ rt = null
117
+ // vans[4] = [
118
+ // { id: 'id-kettle', name: 'kettle', value: 456 },
119
+ // { id: 'id-peter', name: 'peter', value: 123 },
120
+ // { id: 'id-rosemary', name: 'rosemary(modify)', value: 654.321 },
121
+ // ]
122
+ await wo.select()
123
+ .then(function(msg) {
124
+ // console.log('select 3rd then', msg)
125
+ msg = _.sortBy(msg, 'name')
126
+ rt = msg
127
+ })
128
+ .catch(function(msg) {
129
+ // console.log('select 3rd catch', msg)
130
+ rt = msg.toString()
131
+ })
132
+ vget[4] = rt
133
+
134
+ })
135
+
136
+ vans[1] = [
137
+ { id: 'id-kettle', name: 'kettle', value: 456 },
138
+ { id: 'id-peter', name: 'peter', value: 123 },
139
+ { id: 'id-rosemary', name: 'rosemary', value: 123.456 },
140
+ ]
141
+ it(`should get ${JSON.stringify(vans[1])} for select all (1st, fill cache)`, async function() {
142
+ assert.strict.deepStrictEqual(vget[1], vans[1])
143
+ })
144
+
145
+ vans[2] = [
146
+ { id: 'id-kettle', name: 'kettle', value: 456 },
147
+ { id: 'id-peter', name: 'peter', value: 123 },
148
+ { id: 'id-rosemary', name: 'rosemary', value: 123.456 },
149
+ ]
150
+ it(`should get ${JSON.stringify(vans[2])} for select all (2nd, cache hit)`, async function() {
151
+ assert.strict.deepStrictEqual(vget[2], vans[2])
152
+ })
153
+
154
+ vans[3] = [{ n: 1, nModified: 1, ok: 1 }]
155
+ it(`should get ${JSON.stringify(vans[3])} for save (invalidate cache)`, async function() {
156
+ assert.strict.deepStrictEqual(vget[3], vans[3])
157
+ })
158
+
159
+ vans[4] = [
160
+ { id: 'id-kettle', name: 'kettle', value: 456 },
161
+ { id: 'id-peter', name: 'peter', value: 123 },
162
+ { id: 'id-rosemary', name: 'rosemary(modify)', value: 654.321 },
163
+ ]
164
+ it(`should get ${JSON.stringify(vans[4])} for select all (3rd, reload after save)`, async function() {
165
+ assert.strict.deepStrictEqual(vget[4], vans[4])
166
+ })
167
+
168
+ })