w-orm-mongodb 1.1.37 → 1.1.39

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,348 +0,0 @@
1
- import assert from 'assert'
2
- import WOrm from '../src/WOrmMongodb.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: 'mongodb://username:password@127.0.0.1:27017',
20
- db: 'worm',
21
- cl: 'users',
22
- }
23
-
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)
63
- })
64
-
65
- //delAll
66
- rt = null
67
- vans[1] = {
68
- nDeleted: 2,
69
- ok: 1,
70
- }
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
- })
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
303
-
304
- })
305
-
306
- it(`should get ${JSON.stringify(vans[1])} for delAll`, async function() {
307
- assert.strict.deepStrictEqual(vget[1], vans[1])
308
- })
309
-
310
- it(`should get ${JSON.stringify(vans[2])} for insert`, async function() {
311
- assert.strict.deepStrictEqual(vget[2], vans[2])
312
- })
313
-
314
- it(`should get ${JSON.stringify(vans[3])} for save`, async function() {
315
- assert.strict.deepStrictEqual(vget[3], vans[3])
316
- })
317
-
318
- it(`should get ${JSON.stringify(vans[4])} for select all`, async function() {
319
- assert.strict.deepStrictEqual(vget[4], vans[4])
320
- })
321
-
322
- it(`should get ${JSON.stringify(vans[5])} for select`, async function() {
323
- assert.strict.deepStrictEqual(vget[5], vans[5])
324
- })
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
- })
329
-
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
- })
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
- })
337
-
338
- it(`should get ${JSON.stringify(vans[9])} for select by regex`, async function() {
339
- assert.strict.deepStrictEqual(vget[9], vans[9])
340
- })
341
-
342
- it(`should get ${JSON.stringify(vans[10])} for del`, async function() {
343
- assert.strict.deepStrictEqual(vget[10], vans[10])
344
- })
345
-
346
- })
347
- }
348
-
package/test/gfs.test.mjs DELETED
@@ -1,172 +0,0 @@
1
- import assert from 'assert'
2
- import path from 'path'
3
- import fs from 'fs'
4
- import WOrm from '../src/WOrmMongodb.mjs'
5
-
6
-
7
- function isWindows() {
8
- return process.platform === 'win32'
9
- }
10
-
11
-
12
- if (isWindows()) {
13
- describe('gfs', function() {
14
- let rt = null
15
- let vans = {}
16
- let vget = {}
17
-
18
- before(async function () {
19
-
20
- let opt = {
21
- url: 'mongodb://username:password@127.0.0.1:27017',
22
- db: 'worm',
23
- cl: 'usersGfs',
24
- }
25
-
26
- //wo
27
- let wo = WOrm(opt)
28
-
29
- //on
30
- wo.on('change', function(mode, data, res) {
31
- // console.log('change', mode)
32
- })
33
-
34
- //fn_in, fn_out
35
- let fn_in = path.resolve('../', './_data', 'data(in).dat')
36
- let fn_out = path.resolve('../', './_data', 'data(out).dat')
37
- // console.log('fn_in', fn_in)
38
- // console.log('fn_out', fn_out)
39
-
40
- //unlinkSync
41
- try {
42
- fs.unlinkSync(fn_out)
43
- }
44
- catch (err) {}
45
-
46
- //u8a
47
- let b = await fs.readFileSync(fn_in)
48
- let u8a = new Uint8Array(b)
49
- // let u8a = new Uint8Array([66, 97, 115]) //Uint8Array data from nodejs or browser
50
- // console.log('u8a', u8a)
51
-
52
- //delAllGfs
53
- rt = null
54
- vans[1] = { n: 0, ok: 1 }
55
- await wo.delAllGfs()
56
- .then(function(msg) {
57
- // console.log('delAllGfs then', msg)
58
- // delAllGfs then { n: 0, ok: 1 }
59
- rt = msg
60
- })
61
- .catch(function(msg) {
62
- // console.log('delAllGfs catch', msg)
63
- rt = msg.toString()
64
- })
65
- vget[1] = rt
66
-
67
- //insertGfs
68
- let gi = null
69
- rt = null
70
- vans[2] = {
71
- n: 1,
72
- ok: 1,
73
- // id: {random id},
74
- }
75
- await wo.insertGfs(u8a)
76
- .then(function(msg) {
77
- // console.log('insertGfs then', msg)
78
- // insertGfs { n: 1, ok: 1, id: {random id} }
79
- gi = msg
80
- rt = {
81
- n: msg.n,
82
- ok: msg.ok,
83
- }
84
- })
85
- .catch(function(msg) {
86
- // console.log('insertGfs catch', msg)
87
- rt = msg.toString()
88
- })
89
- vget[2] = rt
90
-
91
- //selectGfs
92
- rt = null
93
- vans[3] = [
94
- 0,
95
- 0,
96
- 0,
97
- 24,
98
- 102,
99
- 47381362,
100
- ]
101
- await wo.selectGfs(gi.id)
102
- .then(function(msg) {
103
- // console.log('selectGfs then', msg)
104
- // console.log('msg[0]', msg[0], msg[0] === 0)
105
- // console.log('msg[1]', msg[1], msg[1] === 0)
106
- // console.log('msg[2]', msg[2], msg[2] === 0)
107
- // console.log('msg[3]', msg[3], msg[3] === 24)
108
- // console.log('msg[4]', msg[4], msg[4] === 102)
109
- // console.log('msg.length', msg.length, msg.length === 47381362)
110
- // selectGfs Uint8Array(47381362) [
111
- // 0, 0, 0, 24, 102, 116, 121, 112, 109, 112, 52, 50,
112
- // 0, 0, 0, 0, 105, 115, 111, 109, 109, 112, 52, 50,
113
- // 0, 2, 14, 73, 109, 111, 111, 118, 0, 0, 0, 108,
114
- // 109, 118, 104, 100, 0, 0, 0, 0, 214, 15, 24, 167,
115
- // 214, 15, 24, 167, 0, 1, 95, 144, 1, 106, 95, 88,
116
- // 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
117
- // 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
118
- // 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
119
- // 0, 0, 0, 0,
120
- // ... 47381262 more items
121
- // ]
122
- // fs.writeFileSync(fn_out, msg)
123
- rt = [
124
- msg[0],
125
- msg[1],
126
- msg[2],
127
- msg[3],
128
- msg[4],
129
- msg.length,
130
- ]
131
- })
132
- .catch(function(msg) {
133
- // console.log('selectGfs catch', msg)
134
- rt = msg.toString()
135
- })
136
- vget[3] = rt
137
-
138
- //delGfs
139
- rt = null
140
- vans[4] = { n: 1, nDeleted: 1, ok: 1 }
141
- await wo.delGfs(gi.id)
142
- .then(function(msg) {
143
- // console.log('delGfs then', msg)
144
- // delGfs { n: 1, nDeleted: 1, ok: 1 }
145
- rt = msg
146
- })
147
- .catch(function(msg) {
148
- // console.log('delGfs catch', msg)
149
- rt = msg.toString()
150
- })
151
- vget[4] = rt
152
-
153
- })
154
-
155
- it(`should get ${JSON.stringify(vans[1])} for delAllGfs`, function() {
156
- assert.strict.deepStrictEqual(vget[1], vans[1])
157
- })
158
-
159
- it(`should get ${JSON.stringify(vans[2])} for insertGfs`, async function() {
160
- assert.strict.deepStrictEqual(vget[2], vans[2])
161
- })
162
-
163
- it(`should get ${JSON.stringify(vans[3])} for selectGfs`, async function() {
164
- assert.strict.deepStrictEqual(vget[3], vans[3])
165
- })
166
-
167
- it(`should get ${JSON.stringify(vans[4])} for delGfs`, async function() {
168
- assert.strict.deepStrictEqual(vget[4], vans[4])
169
- })
170
-
171
- })
172
- }