w-orm-mongodb 1.1.31 → 1.1.33

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