w-orm-mongodb 1.1.26 → 1.1.28

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,400 @@
1
+ import assert from 'assert'
2
+ import wo from '../src/WOrmMongodb.mjs'
3
+
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
+ })
58
+
59
+
60
+ //delAll
61
+ if (true) {
62
+
63
+ let rr = {
64
+ nDeleted: 2,
65
+ ok: 1,
66
+ }
67
+
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
+
91
+ }
92
+
93
+
94
+ //insert
95
+ if (true) {
96
+
97
+ let rr = { n: 3, nInserted: 3, ok: 1 }
98
+
99
+ it(`should get ${JSON.stringify(rr)} for insert`, async function() {
100
+
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
+
113
+ assert.strict.deepStrictEqual(rt, rr)
114
+ })
115
+
116
+ }
117
+
118
+
119
+ //save
120
+ if (true) {
121
+
122
+ let rr = [
123
+ { n: 1, nModified: 1, ok: 1 },
124
+ { n: 1, nModified: 1, ok: 1 },
125
+ { n: 0, nModified: 0, ok: 1 }
126
+ ]
127
+
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
+
155
+ let rr = [
156
+ { id: 'id-peter', name: 'peter(modify)', value: 123 },
157
+ { id: 'id-rosemary', name: 'rosemary(modify)', value: 123.456 },
158
+ {
159
+ // id: {random id},
160
+ name: 'kettle',
161
+ value: 456
162
+ }
163
+ ]
164
+
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
+
244
+ assert.strict.deepStrictEqual(rt, rr)
245
+ })
246
+
247
+ }
248
+
249
+
250
+ //select by $or, $gte, $lte
251
+ if (true) {
252
+
253
+ let rr = [
254
+ {
255
+ // id: {random id},
256
+ name: 'kettle',
257
+ value: 456
258
+ }
259
+ ]
260
+
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
+
295
+ let rr = [
296
+ {
297
+ id: 'id-rosemary',
298
+ name: 'rosemary(modify)',
299
+ value: 123.456
300
+ },
301
+ {
302
+ // id: {random id},
303
+ name: 'kettle',
304
+ value: 456
305
+ }
306
+ ]
307
+
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)
340
+ })
341
+
342
+ }
343
+
344
+
345
+ //select by regex
346
+ if (true) {
347
+
348
+ let rr = [{ id: 'id-peter', name: 'peter(modify)', value: 123 }]
349
+
350
+ it(`should get ${JSON.stringify(rr)} for select by regex`, async function() {
351
+
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
+ })
363
+
364
+ assert.strict.deepStrictEqual(rt, rr)
365
+ })
366
+
367
+ }
368
+
369
+
370
+ //del
371
+ if (true) {
372
+
373
+ let rr = [{ n: 1, nDeleted: 1, ok: 1 }]
374
+
375
+ it(`should get ${JSON.stringify(rr)} for del`, async function() {
376
+
377
+ let ss = await w.select()
378
+ let d = ss.filter(function(v) {
379
+ return v.name === 'kettle'
380
+ })
381
+
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
+ })
396
+
397
+ }
398
+
399
+
400
+ })
@@ -0,0 +1,188 @@
1
+ import assert from 'assert'
2
+ import path from 'path'
3
+ import fs from 'fs'
4
+ import wo from '../src/WOrmMongodb.mjs'
5
+
6
+
7
+ describe('gfs', async function() {
8
+
9
+
10
+ let opt = {
11
+ url: 'mongodb://username:password@127.0.0.1:27017',
12
+ db: 'worm',
13
+ cl: 'usersGfs',
14
+ }
15
+
16
+
17
+ //w
18
+ let w = wo(opt)
19
+
20
+
21
+ //on
22
+ w.on('change', function(mode, data, res) {
23
+ // console.log('change', mode)
24
+ })
25
+
26
+
27
+ //fn_in, fn_out
28
+ let fn_in = path.resolve('../', './_data', 'data(in).dat')
29
+ let fn_out = path.resolve('../', './_data', 'data(out).dat')
30
+ // console.log('fn_in', fn_in)
31
+ // console.log('fn_out', fn_out)
32
+
33
+
34
+ //unlinkSync
35
+ try {
36
+ fs.unlinkSync(fn_out)
37
+ }
38
+ catch (err) {}
39
+
40
+
41
+ //u8a
42
+ let b = await fs.readFileSync(fn_in)
43
+ let u8a = new Uint8Array(b)
44
+ // let u8a = new Uint8Array([66, 97, 115]) //Uint8Array data from nodejs or browser
45
+ // console.log('u8a', u8a)
46
+
47
+
48
+ //delAllGfs
49
+ if (true) {
50
+
51
+ let rr = { n: 0, ok: 1 }
52
+
53
+ it(`should get ${JSON.stringify(rr)} for delAllGfs`, async function() {
54
+
55
+ let rt = null
56
+ await w.delAllGfs()
57
+ .then(function(msg) {
58
+ // console.log('delAllGfs then', msg)
59
+ // delAllGfs then { n: 0, ok: 1 }
60
+ rt = msg
61
+ })
62
+ .catch(function(msg) {
63
+ // console.log('delAllGfs catch', msg)
64
+ rt = msg.toString()
65
+ })
66
+
67
+ assert.strict.deepStrictEqual(rt, rr)
68
+ })
69
+
70
+ }
71
+
72
+
73
+ //insertGfs
74
+ if (true) {
75
+
76
+ let rr = {
77
+ n: 1,
78
+ ok: 1,
79
+ // id: {random id},
80
+ }
81
+
82
+ it(`should get ${JSON.stringify(rr)} for insertGfs`, async function() {
83
+
84
+ let rt = null
85
+ await w.insertGfs(u8a)
86
+ .then(function(msg) {
87
+ // console.log('insertGfs then', msg)
88
+ // insertGfs { n: 1, ok: 1, id: {random id} }
89
+ rt = {
90
+ n: msg.n,
91
+ ok: msg.ok,
92
+ }
93
+ })
94
+ .catch(function(msg) {
95
+ // console.log('insertGfs catch', msg)
96
+ rt = msg.toString()
97
+ })
98
+
99
+ assert.strict.deepStrictEqual(rt, rr)
100
+ })
101
+
102
+ }
103
+ let gi = await w.insertGfs(u8a)
104
+
105
+
106
+ //selectGfs
107
+ if (true) {
108
+
109
+ let rr = [
110
+ 0,
111
+ 0,
112
+ 0,
113
+ 24,
114
+ 102,
115
+ 47381362,
116
+ ]
117
+
118
+ it(`should get ${JSON.stringify(rr)} for selectGfs`, async function() {
119
+
120
+ let rt = null
121
+ await w.selectGfs(gi.id)
122
+ .then(function(msg) {
123
+ // console.log('selectGfs then', msg)
124
+ // console.log('msg[0]', msg[0], msg[0] === 0)
125
+ // console.log('msg[1]', msg[1], msg[1] === 0)
126
+ // console.log('msg[2]', msg[2], msg[2] === 0)
127
+ // console.log('msg[3]', msg[3], msg[3] === 24)
128
+ // console.log('msg[4]', msg[4], msg[4] === 102)
129
+ // console.log('msg.length', msg.length, msg.length === 47381362)
130
+ // selectGfs Uint8Array(47381362) [
131
+ // 0, 0, 0, 24, 102, 116, 121, 112, 109, 112, 52, 50,
132
+ // 0, 0, 0, 0, 105, 115, 111, 109, 109, 112, 52, 50,
133
+ // 0, 2, 14, 73, 109, 111, 111, 118, 0, 0, 0, 108,
134
+ // 109, 118, 104, 100, 0, 0, 0, 0, 214, 15, 24, 167,
135
+ // 214, 15, 24, 167, 0, 1, 95, 144, 1, 106, 95, 88,
136
+ // 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
137
+ // 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
138
+ // 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
139
+ // 0, 0, 0, 0,
140
+ // ... 47381262 more items
141
+ // ]
142
+ // fs.writeFileSync(fn_out, msg)
143
+ rt = [
144
+ msg[0],
145
+ msg[1],
146
+ msg[2],
147
+ msg[3],
148
+ msg[4],
149
+ msg.length,
150
+ ]
151
+ })
152
+ .catch(function(msg) {
153
+ // console.log('selectGfs catch', msg)
154
+ rt = msg.toString()
155
+ })
156
+
157
+ assert.strict.deepStrictEqual(rt, rr)
158
+ })
159
+
160
+ }
161
+
162
+
163
+ //delGfs
164
+ if (true) {
165
+
166
+ let rr = { n: 1, nDeleted: 1, ok: 1 }
167
+
168
+ it(`should get ${JSON.stringify(rr)} for delGfs`, async function() {
169
+
170
+ let rt = null
171
+ await w.delGfs(gi.id)
172
+ .then(function(msg) {
173
+ // console.log('delGfs then', msg)
174
+ // delGfs { n: 1, nDeleted: 1, ok: 1 }
175
+ rt = msg
176
+ })
177
+ .catch(function(msg) {
178
+ // console.log('delGfs catch', msg)
179
+ rt = msg.toString()
180
+ })
181
+
182
+ assert.strict.deepStrictEqual(rt, rr)
183
+ })
184
+
185
+ }
186
+
187
+
188
+ })
package/test/all.test.mjs DELETED
@@ -1,10 +0,0 @@
1
- import assert from 'assert'
2
-
3
-
4
- describe('all', function() {
5
-
6
- it('test in localhost', function() {
7
- assert.strict.deepEqual(1, 1)
8
- })
9
-
10
- })