w-orm-mongodb 1.1.31 → 1.1.32

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