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.
@@ -55,7 +55,10 @@ import omit from 'lodash-es/omit.js'
55
55
  import size from 'lodash-es/size.js'
56
56
  import genPm from 'wsemi/src/genPm.mjs'
57
57
  import genID from 'wsemi/src/genID.mjs'
58
+ import isestr from 'wsemi/src/isestr.mjs'
58
59
  import isarr from 'wsemi/src/isarr.mjs'
60
+ import isearr from 'wsemi/src/isearr.mjs'
61
+ import iseobj from 'wsemi/src/iseobj.mjs'
59
62
  import pmSeries from 'wsemi/src/pmSeries.mjs'
60
63
 
61
64
 
@@ -143,6 +146,7 @@ function WOrmMongodb(opt = {}) {
143
146
  }
144
147
  finally {
145
148
  await client.close()
149
+ client = null
146
150
  }
147
151
 
148
152
  if (isErr) {
@@ -162,6 +166,15 @@ function WOrmMongodb(opt = {}) {
162
166
  async function insert(data) {
163
167
  let isErr = false
164
168
 
169
+ //check
170
+ if (!iseobj(data) && !isearr(data)) {
171
+ return {
172
+ n: 0,
173
+ nInserted: 0,
174
+ ok: 1,
175
+ }
176
+ }
177
+
165
178
  //cloneDeep
166
179
  data = cloneDeep(data)
167
180
 
@@ -185,7 +198,7 @@ function WOrmMongodb(opt = {}) {
185
198
 
186
199
  //check id
187
200
  data = map(data, function(v) {
188
- if (!v.id) {
201
+ if (!isestr(v.id)) {
189
202
  v.id = genID()
190
203
  }
191
204
  return v
@@ -223,6 +236,7 @@ function WOrmMongodb(opt = {}) {
223
236
  }
224
237
  finally {
225
238
  await client.close()
239
+ client = null
226
240
  }
227
241
 
228
242
  if (isErr) {
@@ -239,18 +253,21 @@ function WOrmMongodb(opt = {}) {
239
253
  * @param {Object|Array} data 輸入數據物件或陣列
240
254
  * @param {Object} [option={}] 輸入設定物件,預設為{}
241
255
  * @param {boolean} [option.autoInsert=true] 輸入是否於儲存時發現原本無數據,則自動改以插入處理,預設為true
242
- * @param {boolean} [option.atomic=false] 輸入是否於儲存時採用上鎖,避免同時操作互改問題,預設為false
243
256
  * @returns {Promise} 回傳Promise,resolve回傳儲存結果,reject回傳錯誤訊息
244
257
  */
245
258
  async function save(data, option = {}) {
246
259
  let isErr = false
247
260
 
261
+ //check
262
+ if (!iseobj(data) && !isearr(data)) {
263
+ return []
264
+ }
265
+
248
266
  //cloneDeep
249
267
  data = cloneDeep(data)
250
268
 
251
- //autoInsert, atomic
269
+ //autoInsert
252
270
  let autoInsert = get(option, 'autoInsert', true)
253
- let atomic = get(option, 'atomic', false)
254
271
 
255
272
  //client
256
273
  // let client = await MongoClient.connect(opt.url, optMGConn)
@@ -270,15 +287,13 @@ function WOrmMongodb(opt = {}) {
270
287
  data = [data]
271
288
  }
272
289
 
273
- //oper
274
- let oper = null
275
- if (atomic) {
276
- oper = 'findOneAndUpdate'
277
- }
278
- else {
279
- oper = 'updateOne'
280
- }
281
- // console.log('oper', oper)
290
+ //check id
291
+ data = map(data, function(v) {
292
+ if (!isestr(v.id)) {
293
+ v.id = genID()
294
+ }
295
+ return v
296
+ })
282
297
 
283
298
  //pmSeries
284
299
  res = await pmSeries(data, async(v) => {
@@ -287,16 +302,10 @@ function WOrmMongodb(opt = {}) {
287
302
  let rest = null
288
303
 
289
304
  //oper
290
- rest = await collection[oper]({ id: v.id }, { $set: v })
305
+ rest = await collection.findOneAndUpdate({ id: v.id }, { $set: v })
291
306
  // console.log('rest', rest)
292
307
 
293
- if (rest) {
294
- // rest {
295
- // _id: new ObjectId('66002783fdb8a009c825f716'),
296
- // id: 'id-peter',
297
- // name: 'peter',
298
- // value: 123
299
- // }
308
+ if (iseobj(rest)) {
300
309
  rest = {
301
310
  n: 1,
302
311
  nModified: 1,
@@ -311,36 +320,6 @@ function WOrmMongodb(opt = {}) {
311
320
  }
312
321
  }
313
322
 
314
- // //check
315
- // if (get(rest, 'lastErrorObject')) { //findOneAndUpdate
316
- // // console.log('rest.lastErrorObject', rest.lastErrorObject)
317
- // rest = {
318
- // // lastErrorObject: { n: 1, updatedExisting: true },
319
- // // value: {
320
- // // _id: new ObjectId("615c18bb6e5db9935b10d88e"),
321
- // // id: 'id-rosemary',
322
- // // name: 'rosemary',
323
- // // value: 123.456
324
- // // },
325
- // // ok: 1
326
- // n: get(rest, 'lastErrorObject.n', -1),
327
- // nModified: get(rest, 'lastErrorObject.updatedExisting', false) ? 1 : 0,
328
- // ok: 1,
329
- // }
330
- // }
331
- // else { //updateOne
332
- // rest = {
333
- // // acknowledged: true,
334
- // // modifiedCount: 1,
335
- // // upsertedId: null,
336
- // // upsertedCount: 0,
337
- // // matchedCount: 1
338
- // n: get(rest, 'matchedCount', -1),
339
- // nModified: get(rest, 'modifiedCount', -1),
340
- // ok: get(rest, 'acknowledged', false) ? 1 : 0,
341
- // }
342
- // }
343
-
344
323
  //autoInsert
345
324
  if (autoInsert && rest.n === 0) {
346
325
  rest = await insert(v)
@@ -359,6 +338,7 @@ function WOrmMongodb(opt = {}) {
359
338
  }
360
339
  finally {
361
340
  await client.close()
341
+ client = null
362
342
  }
363
343
 
364
344
  if (isErr) {
@@ -378,6 +358,11 @@ function WOrmMongodb(opt = {}) {
378
358
  async function del(data) {
379
359
  let isErr = false
380
360
 
361
+ //check
362
+ if (!iseobj(data) && !isearr(data)) {
363
+ return []
364
+ }
365
+
381
366
  //cloneDeep
382
367
  data = cloneDeep(data)
383
368
 
@@ -428,6 +413,7 @@ function WOrmMongodb(opt = {}) {
428
413
  }
429
414
  finally {
430
415
  await client.close()
416
+ client = null
431
417
  }
432
418
 
433
419
  if (isErr) {
@@ -480,8 +466,10 @@ function WOrmMongodb(opt = {}) {
480
466
  }
481
467
  finally {
482
468
  await client.close()
469
+ client = null
483
470
  }
484
471
 
472
+
485
473
  if (isErr) {
486
474
  return Promise.reject(res)
487
475
  }
@@ -567,6 +555,7 @@ function WOrmMongodb(opt = {}) {
567
555
  }
568
556
  finally {
569
557
  await client.close()
558
+ client = null
570
559
  }
571
560
 
572
561
  if (isErr) {
@@ -650,6 +639,7 @@ function WOrmMongodb(opt = {}) {
650
639
  }
651
640
  finally {
652
641
  await client.close()
642
+ client = null
653
643
  }
654
644
 
655
645
  if (isErr) {
@@ -753,6 +743,7 @@ function WOrmMongodb(opt = {}) {
753
743
  }
754
744
  finally {
755
745
  await client.close()
746
+ client = null
756
747
  }
757
748
 
758
749
  if (isErr) {
@@ -854,6 +845,7 @@ function WOrmMongodb(opt = {}) {
854
845
  }
855
846
  finally {
856
847
  await client.close()
848
+ client = null
857
849
  }
858
850
 
859
851
  if (isErr) {
@@ -894,7 +886,7 @@ export default WOrmMongodb
894
886
  <br class="clear">
895
887
 
896
888
  <footer>
897
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Sun Mar 24 2024 21:44:00 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
889
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Sun May 11 2025 15:22:28 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
898
890
  </footer>
899
891
 
900
892
  <script>prettyPrint();</script>
package/docs/index.html CHANGED
@@ -71,7 +71,7 @@
71
71
  <br class="clear">
72
72
 
73
73
  <footer>
74
- Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Sun Mar 24 2024 21:44:00 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
74
+ Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.2</a> on Sun May 11 2025 15:22:28 GMT+0800 (台北標準時間) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
75
75
  </footer>
76
76
 
77
77
  <script>prettyPrint();</script>
package/g-basic.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import wo from './src/WOrmMongodb.mjs'
2
- //import wo from './dist/w-orm-mongodb.umd.js'
1
+ import WOrm from './src/WOrmMongodb.mjs'
2
+ //import WOrm from './dist/w-orm-mongodb.umd.js'
3
3
 
4
4
 
5
5
  let opt = {
@@ -43,19 +43,16 @@ let rsm = [
43
43
 
44
44
  async function test() {
45
45
 
46
-
47
- //w
48
- let w = wo(opt)
49
-
46
+ //wo
47
+ let wo = WOrm(opt)
50
48
 
51
49
  //on
52
- w.on('change', function(mode, data, res) {
50
+ wo.on('change', function(mode, data, res) {
53
51
  console.log('change', mode)
54
52
  })
55
53
 
56
-
57
54
  //delAll
58
- await w.delAll()
55
+ await wo.delAll()
59
56
  .then(function(msg) {
60
57
  console.log('delAll then', msg)
61
58
  })
@@ -63,9 +60,8 @@ async function test() {
63
60
  console.log('delAll catch', msg)
64
61
  })
65
62
 
66
-
67
63
  //insert
68
- await w.insert(rs)
64
+ await wo.insert(rs)
69
65
  .then(function(msg) {
70
66
  console.log('insert then', msg)
71
67
  })
@@ -73,9 +69,8 @@ async function test() {
73
69
  console.log('insert catch', msg)
74
70
  })
75
71
 
76
-
77
72
  //save
78
- await w.save(rsm, { autoInsert: false, atomic: true })
73
+ await wo.save(rsm, { autoInsert: false })
79
74
  .then(function(msg) {
80
75
  console.log('save then', msg)
81
76
  })
@@ -83,42 +78,35 @@ async function test() {
83
78
  console.log('save catch', msg)
84
79
  })
85
80
 
86
-
87
81
  //select all
88
- let ss = await w.select()
82
+ let ss = await wo.select()
89
83
  console.log('select all', ss)
90
84
 
91
-
92
85
  //select
93
- let so = await w.select({ id: 'id-rosemary' })
86
+ let so = await wo.select({ id: 'id-rosemary' })
94
87
  console.log('select', so)
95
88
 
96
-
97
89
  //select by $and, $gt, $lt
98
- let spa = await w.select({ '$and': [{ value: { '$gt': 123 } }, { value: { '$lt': 200 } }] })
90
+ let spa = await wo.select({ '$and': [{ value: { '$gt': 123 } }, { value: { '$lt': 200 } }] })
99
91
  console.log('select by $and, $gt, $lt', spa)
100
92
 
101
-
102
93
  //select by $or, $gte, $lte
103
- let spb = await w.select({ '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 200 } }] })
94
+ let spb = await wo.select({ '$or': [{ value: { '$lte': -1 } }, { value: { '$gte': 200 } }] })
104
95
  console.log('select by $or, $gte, $lte', spb)
105
96
 
106
-
107
97
  //select by $or, $and, $ne, $in, $nin
108
- let spc = 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 } }] }] })
98
+ let spc = 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 } }] }] })
109
99
  console.log('select by $or, $and, $ne, $in, $nin', spc)
110
100
 
111
-
112
101
  //select by regex
113
- let sr = await w.select({ name: { $regex: 'PeT', $options: '$i' } })
102
+ let sr = await wo.select({ name: { $regex: 'PeT', $options: '$i' } })
114
103
  console.log('selectReg', sr)
115
104
 
116
-
117
105
  //del
118
106
  let d = ss.filter(function(v) {
119
107
  return v.name === 'kettle'
120
108
  })
121
- await w.del(d)
109
+ await wo.del(d)
122
110
  .then(function(msg) {
123
111
  console.log('del then', msg)
124
112
  })
@@ -126,7 +114,6 @@ async function test() {
126
114
  console.log('del catch', msg)
127
115
  })
128
116
 
129
-
130
117
  }
131
118
  test()
132
119
  // change delAll
@@ -173,4 +160,4 @@ test()
173
160
  // change del
174
161
  // del then [ { n: 1, nDeleted: 1, ok: 1 } ]
175
162
 
176
- //node --experimental-modules --es-module-specifier-resolution=node g-basic.mjs
163
+ //node g-basic.mjs
package/g-gfs.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import path from 'path'
2
2
  import fs from 'fs'
3
- import wo from './src/WOrmMongodb.mjs'
4
- //import wo from './dist/w-orm-mongodb.umd.js'
3
+ import WOrm from './src/WOrmMongodb.mjs'
4
+ //import WOrm from './dist/w-orm-mongodb.umd.js'
5
5
 
6
6
 
7
7
  let opt = {
@@ -12,40 +12,34 @@ let opt = {
12
12
 
13
13
  async function test() {
14
14
 
15
-
16
- //w
17
- let w = wo(opt)
18
-
15
+ //wo
16
+ let wo = WOrm(opt)
19
17
 
20
18
  //on
21
- w.on('change', function(mode, data, res) {
19
+ wo.on('change', function(mode, data, res) {
22
20
  console.log('change', mode)
23
21
  })
24
22
 
25
-
26
23
  //fn_in, fn_out
27
24
  let fn_in = path.resolve('../', './_data', 'data(in).dat')
28
25
  let fn_out = path.resolve('../', './_data', 'data(out).dat')
29
26
  // console.log('fn_in', fn_in)
30
27
  // console.log('fn_out', fn_out)
31
28
 
32
-
33
29
  //unlinkSync
34
30
  try {
35
31
  fs.unlinkSync(fn_out)
36
32
  }
37
33
  catch (err) {}
38
34
 
39
-
40
35
  //u8a
41
36
  let b = await fs.readFileSync(fn_in)
42
37
  let u8a = new Uint8Array(b)
43
38
  // let u8a = new Uint8Array([66, 97, 115]) //Uint8Array data from nodejs or browser
44
39
  console.log('u8a', u8a)
45
40
 
46
-
47
41
  //delAllGfs
48
- await w.delAllGfs()
42
+ await wo.delAllGfs()
49
43
  .then(function(msg) {
50
44
  console.log('delAllGfs then', msg)
51
45
  })
@@ -53,14 +47,12 @@ async function test() {
53
47
  console.log('delAllGfs catch', msg)
54
48
  })
55
49
 
56
-
57
50
  //insertGfs
58
- let gi = await w.insertGfs(u8a)
51
+ let gi = await wo.insertGfs(u8a)
59
52
  console.log('insertGfs', gi)
60
53
 
61
-
62
54
  //selectGfs
63
- let gs = await w.selectGfs(gi.id)
55
+ let gs = await wo.selectGfs(gi.id)
64
56
  console.log('selectGfs', gs)
65
57
  console.log('gs[0]', gs[0], gs[0] === 0)
66
58
  console.log('gs[1]', gs[1], gs[1] === 0)
@@ -70,12 +62,10 @@ async function test() {
70
62
  console.log('gs.length', gs.length, gs.length === 47381362)
71
63
  fs.writeFileSync(fn_out, gs)
72
64
 
73
-
74
65
  //delGfs
75
- let gd = await w.delGfs(gi.id)
66
+ let gd = await wo.delGfs(gi.id)
76
67
  console.log('delGfs', gd)
77
68
 
78
-
79
69
  }
80
70
  test()
81
71
  // u8a Uint8Array(47381362) [
@@ -109,4 +99,4 @@ test()
109
99
  // change delGfs
110
100
  // delGfs { n: 1, nDeleted: 1, ok: 1 }
111
101
 
112
- //node --experimental-modules --es-module-specifier-resolution=node g-gfs.mjs
102
+ //node g-gfs.mjs
package/package.json CHANGED
@@ -1,18 +1,18 @@
1
1
  {
2
2
  "name": "w-orm-mongodb",
3
- "version": "1.1.31",
3
+ "version": "1.1.33",
4
4
  "main": "dist/w-orm-mongodb.umd.js",
5
5
  "dependencies": {
6
- "mongodb": "^6.5.0",
6
+ "mongodb": "^6.16.0",
7
7
  "saslprep": "^1.0.3",
8
8
  "lodash-es": "^4.17.21",
9
- "wsemi": "^1.7.49"
9
+ "wsemi": "^1.7.90"
10
10
  },
11
11
  "devDependencies": {
12
- "w-package-tools": "^1.0.75"
12
+ "w-package-tools": "^1.0.90"
13
13
  },
14
14
  "scripts": {
15
- "test": "mocha --parallel --timeout 60000 --experimental-modules --es-module-specifier-resolution=node",
15
+ "test": "mocha --parallel --timeout 60000",
16
16
  "deploy": "gh-pages -d docs"
17
17
  },
18
18
  "repository": {
package/script.txt CHANGED
@@ -1,10 +1,10 @@
1
- #node --experimental-modules --es-module-specifier-resolution=node toolg/addVersion.mjs
2
- node --experimental-modules --es-module-specifier-resolution=node toolg/modifyReadme.mjs
1
+ #node toolg/addVersion.mjs
2
+ node toolg/modifyReadme.mjs
3
3
 
4
- node --experimental-modules --es-module-specifier-resolution=node toolg/cleanFolder.mjs
4
+ node toolg/cleanFolder.mjs
5
5
  ./node_modules/.bin/jsdoc -c .jsdoc
6
6
 
7
- node --experimental-modules --es-module-specifier-resolution=node toolg/gDistRollup.mjs
7
+ node toolg/gDistRollup.mjs
8
8
 
9
9
  git add . -A
10
10
  git commit -m 'modify: '