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.
@@ -1,470 +1,681 @@
1
- import assert from 'assert'
2
- import WOrm from '../src/WOrmPostgresql.mjs'
3
-
4
-
5
- function isWindows() {
6
- return process.platform === 'win32'
7
- }
8
-
9
-
10
- if (isWindows()) {
11
- describe('basic', function() {
12
- let rt = null
13
- let vans = {}
14
- let vget = {}
15
-
16
- before(async function () {
17
-
18
- let opt = {
19
- url: 'postgresql://username:password@127.0.0.1:5432',
20
- db: 'worm',
21
- cl: 'users',
22
- }
23
-
24
- let rs = [
25
- {
26
- time: '2025-01-01T00:00:00Z',
27
- name: 'peter',
28
- value: 123,
29
- },
30
- {
31
- time: '2025-01-01T00:01:00Z',
32
- name: 'rosemary',
33
- value: 123.456,
34
- },
35
- {
36
- time: '2025-01-01T00:02:00Z',
37
- name: 'kettle',
38
- value: 456,
39
- },
40
- {
41
- time: '2025-01-01T00:03:00Z',
42
- name: 'peter',
43
- value: 200,
44
- },
45
- {
46
- time: '2025-01-01T00:04:00Z',
47
- name: 'rosemary',
48
- value: 123.1236,
49
- },
50
- {
51
- time: '2025-01-01T00:05:00Z',
52
- name: 'kettle',
53
- value: 488,
54
- },
55
- {
56
- time: '2025-01-01T00:06:00Z',
57
- name: 'peter',
58
- value: 125,
59
- },
60
- {
61
- time: '2025-01-01T00:07:00Z',
62
- name: 'rosemary',
63
- value: 124.76,
64
- },
65
- {
66
- time: '2025-01-01T00:08:00Z',
67
- name: 'kettle',
68
- value: 524,
69
- },
70
- {
71
- time: '2025-01-01T00:09:00Z',
72
- name: 'peter',
73
- value: 127,
74
- },
75
- {
76
- time: '2025-01-01T00:10:00Z',
77
- name: 'rosemary',
78
- value: 113.98,
79
- },
80
- {
81
- time: '2025-01-01T00:11:00Z',
82
- name: 'kettle',
83
- value: 447,
84
- },
85
- {
86
- time: '2025-01-01T00:12:00Z',
87
- name: 'peter',
88
- value: 131,
89
- },
90
- ]
91
-
92
- let rsm = [
93
- { //相同
94
- time: '2025-01-01T00:09:00Z',
95
- name: 'peter',
96
- value: 127,
97
- },
98
- { //name變更
99
- time: '2025-01-01T00:10:00Z',
100
- name: 'rosemary(modify)',
101
- value: 113.98,
102
- },
103
- { //name變更
104
- time: '2025-01-01T00:11:00Z',
105
- name: 'kettle(modify)',
106
- value: 447,
107
- },
108
- { //無name且value變更
109
- time: '2025-01-01T00:12:00Z',
110
- // name: 'peter',
111
- value: 99,
112
- },
113
- { //無time須新增且新增數據無value
114
- time: '2025-01-01T00:13:00Z',
115
- name: 'sandler',
116
- },
117
- ]
118
-
119
- let rsa = [
120
- {
121
- time: '2025-01-01T00:13:00Z',
122
- name: 'sandler',
123
- },
124
- ]
125
-
126
- //wo
127
- let wo = WOrm(opt)
128
-
129
- //on
130
- wo.on('change', function(mode, data, res) {
131
- // console.log('change', mode)
132
- })
133
-
134
- await wo.createTable(opt.cl, 'time', {
135
- time: '2000-01-01T00:00:00Z', //time
136
- name: 'abc', //string
137
- value: 0.1, //float
138
- })
139
- .then(function(msg) {
140
- // console.log('createTable then', msg)
141
- })
142
- .catch(function(msg) {
143
- // console.log('createTable catch', msg)
144
- })
145
-
146
- //delAll
147
- rt = null
148
- vans[1] = { ok: 1 }
149
- await wo.delAll()
150
- .then(function(msg) {
151
- // console.log('delAll then', msg)
152
- //考慮有不同初始狀態, 僅比對ok欄位
153
- rt = {
154
- ok: msg.ok,
155
- }
156
- })
157
- .catch(function(msg) {
158
- // console.log('delAll catch', msg)
159
- rt = msg.toString()
160
- })
161
- vget[1] = rt
162
-
163
- //insert
164
- rt = null
165
- vans[2] = { n: 13, nInserted: 13, ok: 1 }
166
- await wo.insert(rs)
167
- .then(function(msg) {
168
- // console.log('insert then', msg)
169
- rt = msg
170
- })
171
- .catch(function(msg) {
172
- // console.log('insert catch', msg)
173
- rt = msg.toString()
174
- })
175
- vget[2] = rt
176
-
177
- //save
178
- rt = null
179
- vans[3] = [
180
- { n: 1, nModified: 0, ok: 1 },
181
- { n: 1, nModified: 1, ok: 1 },
182
- { n: 1, nModified: 1, ok: 1 },
183
- { n: 1, nModified: 1, ok: 1 },
184
- { n: 0, nModified: 0, ok: 1 }
185
- ]
186
- await wo.save(rsm, { autoInsert: false })
187
- .then(function(msg) {
188
- // console.log('save then', msg)
189
- rt = msg
190
- })
191
- .catch(function(msg) {
192
- // console.log('save catch', msg)
193
- rt = msg.toString()
194
- })
195
- vget[3] = rt
196
-
197
- //select all
198
- rt = null
199
- vans[4] = [
200
- { time: new Date('2025-01-01T00:00:00.000Z'), name: 'peter', value: 123 },
201
- { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
202
- { time: new Date('2025-01-01T00:02:00.000Z'), name: 'kettle', value: 456 },
203
- { time: new Date('2025-01-01T00:03:00.000Z'), name: 'peter', value: 200 },
204
- { time: new Date('2025-01-01T00:04:00.000Z'), name: 'rosemary', value: 123.1236 },
205
- { time: new Date('2025-01-01T00:05:00.000Z'), name: 'kettle', value: 488 },
206
- { time: new Date('2025-01-01T00:06:00.000Z'), name: 'peter', value: 125 },
207
- { time: new Date('2025-01-01T00:07:00.000Z'), name: 'rosemary', value: 124.76 },
208
- { time: new Date('2025-01-01T00:08:00.000Z'), name: 'kettle', value: 524 },
209
- { time: new Date('2025-01-01T00:09:00.000Z'), name: 'peter', value: 127 },
210
- { time: new Date('2025-01-01T00:10:00.000Z'), name: 'rosemary(modify)', value: 113.98 },
211
- { time: new Date('2025-01-01T00:11:00.000Z'), name: 'kettle(modify)', value: 447 },
212
- { time: new Date('2025-01-01T00:12:00.000Z'), name: 'peter', value: 99 }
213
- ]
214
- await wo.select()
215
- .then(function(msg) {
216
- // console.log('select all then', msg)
217
- rt = msg
218
- })
219
- .catch(function(msg) {
220
- // console.log('select all catch', msg)
221
- rt = msg.toString()
222
- })
223
- vget[4] = rt
224
-
225
- //select all by cache
226
- rt = null
227
- vans[5] = vans[4]
228
- await wo.select()
229
- .then(function(msg) {
230
- // console.log('select all by cache then', msg)
231
- rt = msg
232
- })
233
- .catch(function(msg) {
234
- // console.log('select all by cache catch', msg)
235
- rt = msg.toString()
236
- })
237
- vget[5] = rt
238
-
239
- //select by name
240
- rt = null
241
- vans[6] = [
242
- { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
243
- { time: new Date('2025-01-01T00:04:00.000Z'), name: 'rosemary', value: 123.1236 },
244
- { time: new Date('2025-01-01T00:07:00.000Z'), name: 'rosemary', value: 124.76 }
245
- ]
246
- await wo.select({ name: 'rosemary' })
247
- .then(function(msg) {
248
- // console.log('select all then', msg)
249
- rt = msg
250
- })
251
- .catch(function(msg) {
252
- // console.log('select all catch', msg)
253
- rt = msg.toString()
254
- })
255
- vget[6] = rt
256
-
257
- //select by $and, $gt, $lt
258
- rt = null
259
- vans[7] = [
260
- { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
261
- { time: new Date('2025-01-01T00:04:00.000Z'), name: 'rosemary', value: 123.1236 },
262
- { time: new Date('2025-01-01T00:06:00.000Z'), name: 'peter', value: 125 },
263
- { time: new Date('2025-01-01T00:07:00.000Z'), name: 'rosemary', value: 124.76 },
264
- { time: new Date('2025-01-01T00:09:00.000Z'), name: 'peter', value: 127 }
265
- ]
266
- await wo.select({ '$and': [{ value: { '$gt': 123 } }, { value: { '$lt': 200 } }] })
267
- .then(function(msg) {
268
- // console.log('select all then', msg)
269
- rt = msg
270
- })
271
- .catch(function(msg) {
272
- // console.log('select all catch', msg)
273
- rt = msg.toString()
274
- })
275
- vget[7] = rt
276
-
277
- //select by $or, $gte, $lte
278
- rt = null
279
- vans[8] = [
280
- { time: new Date('2025-01-01T00:02:00.000Z'), name: 'kettle', value: 456 },
281
- { time: new Date('2025-01-01T00:03:00.000Z'), name: 'peter', value: 200 },
282
- { time: new Date('2025-01-01T00:05:00.000Z'), name: 'kettle', value: 488 },
283
- { time: new Date('2025-01-01T00:08:00.000Z'), name: 'kettle', value: 524 },
284
- { time: new Date('2025-01-01T00:11:00.000Z'), name: 'kettle(modify)', value: 447 }
285
- ]
286
- await wo.select({ '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 200 } }] })
287
- .then(function(msg) {
288
- // console.log('select all then', msg)
289
- rt = msg
290
- })
291
- .catch(function(msg) {
292
- // console.log('select all catch', msg)
293
- rt = msg.toString()
294
- })
295
- vget[8] = rt
296
-
297
- //select by $or, $and, $ne, $in, $nin
298
- rt = null
299
- vans[9] = [
300
- { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
301
- { time: new Date('2025-01-01T00:02:00.000Z'), name: 'kettle', value: 456 },
302
- { time: new Date('2025-01-01T00:05:00.000Z'), name: 'kettle', value: 488 },
303
- { time: new Date('2025-01-01T00:08:00.000Z'), name: 'kettle', value: 524 },
304
- { time: new Date('2025-01-01T00:11:00.000Z'), name: 'kettle(modify)', value: 447 }
305
- ]
306
- await wo.select({ '$or': [{ '$and': [{ value: { '$ne': 123 } }, { value: { '$in': [123, 321, 123.456, 456] } }, { value: { '$nin': [456, 654] } }] }, { '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 400 } }] }] })
307
- .then(function(msg) {
308
- // console.log('select all then', msg)
309
- rt = msg
310
- })
311
- .catch(function(msg) {
312
- // console.log('select all catch', msg)
313
- rt = msg.toString()
314
- })
315
- vget[9] = rt
316
-
317
- // //select by regex
318
- // rt = null
319
- // vans[14] = []
320
- // let sr = await wo.select({ name: { $regex: 'PeT', $options: '$i' } })
321
- // .then(function(msg) {
322
- // // console.log('select all then', msg)
323
- // rt = msg
324
- // })
325
- // .catch(function(msg) {
326
- // // console.log('select all catch', msg)
327
- // rt = msg.toString()
328
- // })
329
- // vget[14] = rt
330
-
331
- //save
332
- rt = null
333
- vans[10] = [{ n: 1, nInserted: 1, ok: 1 }]
334
- await wo.save(rsa, { autoInsert: true })
335
- .then(function(msg) {
336
- // console.log('save then', msg)
337
- rt = msg
338
- })
339
- .catch(function(msg) {
340
- // console.log('save catch', msg)
341
- rt = msg.toString()
342
- })
343
- vget[10] = rt
344
-
345
- //del
346
- rt = null
347
- vans[11] = [{ n: 1, nDeleted: 0, ok: 1 }]
348
- let d = {
349
- time: '2024-01-01T00:00:00Z',
350
- }
351
- await wo.del(d)
352
- .then(function(msg) {
353
- // console.log('del then', msg)
354
- rt = msg
355
- })
356
- .catch(function(msg) {
357
- // console.log('del catch', msg)
358
- rt = msg.toString()
359
- })
360
- vget[11] = rt
361
-
362
- //del
363
- rt = null
364
- vans[12] = [
365
- { n: 1, nDeleted: 1, ok: 1 },
366
- { n: 1, nDeleted: 1, ok: 1 },
367
- { n: 1, nDeleted: 1, ok: 1 },
368
- { n: 1, nDeleted: 1, ok: 1 },
369
- { n: 1, nDeleted: 1, ok: 1 },
370
- { n: 1, nDeleted: 1, ok: 1 },
371
- { n: 1, nDeleted: 1, ok: 1 },
372
- { n: 1, nDeleted: 1, ok: 1 },
373
- { n: 1, nDeleted: 1, ok: 1 },
374
- { n: 1, nDeleted: 1, ok: 1 }
375
- ]
376
- let ss = await wo.select()
377
- let ds = ss.filter(function(v) {
378
- return v.name.indexOf('peter') >= 0 || v.name.indexOf('kettle') >= 0 || v.name.indexOf('sandler') >= 0
379
- })
380
- await wo.del(ds)
381
- .then(function(msg) {
382
- // console.log('del then', msg)
383
- rt = msg
384
- })
385
- .catch(function(msg) {
386
- // console.log('del catch', msg)
387
- rt = msg.toString()
388
- })
389
- vget[12] = rt
390
-
391
- //select all final
392
- rt = null
393
- vans[13] = [
394
- { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
395
- { time: new Date('2025-01-01T00:04:00.000Z'), name: 'rosemary', value: 123.1236 },
396
- { time: new Date('2025-01-01T00:07:00.000Z'), name: 'rosemary', value: 124.76 },
397
- { time: new Date('2025-01-01T00:10:00.000Z'), name: 'rosemary(modify)', value: 113.98 }
398
- ]
399
- await wo.select()
400
- .then(function(msg) {
401
- // console.log('select all then', msg)
402
- rt = msg
403
- })
404
- .catch(function(msg) {
405
- // console.log('select all catch', msg)
406
- rt = msg.toString()
407
- })
408
- vget[13] = rt
409
-
410
- })
411
-
412
- it(`should get ${JSON.stringify(vans[1])} for delAll`, async function() {
413
- assert.strict.deepStrictEqual(vget[1], vans[1])
414
- })
415
-
416
- it(`should get ${JSON.stringify(vans[2])} for insert`, async function() {
417
- assert.strict.deepStrictEqual(vget[2], vans[2])
418
- })
419
-
420
- it(`should get ${JSON.stringify(vans[3])} for save(autoInsert=false)`, async function() {
421
- assert.strict.deepStrictEqual(vget[3], vans[3])
422
- })
1
+ import assert from 'assert'
2
+ import WOrm from '../src/WOrmPostgresql.mjs'
423
3
 
424
- it(`should get ${JSON.stringify(vans[4])} for select all`, async function() {
425
- assert.strict.deepStrictEqual(vget[4], vans[4])
426
- })
427
4
 
428
- it(`should get ${JSON.stringify(vans[5])} for select all by cache`, async function() {
429
- assert.strict.deepStrictEqual(vget[5], vans[5])
430
- })
5
+ describe('basic', function() {
6
+ let rt = null
7
+ let vans = {}
8
+ let vget = {}
431
9
 
432
- it(`should get ${JSON.stringify(vans[6])} for select by name`, async function() {
433
- assert.strict.deepStrictEqual(vget[6], vans[6])
434
- })
10
+ before(async function () {
435
11
 
436
- it(`should get ${JSON.stringify(vans[7])} for select by $and, $gt, $lt`, async function() {
437
- assert.strict.deepStrictEqual(vget[7], vans[7])
438
- })
12
+ let opt = {
13
+ url: 'postgresql://username:password@127.0.0.1:5432',
14
+ db: 'worm',
15
+ cl: 'users',
16
+ }
439
17
 
440
- it(`should get ${JSON.stringify(vans[8])} for select by $or, $gte, $lte`, async function() {
441
- assert.strict.deepStrictEqual(vget[8], vans[8])
442
- })
18
+ //probe, 偵測postgres是否可用, 不可用則skip全部測試
19
+ //pg預設無connect timeout, 連不到時會等到OS socket timeout (~75s), 故用Promise.race包3s timeout
20
+ try {
21
+ let probe = WOrm(opt)
22
+ await Promise.race([
23
+ probe.select(),
24
+ new Promise((_resolve, reject) => setTimeout(() => reject(new Error('probe timeout')), 3000)),
25
+ ])
26
+ }
27
+ catch (err) {
28
+ let msg = err && err.message ? err.message : err
29
+ console.log(`PostgreSQL unavailable, skip tests: ${msg}`)
30
+ this.skip()
31
+ return
32
+ }
443
33
 
444
- it(`should get ${JSON.stringify(vans[9])} for select by $or, $and, $ne, $in, $nin`, async function() {
445
- assert.strict.deepStrictEqual(vget[9], vans[9])
446
- })
34
+ let rs = [
35
+ {
36
+ time: '2025-01-01T00:00:00Z',
37
+ name: 'peter',
38
+ value: 123,
39
+ },
40
+ {
41
+ time: '2025-01-01T00:01:00Z',
42
+ name: 'rosemary',
43
+ value: 123.456,
44
+ },
45
+ {
46
+ time: '2025-01-01T00:02:00Z',
47
+ name: 'kettle',
48
+ value: 456,
49
+ },
50
+ {
51
+ time: '2025-01-01T00:03:00Z',
52
+ name: 'peter',
53
+ value: 200,
54
+ },
55
+ {
56
+ time: '2025-01-01T00:04:00Z',
57
+ name: 'rosemary',
58
+ value: 123.1236,
59
+ },
60
+ {
61
+ time: '2025-01-01T00:05:00Z',
62
+ name: 'kettle',
63
+ value: 488,
64
+ },
65
+ {
66
+ time: '2025-01-01T00:06:00Z',
67
+ name: 'peter',
68
+ value: 125,
69
+ },
70
+ {
71
+ time: '2025-01-01T00:07:00Z',
72
+ name: 'rosemary',
73
+ value: 124.76,
74
+ },
75
+ {
76
+ time: '2025-01-01T00:08:00Z',
77
+ name: 'kettle',
78
+ value: 524,
79
+ },
80
+ {
81
+ time: '2025-01-01T00:09:00Z',
82
+ name: 'peter',
83
+ value: 127,
84
+ },
85
+ {
86
+ time: '2025-01-01T00:10:00Z',
87
+ name: 'rosemary',
88
+ value: 113.98,
89
+ },
90
+ {
91
+ time: '2025-01-01T00:11:00Z',
92
+ name: 'kettle',
93
+ value: 447,
94
+ },
95
+ {
96
+ time: '2025-01-01T00:12:00Z',
97
+ name: 'peter',
98
+ value: 131,
99
+ },
100
+ ]
447
101
 
448
- // it(`should get ${JSON.stringify(vans[14])} for select by regex`, async function() {
449
- // assert.strict.deepStrictEqual(vget[14], vans[14])
450
- // })
102
+ let rsm = [
103
+ { //相同
104
+ time: '2025-01-01T00:09:00Z',
105
+ name: 'peter',
106
+ value: 127,
107
+ },
108
+ { //name變更
109
+ time: '2025-01-01T00:10:00Z',
110
+ name: 'rosemary(modify)',
111
+ value: 113.98,
112
+ },
113
+ { //name變更
114
+ time: '2025-01-01T00:11:00Z',
115
+ name: 'kettle(modify)',
116
+ value: 447,
117
+ },
118
+ { //無name且value變更
119
+ time: '2025-01-01T00:12:00Z',
120
+ // name: 'peter',
121
+ value: 99,
122
+ },
123
+ { //無time須新增且新增數據無value
124
+ time: '2025-01-01T00:13:00Z',
125
+ name: 'sandler',
126
+ },
127
+ ]
451
128
 
452
- it(`should get ${JSON.stringify(vans[10])} for save(autoInsert=true)`, async function() {
453
- assert.strict.deepStrictEqual(vget[10], vans[10])
129
+ let rsa = [
130
+ {
131
+ time: '2025-01-01T00:13:00Z',
132
+ name: 'sandler',
133
+ },
134
+ ]
135
+
136
+ //wo
137
+ let wo = WOrm(opt)
138
+
139
+ //on
140
+ wo.on('change', function(mode, data, res) {
141
+ // console.log('change', mode)
454
142
  })
455
143
 
456
- it(`should get ${JSON.stringify(vans[11])} for del`, async function() {
457
- assert.strict.deepStrictEqual(vget[11], vans[11])
144
+ await wo.createTable(opt.cl, 'time', {
145
+ time: '2000-01-01T00:00:00Z', //time
146
+ name: 'abc', //string
147
+ value: 0.1, //float
458
148
  })
149
+ .then(function(msg) {
150
+ // console.log('createTable then', msg)
151
+ })
152
+ .catch(function(msg) {
153
+ // console.log('createTable catch', msg)
154
+ })
155
+
156
+ //delAll
157
+ rt = null
158
+ // vans[1] = { ok: 1 }
159
+ await wo.delAll()
160
+ .then(function(msg) {
161
+ // console.log('delAll then', msg)
162
+ //考慮有不同初始狀態, 僅比對ok欄位
163
+ rt = {
164
+ ok: msg.ok,
165
+ }
166
+ })
167
+ .catch(function(msg) {
168
+ // console.log('delAll catch', msg)
169
+ rt = msg.toString()
170
+ })
171
+ vget[1] = rt
172
+
173
+ //insert
174
+ rt = null
175
+ // vans[2] = { n: 13, nInserted: 13, ok: 1 }
176
+ await wo.insert(rs)
177
+ .then(function(msg) {
178
+ // console.log('insert then', msg)
179
+ rt = msg
180
+ })
181
+ .catch(function(msg) {
182
+ // console.log('insert catch', msg)
183
+ rt = msg.toString()
184
+ })
185
+ vget[2] = rt
186
+
187
+ //save
188
+ rt = null
189
+ // vans[3] = [
190
+ // { n: 1, nModified: 0, ok: 1 },
191
+ // { n: 1, nModified: 1, ok: 1 },
192
+ // { n: 1, nModified: 1, ok: 1 },
193
+ // { n: 1, nModified: 1, ok: 1 },
194
+ // { n: 0, nModified: 0, ok: 1 }
195
+ // ]
196
+ await wo.save(rsm, { autoInsert: false })
197
+ .then(function(msg) {
198
+ // console.log('save then', msg)
199
+ rt = msg
200
+ })
201
+ .catch(function(msg) {
202
+ // console.log('save catch', msg)
203
+ rt = msg.toString()
204
+ })
205
+ vget[3] = rt
206
+
207
+ //select all
208
+ rt = null
209
+ // vans[4] = [
210
+ // { time: new Date('2025-01-01T00:00:00.000Z'), name: 'peter', value: 123 },
211
+ // { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
212
+ // { time: new Date('2025-01-01T00:02:00.000Z'), name: 'kettle', value: 456 },
213
+ // { time: new Date('2025-01-01T00:03:00.000Z'), name: 'peter', value: 200 },
214
+ // { time: new Date('2025-01-01T00:04:00.000Z'), name: 'rosemary', value: 123.1236 },
215
+ // { time: new Date('2025-01-01T00:05:00.000Z'), name: 'kettle', value: 488 },
216
+ // { time: new Date('2025-01-01T00:06:00.000Z'), name: 'peter', value: 125 },
217
+ // { time: new Date('2025-01-01T00:07:00.000Z'), name: 'rosemary', value: 124.76 },
218
+ // { time: new Date('2025-01-01T00:08:00.000Z'), name: 'kettle', value: 524 },
219
+ // { time: new Date('2025-01-01T00:09:00.000Z'), name: 'peter', value: 127 },
220
+ // { time: new Date('2025-01-01T00:10:00.000Z'), name: 'rosemary(modify)', value: 113.98 },
221
+ // { time: new Date('2025-01-01T00:11:00.000Z'), name: 'kettle(modify)', value: 447 },
222
+ // { time: new Date('2025-01-01T00:12:00.000Z'), name: 'peter', value: 99 }
223
+ // ]
224
+ await wo.select()
225
+ .then(function(msg) {
226
+ // console.log('select all then', msg)
227
+ rt = msg
228
+ })
229
+ .catch(function(msg) {
230
+ // console.log('select all catch', msg)
231
+ rt = msg.toString()
232
+ })
233
+ vget[4] = rt
459
234
 
460
- it(`should get ${JSON.stringify(vans[12])} for del`, async function() {
461
- assert.strict.deepStrictEqual(vget[12], vans[12])
235
+ //select all by cache
236
+ rt = null
237
+ // vans[5] = vans[4]
238
+ await wo.select()
239
+ .then(function(msg) {
240
+ // console.log('select all by cache then', msg)
241
+ rt = msg
242
+ })
243
+ .catch(function(msg) {
244
+ // console.log('select all by cache catch', msg)
245
+ rt = msg.toString()
246
+ })
247
+ vget[5] = rt
248
+
249
+ //select by name
250
+ rt = null
251
+ // vans[6] = [
252
+ // { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
253
+ // { time: new Date('2025-01-01T00:04:00.000Z'), name: 'rosemary', value: 123.1236 },
254
+ // { time: new Date('2025-01-01T00:07:00.000Z'), name: 'rosemary', value: 124.76 }
255
+ // ]
256
+ await wo.select({ name: 'rosemary' })
257
+ .then(function(msg) {
258
+ // console.log('select all then', msg)
259
+ rt = msg
260
+ })
261
+ .catch(function(msg) {
262
+ // console.log('select all catch', msg)
263
+ rt = msg.toString()
264
+ })
265
+ vget[6] = rt
266
+
267
+ //select by $and, $gt, $lt
268
+ rt = null
269
+ // vans[7] = [
270
+ // { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
271
+ // { time: new Date('2025-01-01T00:04:00.000Z'), name: 'rosemary', value: 123.1236 },
272
+ // { time: new Date('2025-01-01T00:06:00.000Z'), name: 'peter', value: 125 },
273
+ // { time: new Date('2025-01-01T00:07:00.000Z'), name: 'rosemary', value: 124.76 },
274
+ // { time: new Date('2025-01-01T00:09:00.000Z'), name: 'peter', value: 127 }
275
+ // ]
276
+ await wo.select({ '$and': [{ value: { '$gt': 123 } }, { value: { '$lt': 200 } }] })
277
+ .then(function(msg) {
278
+ // console.log('select all then', msg)
279
+ rt = msg
280
+ })
281
+ .catch(function(msg) {
282
+ // console.log('select all catch', msg)
283
+ rt = msg.toString()
284
+ })
285
+ vget[7] = rt
286
+
287
+ //select by $or, $gte, $lte
288
+ rt = null
289
+ // vans[8] = [
290
+ // { time: new Date('2025-01-01T00:02:00.000Z'), name: 'kettle', value: 456 },
291
+ // { time: new Date('2025-01-01T00:03:00.000Z'), name: 'peter', value: 200 },
292
+ // { time: new Date('2025-01-01T00:05:00.000Z'), name: 'kettle', value: 488 },
293
+ // { time: new Date('2025-01-01T00:08:00.000Z'), name: 'kettle', value: 524 },
294
+ // { time: new Date('2025-01-01T00:11:00.000Z'), name: 'kettle(modify)', value: 447 }
295
+ // ]
296
+ await wo.select({ '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 200 } }] })
297
+ .then(function(msg) {
298
+ // console.log('select all then', msg)
299
+ rt = msg
300
+ })
301
+ .catch(function(msg) {
302
+ // console.log('select all catch', msg)
303
+ rt = msg.toString()
304
+ })
305
+ vget[8] = rt
306
+
307
+ //select by $or, $and, $ne, $in, $nin
308
+ rt = null
309
+ // vans[9] = [
310
+ // { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
311
+ // { time: new Date('2025-01-01T00:02:00.000Z'), name: 'kettle', value: 456 },
312
+ // { time: new Date('2025-01-01T00:05:00.000Z'), name: 'kettle', value: 488 },
313
+ // { time: new Date('2025-01-01T00:08:00.000Z'), name: 'kettle', value: 524 },
314
+ // { time: new Date('2025-01-01T00:11:00.000Z'), name: 'kettle(modify)', value: 447 }
315
+ // ]
316
+ await wo.select({ '$or': [{ '$and': [{ value: { '$ne': 123 } }, { value: { '$in': [123, 321, 123.456, 456] } }, { value: { '$nin': [456, 654] } }] }, { '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 400 } }] }] })
317
+ .then(function(msg) {
318
+ // console.log('select all then', msg)
319
+ rt = msg
320
+ })
321
+ .catch(function(msg) {
322
+ // console.log('select all catch', msg)
323
+ rt = msg.toString()
324
+ })
325
+ vget[9] = rt
326
+
327
+ // //select by regex
328
+ // rt = null
329
+ // vans[14] = []
330
+ // let sr = await wo.select({ name: { $regex: 'PeT', $options: '$i' } })
331
+ // .then(function(msg) {
332
+ // // console.log('select all then', msg)
333
+ // rt = msg
334
+ // })
335
+ // .catch(function(msg) {
336
+ // // console.log('select all catch', msg)
337
+ // rt = msg.toString()
338
+ // })
339
+ // vget[14] = rt
340
+
341
+ //save
342
+ rt = null
343
+ // vans[10] = [{ n: 1, nInserted: 1, ok: 1 }]
344
+ await wo.save(rsa, { autoInsert: true })
345
+ .then(function(msg) {
346
+ // console.log('save then', msg)
347
+ rt = msg
348
+ })
349
+ .catch(function(msg) {
350
+ // console.log('save catch', msg)
351
+ rt = msg.toString()
352
+ })
353
+ vget[10] = rt
354
+
355
+ //del
356
+ rt = null
357
+ // vans[11] = [{ n: 1, nDeleted: 0, ok: 1 }]
358
+ let d = {
359
+ time: '2024-01-01T00:00:00Z',
360
+ }
361
+ await wo.del(d)
362
+ .then(function(msg) {
363
+ // console.log('del then', msg)
364
+ rt = msg
365
+ })
366
+ .catch(function(msg) {
367
+ // console.log('del catch', msg)
368
+ rt = msg.toString()
369
+ })
370
+ vget[11] = rt
371
+
372
+ //del
373
+ rt = null
374
+ // vans[12] = [
375
+ // { n: 1, nDeleted: 1, ok: 1 },
376
+ // { n: 1, nDeleted: 1, ok: 1 },
377
+ // { n: 1, nDeleted: 1, ok: 1 },
378
+ // { n: 1, nDeleted: 1, ok: 1 },
379
+ // { n: 1, nDeleted: 1, ok: 1 },
380
+ // { n: 1, nDeleted: 1, ok: 1 },
381
+ // { n: 1, nDeleted: 1, ok: 1 },
382
+ // { n: 1, nDeleted: 1, ok: 1 },
383
+ // { n: 1, nDeleted: 1, ok: 1 },
384
+ // { n: 1, nDeleted: 1, ok: 1 }
385
+ // ]
386
+ let ss = await wo.select()
387
+ let ds = ss.filter(function(v) {
388
+ return v.name.indexOf('peter') >= 0 || v.name.indexOf('kettle') >= 0 || v.name.indexOf('sandler') >= 0
462
389
  })
390
+ await wo.del(ds)
391
+ .then(function(msg) {
392
+ // console.log('del then', msg)
393
+ rt = msg
394
+ })
395
+ .catch(function(msg) {
396
+ // console.log('del catch', msg)
397
+ rt = msg.toString()
398
+ })
399
+ vget[12] = rt
400
+
401
+ //select all final
402
+ rt = null
403
+ // vans[13] = [
404
+ // { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
405
+ // { time: new Date('2025-01-01T00:04:00.000Z'), name: 'rosemary', value: 123.1236 },
406
+ // { time: new Date('2025-01-01T00:07:00.000Z'), name: 'rosemary', value: 124.76 },
407
+ // { time: new Date('2025-01-01T00:10:00.000Z'), name: 'rosemary(modify)', value: 113.98 }
408
+ // ]
409
+ await wo.select()
410
+ .then(function(msg) {
411
+ // console.log('select all then', msg)
412
+ rt = msg
413
+ })
414
+ .catch(function(msg) {
415
+ // console.log('select all catch', msg)
416
+ rt = msg.toString()
417
+ })
418
+ vget[13] = rt
463
419
 
464
- it(`should get ${JSON.stringify(vans[13])} for select all final`, async function() {
465
- assert.strict.deepStrictEqual(vget[13], vans[13])
420
+ //清除數據, 後續cache測試共用users表
421
+ await wo.delAll()
422
+
423
+ //woc, 啟用useCache
424
+ let woc = WOrm({
425
+ ...opt,
426
+ useCache: true,
466
427
  })
467
-
468
- })
469
- }
470
-
428
+
429
+ let rsc = [
430
+ {
431
+ time: '2025-01-01T00:00:00Z',
432
+ name: 'peter',
433
+ value: 123,
434
+ },
435
+ {
436
+ time: '2025-01-01T00:01:00Z',
437
+ name: 'rosemary',
438
+ value: 123.456,
439
+ },
440
+ {
441
+ time: '2025-01-01T00:02:00Z',
442
+ name: 'kettle',
443
+ value: 456,
444
+ },
445
+ ]
446
+
447
+ let rscm = [
448
+ {
449
+ time: '2025-01-01T00:01:00Z',
450
+ name: 'rosemary(modify)',
451
+ value: 654.321,
452
+ },
453
+ ]
454
+
455
+ //insert
456
+ await woc.insert(rsc)
457
+
458
+ //select all (1st time, 從DB讀取並填入快取)
459
+ rt = null
460
+ // vans[14] = [
461
+ // { time: new Date('2025-01-01T00:00:00.000Z'), name: 'peter', value: 123 },
462
+ // { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
463
+ // { time: new Date('2025-01-01T00:02:00.000Z'), name: 'kettle', value: 456 },
464
+ // ]
465
+ await woc.select()
466
+ .then(function(msg) {
467
+ // console.log('select 1st then', msg)
468
+ //依time排序, postgres UPDATE後物理順序可能改變
469
+ rt = msg.sort((a, b) => a.time - b.time)
470
+ })
471
+ .catch(function(msg) {
472
+ // console.log('select 1st catch', msg)
473
+ rt = msg.toString()
474
+ })
475
+ vget[14] = rt
476
+
477
+ //select all (2nd time, 命中快取, 內容須與第一次相同)
478
+ rt = null
479
+ // vans[15] = vans[14]
480
+ await woc.select()
481
+ .then(function(msg) {
482
+ // console.log('select 2nd then', msg)
483
+ rt = msg.sort((a, b) => a.time - b.time)
484
+ })
485
+ .catch(function(msg) {
486
+ // console.log('select 2nd catch', msg)
487
+ rt = msg.toString()
488
+ })
489
+ vget[15] = rt
490
+
491
+ //save (更新rosemary, 觸發快取重設)
492
+ rt = null
493
+ // vans[16] = [{ n: 1, nModified: 1, ok: 1 }]
494
+ await woc.save(rscm, { autoInsert: false })
495
+ .then(function(msg) {
496
+ // console.log('save then', msg)
497
+ rt = msg
498
+ })
499
+ .catch(function(msg) {
500
+ // console.log('save catch', msg)
501
+ rt = msg.toString()
502
+ })
503
+ vget[16] = rt
504
+
505
+ //select all (3rd time, 快取已重設, 從DB重新讀取, 須反映rosemary更新)
506
+ rt = null
507
+ // vans[17] = [
508
+ // { time: new Date('2025-01-01T00:00:00.000Z'), name: 'peter', value: 123 },
509
+ // { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary(modify)', value: 654.321 },
510
+ // { time: new Date('2025-01-01T00:02:00.000Z'), name: 'kettle', value: 456 },
511
+ // ]
512
+ await woc.select()
513
+ .then(function(msg) {
514
+ // console.log('select 3rd then', msg)
515
+ rt = msg.sort((a, b) => a.time - b.time)
516
+ })
517
+ .catch(function(msg) {
518
+ // console.log('select 3rd catch', msg)
519
+ rt = msg.toString()
520
+ })
521
+ vget[17] = rt
522
+
523
+ })
524
+
525
+ vans[1] = { ok: 1 }
526
+ it(`should get ${JSON.stringify(vans[1])} for delAll`, async function() {
527
+ assert.strict.deepStrictEqual(vget[1], vans[1])
528
+ })
529
+
530
+ vans[2] = { n: 13, nInserted: 13, ok: 1 }
531
+ it(`should get ${JSON.stringify(vans[2])} for insert`, async function() {
532
+ assert.strict.deepStrictEqual(vget[2], vans[2])
533
+ })
534
+
535
+ vans[3] = [
536
+ { n: 1, nModified: 0, ok: 1 },
537
+ { n: 1, nModified: 1, ok: 1 },
538
+ { n: 1, nModified: 1, ok: 1 },
539
+ { n: 1, nModified: 1, ok: 1 },
540
+ { n: 0, nModified: 0, ok: 1 }
541
+ ]
542
+ it(`should get ${JSON.stringify(vans[3])} for save(autoInsert=false)`, async function() {
543
+ assert.strict.deepStrictEqual(vget[3], vans[3])
544
+ })
545
+
546
+ vans[4] = [
547
+ { time: new Date('2025-01-01T00:00:00.000Z'), name: 'peter', value: 123 },
548
+ { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
549
+ { time: new Date('2025-01-01T00:02:00.000Z'), name: 'kettle', value: 456 },
550
+ { time: new Date('2025-01-01T00:03:00.000Z'), name: 'peter', value: 200 },
551
+ { time: new Date('2025-01-01T00:04:00.000Z'), name: 'rosemary', value: 123.1236 },
552
+ { time: new Date('2025-01-01T00:05:00.000Z'), name: 'kettle', value: 488 },
553
+ { time: new Date('2025-01-01T00:06:00.000Z'), name: 'peter', value: 125 },
554
+ { time: new Date('2025-01-01T00:07:00.000Z'), name: 'rosemary', value: 124.76 },
555
+ { time: new Date('2025-01-01T00:08:00.000Z'), name: 'kettle', value: 524 },
556
+ { time: new Date('2025-01-01T00:09:00.000Z'), name: 'peter', value: 127 },
557
+ { time: new Date('2025-01-01T00:10:00.000Z'), name: 'rosemary(modify)', value: 113.98 },
558
+ { time: new Date('2025-01-01T00:11:00.000Z'), name: 'kettle(modify)', value: 447 },
559
+ { time: new Date('2025-01-01T00:12:00.000Z'), name: 'peter', value: 99 }
560
+ ]
561
+ it(`should get ${JSON.stringify(vans[4])} for select all`, async function() {
562
+ assert.strict.deepStrictEqual(vget[4], vans[4])
563
+ })
564
+
565
+ vans[5] = vans[4]
566
+ it(`should get ${JSON.stringify(vans[5])} for select all by cache`, async function() {
567
+ assert.strict.deepStrictEqual(vget[5], vans[5])
568
+ })
569
+
570
+ vans[6] = [
571
+ { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
572
+ { time: new Date('2025-01-01T00:04:00.000Z'), name: 'rosemary', value: 123.1236 },
573
+ { time: new Date('2025-01-01T00:07:00.000Z'), name: 'rosemary', value: 124.76 }
574
+ ]
575
+ it(`should get ${JSON.stringify(vans[6])} for select by name`, async function() {
576
+ assert.strict.deepStrictEqual(vget[6], vans[6])
577
+ })
578
+
579
+ vans[7] = [
580
+ { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
581
+ { time: new Date('2025-01-01T00:04:00.000Z'), name: 'rosemary', value: 123.1236 },
582
+ { time: new Date('2025-01-01T00:06:00.000Z'), name: 'peter', value: 125 },
583
+ { time: new Date('2025-01-01T00:07:00.000Z'), name: 'rosemary', value: 124.76 },
584
+ { time: new Date('2025-01-01T00:09:00.000Z'), name: 'peter', value: 127 }
585
+ ]
586
+ it(`should get ${JSON.stringify(vans[7])} for select by $and, $gt, $lt`, async function() {
587
+ assert.strict.deepStrictEqual(vget[7], vans[7])
588
+ })
589
+
590
+ vans[8] = [
591
+ { time: new Date('2025-01-01T00:02:00.000Z'), name: 'kettle', value: 456 },
592
+ { time: new Date('2025-01-01T00:03:00.000Z'), name: 'peter', value: 200 },
593
+ { time: new Date('2025-01-01T00:05:00.000Z'), name: 'kettle', value: 488 },
594
+ { time: new Date('2025-01-01T00:08:00.000Z'), name: 'kettle', value: 524 },
595
+ { time: new Date('2025-01-01T00:11:00.000Z'), name: 'kettle(modify)', value: 447 }
596
+ ]
597
+ it(`should get ${JSON.stringify(vans[8])} for select by $or, $gte, $lte`, async function() {
598
+ assert.strict.deepStrictEqual(vget[8], vans[8])
599
+ })
600
+
601
+ vans[9] = [
602
+ { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
603
+ { time: new Date('2025-01-01T00:02:00.000Z'), name: 'kettle', value: 456 },
604
+ { time: new Date('2025-01-01T00:05:00.000Z'), name: 'kettle', value: 488 },
605
+ { time: new Date('2025-01-01T00:08:00.000Z'), name: 'kettle', value: 524 },
606
+ { time: new Date('2025-01-01T00:11:00.000Z'), name: 'kettle(modify)', value: 447 }
607
+ ]
608
+ it(`should get ${JSON.stringify(vans[9])} for select by $or, $and, $ne, $in, $nin`, async function() {
609
+ assert.strict.deepStrictEqual(vget[9], vans[9])
610
+ })
611
+
612
+ // vans[14] = []
613
+ // it(`should get ${JSON.stringify(vans[14])} for select by regex`, async function() {
614
+ // assert.strict.deepStrictEqual(vget[14], vans[14])
615
+ // })
616
+
617
+ vans[10] = [{ n: 1, nInserted: 1, ok: 1 }]
618
+ it(`should get ${JSON.stringify(vans[10])} for save(autoInsert=true)`, async function() {
619
+ assert.strict.deepStrictEqual(vget[10], vans[10])
620
+ })
621
+
622
+ vans[11] = [{ n: 1, nDeleted: 0, ok: 1 }]
623
+ it(`should get ${JSON.stringify(vans[11])} for del`, async function() {
624
+ assert.strict.deepStrictEqual(vget[11], vans[11])
625
+ })
626
+
627
+ vans[12] = [
628
+ { n: 1, nDeleted: 1, ok: 1 },
629
+ { n: 1, nDeleted: 1, ok: 1 },
630
+ { n: 1, nDeleted: 1, ok: 1 },
631
+ { n: 1, nDeleted: 1, ok: 1 },
632
+ { n: 1, nDeleted: 1, ok: 1 },
633
+ { n: 1, nDeleted: 1, ok: 1 },
634
+ { n: 1, nDeleted: 1, ok: 1 },
635
+ { n: 1, nDeleted: 1, ok: 1 },
636
+ { n: 1, nDeleted: 1, ok: 1 },
637
+ { n: 1, nDeleted: 1, ok: 1 }
638
+ ]
639
+ it(`should get ${JSON.stringify(vans[12])} for del`, async function() {
640
+ assert.strict.deepStrictEqual(vget[12], vans[12])
641
+ })
642
+
643
+ vans[13] = [
644
+ { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
645
+ { time: new Date('2025-01-01T00:04:00.000Z'), name: 'rosemary', value: 123.1236 },
646
+ { time: new Date('2025-01-01T00:07:00.000Z'), name: 'rosemary', value: 124.76 },
647
+ { time: new Date('2025-01-01T00:10:00.000Z'), name: 'rosemary(modify)', value: 113.98 }
648
+ ]
649
+ it(`should get ${JSON.stringify(vans[13])} for select all final`, async function() {
650
+ assert.strict.deepStrictEqual(vget[13], vans[13])
651
+ })
652
+
653
+ vans[14] = [
654
+ { time: new Date('2025-01-01T00:00:00.000Z'), name: 'peter', value: 123 },
655
+ { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary', value: 123.456 },
656
+ { time: new Date('2025-01-01T00:02:00.000Z'), name: 'kettle', value: 456 },
657
+ ]
658
+ it(`should get ${JSON.stringify(vans[14])} for select all (1st, fill cache)`, async function() {
659
+ assert.strict.deepStrictEqual(vget[14], vans[14])
660
+ })
661
+
662
+ vans[15] = vans[14]
663
+ it(`should get ${JSON.stringify(vans[15])} for select all (2nd, cache hit)`, async function() {
664
+ assert.strict.deepStrictEqual(vget[15], vans[15])
665
+ })
666
+
667
+ vans[16] = [{ n: 1, nModified: 1, ok: 1 }]
668
+ it(`should get ${JSON.stringify(vans[16])} for save (invalidate cache)`, async function() {
669
+ assert.strict.deepStrictEqual(vget[16], vans[16])
670
+ })
671
+
672
+ vans[17] = [
673
+ { time: new Date('2025-01-01T00:00:00.000Z'), name: 'peter', value: 123 },
674
+ { time: new Date('2025-01-01T00:01:00.000Z'), name: 'rosemary(modify)', value: 654.321 },
675
+ { time: new Date('2025-01-01T00:02:00.000Z'), name: 'kettle', value: 456 },
676
+ ]
677
+ it(`should get ${JSON.stringify(vans[17])} for select all (3rd, reload after save)`, async function() {
678
+ assert.strict.deepStrictEqual(vget[17], vans[17])
679
+ })
680
+
681
+ })