w-orm-mongodb 1.1.28 → 1.1.30

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.
@@ -2,157 +2,135 @@ import assert from 'assert'
2
2
  import wo from '../src/WOrmMongodb.mjs'
3
3
 
4
4
 
5
- describe('basic', async function() {
6
-
7
-
8
- let opt = {
9
- url: 'mongodb://username:password@127.0.0.1:27017',
10
- db: 'worm',
11
- cl: 'users',
12
- }
13
-
14
-
15
- let rs = [
16
- {
17
- id: 'id-peter',
18
- name: 'peter',
19
- value: 123,
20
- },
21
- {
22
- id: 'id-rosemary',
23
- name: 'rosemary',
24
- value: 123.456,
25
- },
26
- {
27
- id: '',
28
- name: 'kettle',
29
- value: 456,
30
- },
31
- ]
32
-
33
-
34
- let rsm = [
35
- {
36
- id: 'id-peter',
37
- name: 'peter(modify)'
38
- },
39
- {
40
- id: 'id-rosemary',
41
- name: 'rosemary(modify)'
42
- },
43
- {
44
- id: '',
45
- name: 'kettle(modify)'
46
- },
47
- ]
48
-
49
-
50
- //w
51
- let w = wo(opt)
52
-
53
-
54
- //on
55
- w.on('change', function(mode, data, res) {
56
- // console.log('change', mode)
57
- })
5
+ describe('basic', function() {
6
+ let rt = null
7
+ let vans = {}
8
+ let vget = {}
58
9
 
59
10
 
60
- //delAll
61
- if (true) {
11
+ before(async function () {
62
12
 
63
- let rr = {
64
- nDeleted: 2,
65
- ok: 1,
13
+
14
+ let opt = {
15
+ url: 'mongodb://username:password@127.0.0.1:27017',
16
+ db: 'worm',
17
+ cl: 'users',
66
18
  }
67
19
 
68
- it(`should get ${JSON.stringify(rr)} for delAll`, async function() {
69
-
70
- let rt = null
71
- await w.delAll()
72
- .then(function(msg) {
73
- // console.log('delAll then', msg)
74
- //考慮可能有不同初始測試數據
75
- // delAll then { n: 2, nDeleted: 2, ok: 1 }
76
- // delAll then { n: 3, nDeleted: 2, ok: 1 }
77
- //僅針對nDeleted與ok欄位比對
78
- rt = {
79
- nDeleted: msg.nDeleted,
80
- ok: msg.ok,
81
- }
82
- })
83
- .catch(function(msg) {
84
- // console.log('delAll catch', msg)
85
- rt = msg.toString()
86
- })
87
-
88
- assert.strict.deepStrictEqual(rt, rr)
89
- })
90
20
 
91
- }
21
+ let rs = [
22
+ {
23
+ id: 'id-peter',
24
+ name: 'peter',
25
+ value: 123,
26
+ },
27
+ {
28
+ id: 'id-rosemary',
29
+ name: 'rosemary',
30
+ value: 123.456,
31
+ },
32
+ {
33
+ id: '',
34
+ name: 'kettle',
35
+ value: 456,
36
+ },
37
+ ]
92
38
 
93
39
 
94
- //insert
95
- if (true) {
40
+ let rsm = [
41
+ {
42
+ id: 'id-peter',
43
+ name: 'peter(modify)'
44
+ },
45
+ {
46
+ id: 'id-rosemary',
47
+ name: 'rosemary(modify)'
48
+ },
49
+ {
50
+ id: '',
51
+ name: 'kettle(modify)'
52
+ },
53
+ ]
96
54
 
97
- let rr = { n: 3, nInserted: 3, ok: 1 }
98
55
 
99
- it(`should get ${JSON.stringify(rr)} for insert`, async function() {
56
+ //w
57
+ let w = wo(opt)
100
58
 
101
- let rt = null
102
- await w.insert(rs)
103
- .then(function(msg) {
104
- // console.log('insert then', msg)
105
- // insert then { n: 3, nInserted: 3, ok: 1 }
106
- rt = msg
107
- })
108
- .catch(function(msg) {
109
- // console.log('insert catch', msg)
110
- rt = msg.toString()
111
- })
112
59
 
113
- assert.strict.deepStrictEqual(rt, rr)
60
+ //on
61
+ w.on('change', function(mode, data, res) {
62
+ // console.log('change', mode)
114
63
  })
115
64
 
116
- }
117
65
 
66
+ //delAll
67
+ rt = null
68
+ vans[1] = {
69
+ nDeleted: 2,
70
+ ok: 1,
71
+ }
72
+ await w.delAll()
73
+ .then(function(msg) {
74
+ // console.log('delAll then', msg)
75
+ //考慮可能有不同初始測試數據
76
+ // delAll then { n: 2, nDeleted: 2, ok: 1 }
77
+ // delAll then { n: 3, nDeleted: 2, ok: 1 }
78
+ //僅針對nDeleted與ok欄位比對
79
+ rt = {
80
+ nDeleted: msg.nDeleted,
81
+ ok: msg.ok,
82
+ }
83
+ })
84
+ .catch(function(msg) {
85
+ // console.log('delAll catch', msg)
86
+ rt = msg.toString()
87
+ })
88
+ vget[1] = rt
89
+
90
+
91
+ //insert
92
+ rt = null
93
+ vans[2] = { n: 3, nInserted: 3, ok: 1 }
94
+ await w.insert(rs)
95
+ .then(function(msg) {
96
+ // console.log('insert then', msg)
97
+ // insert then { n: 3, nInserted: 3, ok: 1 }
98
+ rt = msg
99
+ })
100
+ .catch(function(msg) {
101
+ // console.log('insert catch', msg)
102
+ rt = msg.toString()
103
+ })
104
+ vget[2] = rt
118
105
 
119
- //save
120
- if (true) {
121
106
 
122
- let rr = [
107
+ //save
108
+ rt = null
109
+ vans[3] = [
123
110
  { n: 1, nModified: 1, ok: 1 },
124
111
  { n: 1, nModified: 1, ok: 1 },
125
112
  { n: 0, nModified: 0, ok: 1 }
126
113
  ]
114
+ await w.save(rsm, { autoInsert: false, atomic: true })
115
+ .then(function(msg) {
116
+ // console.log('save then', msg)
117
+ // save then [
118
+ // { n: 1, nModified: 1, ok: 1 },
119
+ // { n: 1, nModified: 1, ok: 1 },
120
+ // { n: 0, nModified: 0, ok: 1 }
121
+ // ]
122
+ rt = msg
123
+ })
124
+ .catch(function(msg) {
125
+ // console.log('save catch', msg)
126
+ rt = msg.toString()
127
+ })
128
+ vget[3] = rt
127
129
 
128
- it(`should get ${JSON.stringify(rr)} for save`, async function() {
129
-
130
- let rt = null
131
- await w.save(rsm, { autoInsert: false, atomic: true })
132
- .then(function(msg) {
133
- // console.log('save then', msg)
134
- // save then [
135
- // { n: 1, nModified: 1, ok: 1 },
136
- // { n: 1, nModified: 1, ok: 1 },
137
- // { n: 0, nModified: 0, ok: 1 }
138
- // ]
139
- rt = msg
140
- })
141
- .catch(function(msg) {
142
- // console.log('save catch', msg)
143
- rt = msg.toString()
144
- })
145
-
146
- assert.strict.deepStrictEqual(rt, rr)
147
- })
148
-
149
- }
150
-
151
-
152
- //select all
153
- if (true) {
154
130
 
155
- let rr = [
131
+ //select all
132
+ rt = null
133
+ vans[4] = [
156
134
  { id: 'id-peter', name: 'peter(modify)', value: 123 },
157
135
  { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 },
158
136
  {
@@ -161,138 +139,102 @@ describe('basic', async function() {
161
139
  value: 456
162
140
  }
163
141
  ]
142
+ await w.select()
143
+ .then(function(msg) {
144
+ // console.log('select all then', msg)
145
+ // select all [
146
+ // { id: 'id-peter', name: 'peter(modify)', value: 123 },
147
+ // { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 },
148
+ // {
149
+ // id: {random id},
150
+ // name: 'kettle',
151
+ // value: 456
152
+ // }
153
+ // ]
154
+ rt = [
155
+ msg[0],
156
+ msg[1],
157
+ {
158
+ name: msg[2].name,
159
+ value: msg[2].value,
160
+ },
161
+ ]
162
+ })
163
+ .catch(function(msg) {
164
+ // console.log('select all catch', msg)
165
+ rt = msg.toString()
166
+ })
167
+ vget[4] = rt
164
168
 
165
- it(`should get ${JSON.stringify(rr)} for select all`, async function() {
166
-
167
- let rt = null
168
- await w.select()
169
- .then(function(msg) {
170
- // console.log('select all then', msg)
171
- // select all [
172
- // { id: 'id-peter', name: 'peter(modify)', value: 123 },
173
- // { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 },
174
- // {
175
- // id: {random id},
176
- // name: 'kettle',
177
- // value: 456
178
- // }
179
- // ]
180
- rt = [
181
- msg[0],
182
- msg[1],
183
- {
184
- name: msg[2].name,
185
- value: msg[2].value,
186
- },
187
- ]
188
- })
189
- .catch(function(msg) {
190
- // console.log('select all catch', msg)
191
- rt = msg.toString()
192
- })
193
-
194
- assert.strict.deepStrictEqual(rt, rr)
195
- })
196
-
197
- }
198
-
199
-
200
- //select
201
- if (true) {
202
-
203
- let rr = [{ id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 }]
204
-
205
- it(`should get ${JSON.stringify(rr)} for select`, async function() {
206
-
207
- let rt = null
208
- await w.select({ id: 'id-rosemary' })
209
- .then(function(msg) {
210
- // console.log('select then', msg)
211
- // select [ { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 } ]
212
- rt = msg
213
- })
214
- .catch(function(msg) {
215
- // console.log('select catch', msg)
216
- rt = msg.toString()
217
- })
218
-
219
- assert.strict.deepStrictEqual(rt, rr)
220
- })
221
-
222
- }
223
-
224
-
225
- //select by $and, $gt, $lt
226
- if (true) {
227
-
228
- let rr = [{ id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 }]
229
-
230
- it(`should get ${JSON.stringify(rr)} for select by $and, $gt, $lt`, async function() {
231
-
232
- let rt = null
233
- await w.select({ '$and': [{ value: { '$gt': 123 } }, { value: { '$lt': 200 } }] })
234
- .then(function(msg) {
235
- // console.log('select by $and, $gt, $lt then', msg)
236
- // select by $and, $gt, $lt [ { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 } ]
237
- rt = msg
238
- })
239
- .catch(function(msg) {
240
- // console.log('select by $and, $gt, $lt catch', msg)
241
- rt = msg.toString()
242
- })
243
169
 
244
- assert.strict.deepStrictEqual(rt, rr)
245
- })
170
+ //select
171
+ rt = null
172
+ vans[5] = [{ id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 }]
173
+ await w.select({ id: 'id-rosemary' })
174
+ .then(function(msg) {
175
+ // console.log('select then', msg)
176
+ // select [ { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 } ]
177
+ rt = msg
178
+ })
179
+ .catch(function(msg) {
180
+ // console.log('select catch', msg)
181
+ rt = msg.toString()
182
+ })
183
+ vget[5] = rt
246
184
 
247
- }
248
185
 
186
+ //select by $and, $gt, $lt
187
+ rt = null
188
+ vans[6] = [{ id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 }]
189
+ await w.select({ '$and': [{ value: { '$gt': 123 } }, { value: { '$lt': 200 } }] })
190
+ .then(function(msg) {
191
+ // console.log('select by $and, $gt, $lt then', msg)
192
+ // select by $and, $gt, $lt [ { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 } ]
193
+ rt = msg
194
+ })
195
+ .catch(function(msg) {
196
+ // console.log('select by $and, $gt, $lt catch', msg)
197
+ rt = msg.toString()
198
+ })
199
+ vget[6] = rt
249
200
 
250
- //select by $or, $gte, $lte
251
- if (true) {
252
201
 
253
- let rr = [
202
+ //select by $or, $gte, $lte
203
+ rt = null
204
+ vans[7] = [
254
205
  {
255
206
  // id: {random id},
256
207
  name: 'kettle',
257
208
  value: 456
258
209
  }
259
210
  ]
211
+ await w.select({ '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 200 } }] })
212
+ .then(function(msg) {
213
+ // console.log('select by $or, $gte, $lte then', msg)
214
+ // select by $or, $gte, $lte [
215
+ // {
216
+ // id: {random id},
217
+ // name: 'kettle',
218
+ // value: 456
219
+ // }
220
+ // ]
221
+ rt = [
222
+ {
223
+ name: msg[0].name,
224
+ value: msg[0].value,
225
+ },
226
+ ]
227
+ })
228
+ .catch(function(msg) {
229
+ // console.log('select by $or, $gte, $lte catch', msg)
230
+ rt = msg.toString()
231
+ })
232
+ vget[7] = rt
260
233
 
261
- it(`should get ${JSON.stringify(rr)} for select by $or, $gte, $lte`, async function() {
262
-
263
- let rt = null
264
- await w.select({ '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 200 } }] })
265
- .then(function(msg) {
266
- // console.log('select by $or, $gte, $lte then', msg)
267
- // select by $or, $gte, $lte [
268
- // {
269
- // id: {random id},
270
- // name: 'kettle',
271
- // value: 456
272
- // }
273
- // ]
274
- rt = [
275
- {
276
- name: msg[0].name,
277
- value: msg[0].value,
278
- },
279
- ]
280
- })
281
- .catch(function(msg) {
282
- // console.log('select by $or, $gte, $lte catch', msg)
283
- rt = msg.toString()
284
- })
285
-
286
- assert.strict.deepStrictEqual(rt, rr)
287
- })
288
-
289
- }
290
-
291
-
292
- //select by $or, $and, $ne, $in, $nin
293
- if (true) {
294
234
 
295
- let rr = [
235
+ //select by $or, $and, $ne, $in, $nin
236
+ rt = null
237
+ vans[8] = [
296
238
  {
297
239
  id: 'id-rosemary',
298
240
  name: 'rosemary(modify)',
@@ -304,97 +246,123 @@ describe('basic', async function() {
304
246
  value: 456
305
247
  }
306
248
  ]
249
+ await w.select({ '$or': [{ '$and': [{ value: { '$ne': 123 } }, { value: { '$in': [123, 321, 123.456, 456] } }, { value: { '$nin': [456, 654] } }] }, { '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 400 } }] }] })
250
+ .then(function(msg) {
251
+ // console.log('select by $or, $and, $ne, $in, $nin then', msg)
252
+ // select by $or, $and, $ne, $in, $nin [
253
+ // {
254
+ // id: 'id-rosemary',
255
+ // name: 'rosemary(modify)',
256
+ // value: 123.456
257
+ // },
258
+ // {
259
+ // id: {random id},
260
+ // name: 'kettle',
261
+ // value: 456
262
+ // }
263
+ // ]
264
+ rt = [
265
+ msg[0],
266
+ {
267
+ name: msg[1].name,
268
+ value: msg[1].value,
269
+ },
270
+ ]
271
+ })
272
+ .catch(function(msg) {
273
+ // console.log('select by $or, $and, $ne, $in, $nin catch', msg)
274
+ rt = msg.toString()
275
+ })
276
+ vget[8] = rt
277
+
278
+
279
+ //select by regex
280
+ rt = null
281
+ vans[9] = [{ id: 'id-peter', name: 'peter(modify)', value: 123 }]
282
+ await w.select({ name: { $regex: 'PeT', $options: '$i' } })
283
+ .then(function(msg) {
284
+ // console.log('select by regex then', msg)
285
+ // selectReg [ { id: 'id-peter', name: 'peter(modify)', value: 123 } ]
286
+ rt = msg
287
+ })
288
+ .catch(function(msg) {
289
+ // console.log('select by regex catch', msg)
290
+ rt = msg.toString()
291
+ })
292
+ vget[9] = rt
293
+
307
294
 
308
- it(`should get ${JSON.stringify(rr)} for select by $or, $and, $ne, $in, $nin`, async function() {
309
-
310
- let rt = null
311
- await w.select({ '$or': [{ '$and': [{ value: { '$ne': 123 } }, { value: { '$in': [123, 321, 123.456, 456] } }, { value: { '$nin': [456, 654] } }] }, { '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 400 } }] }] })
312
- .then(function(msg) {
313
- // console.log('select by $or, $and, $ne, $in, $nin then', msg)
314
- // select by $or, $and, $ne, $in, $nin [
315
- // {
316
- // id: 'id-rosemary',
317
- // name: 'rosemary(modify)',
318
- // value: 123.456
319
- // },
320
- // {
321
- // id: {random id},
322
- // name: 'kettle',
323
- // value: 456
324
- // }
325
- // ]
326
- rt = [
327
- msg[0],
328
- {
329
- name: msg[1].name,
330
- value: msg[1].value,
331
- },
332
- ]
333
- })
334
- .catch(function(msg) {
335
- // console.log('select by $or, $and, $ne, $in, $nin catch', msg)
336
- rt = msg.toString()
337
- })
338
-
339
- assert.strict.deepStrictEqual(rt, rr)
295
+ //del
296
+ rt = null
297
+ vans[10] = [{ n: 1, nDeleted: 1, ok: 1 }]
298
+ let ss = await w.select()
299
+ let d = ss.filter(function(v) {
300
+ return v.name === 'kettle'
340
301
  })
302
+ await w.del(d)
303
+ .then(function(msg) {
304
+ // console.log('del then', msg)
305
+ // del then [ { n: 1, nDeleted: 1, ok: 1 } ]
306
+ rt = msg
307
+ })
308
+ .catch(function(msg) {
309
+ // console.log('del catch', msg)
310
+ rt = msg.toString()
311
+ })
312
+ vget[10] = rt
341
313
 
342
- }
343
314
 
315
+ })
344
316
 
345
- //select by regex
346
- if (true) {
347
317
 
348
- let rr = [{ id: 'id-peter', name: 'peter(modify)', value: 123 }]
318
+ it(`should get ${JSON.stringify(vans[1])} for delAll`, async function() {
319
+ assert.strict.deepStrictEqual(vget[1], vans[1])
320
+ })
349
321
 
350
- it(`should get ${JSON.stringify(rr)} for select by regex`, async function() {
351
322
 
352
- let rt = null
353
- await w.select({ name: { $regex: 'PeT', $options: '$i' } })
354
- .then(function(msg) {
355
- // console.log('select by regex then', msg)
356
- // selectReg [ { id: 'id-peter', name: 'peter(modify)', value: 123 } ]
357
- rt = msg
358
- })
359
- .catch(function(msg) {
360
- // console.log('select by regex catch', msg)
361
- rt = msg.toString()
362
- })
323
+ it(`should get ${JSON.stringify(vans[2])} for insert`, async function() {
324
+ assert.strict.deepStrictEqual(vget[2], vans[2])
325
+ })
363
326
 
364
- assert.strict.deepStrictEqual(rt, rr)
365
- })
366
327
 
367
- }
328
+ it(`should get ${JSON.stringify(vans[3])} for save`, async function() {
329
+ assert.strict.deepStrictEqual(vget[3], vans[3])
330
+ })
368
331
 
369
332
 
370
- //del
371
- if (true) {
333
+ it(`should get ${JSON.stringify(vans[4])} for select all`, async function() {
334
+ assert.strict.deepStrictEqual(vget[4], vans[4])
335
+ })
372
336
 
373
- let rr = [{ n: 1, nDeleted: 1, ok: 1 }]
374
337
 
375
- it(`should get ${JSON.stringify(rr)} for del`, async function() {
338
+ it(`should get ${JSON.stringify(vans[5])} for select`, async function() {
339
+ assert.strict.deepStrictEqual(vget[5], vans[5])
340
+ })
376
341
 
377
- let ss = await w.select()
378
- let d = ss.filter(function(v) {
379
- return v.name === 'kettle'
380
- })
381
342
 
382
- let rt = null
383
- await w.del(d)
384
- .then(function(msg) {
385
- // console.log('del then', msg)
386
- // del then [ { n: 1, nDeleted: 1, ok: 1 } ]
387
- rt = msg
388
- })
389
- .catch(function(msg) {
390
- // console.log('del catch', msg)
391
- rt = msg.toString()
392
- })
393
-
394
- assert.strict.deepStrictEqual(rt, rr)
395
- })
343
+ it(`should get ${JSON.stringify(vans[6])} for select by $and, $gt, $lt`, async function() {
344
+ assert.strict.deepStrictEqual(vget[6], vans[6])
345
+ })
396
346
 
397
- }
347
+
348
+ it(`should get ${JSON.stringify(vans[7])} for select by $or, $gte, $lte`, async function() {
349
+ assert.strict.deepStrictEqual(vget[7], vans[7])
350
+ })
351
+
352
+
353
+ it(`should get ${JSON.stringify(vans[8])} for select by $or, $and, $ne, $in, $nin`, async function() {
354
+ assert.strict.deepStrictEqual(vget[8], vans[8])
355
+ })
356
+
357
+
358
+ it(`should get ${JSON.stringify(vans[9])} for select by regex`, async function() {
359
+ assert.strict.deepStrictEqual(vget[9], vans[9])
360
+ })
361
+
362
+
363
+ it(`should get ${JSON.stringify(vans[10])} for del`, async function() {
364
+ assert.strict.deepStrictEqual(vget[10], vans[10])
365
+ })
398
366
 
399
367
 
400
368
  })