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.
@@ -8,7 +8,10 @@ import omit from 'lodash-es/omit.js'
8
8
  import size from 'lodash-es/size.js'
9
9
  import genPm from 'wsemi/src/genPm.mjs'
10
10
  import genID from 'wsemi/src/genID.mjs'
11
+ import isestr from 'wsemi/src/isestr.mjs'
11
12
  import isarr from 'wsemi/src/isarr.mjs'
13
+ import isearr from 'wsemi/src/isearr.mjs'
14
+ import iseobj from 'wsemi/src/iseobj.mjs'
12
15
  import pmSeries from 'wsemi/src/pmSeries.mjs'
13
16
 
14
17
 
@@ -96,6 +99,7 @@ function WOrmMongodb(opt = {}) {
96
99
  }
97
100
  finally {
98
101
  await client.close()
102
+ client = null
99
103
  }
100
104
 
101
105
  if (isErr) {
@@ -115,6 +119,15 @@ function WOrmMongodb(opt = {}) {
115
119
  async function insert(data) {
116
120
  let isErr = false
117
121
 
122
+ //check
123
+ if (!iseobj(data) && !isearr(data)) {
124
+ return {
125
+ n: 0,
126
+ nInserted: 0,
127
+ ok: 1,
128
+ }
129
+ }
130
+
118
131
  //cloneDeep
119
132
  data = cloneDeep(data)
120
133
 
@@ -138,7 +151,7 @@ function WOrmMongodb(opt = {}) {
138
151
 
139
152
  //check id
140
153
  data = map(data, function(v) {
141
- if (!v.id) {
154
+ if (!isestr(v.id)) {
142
155
  v.id = genID()
143
156
  }
144
157
  return v
@@ -176,6 +189,7 @@ function WOrmMongodb(opt = {}) {
176
189
  }
177
190
  finally {
178
191
  await client.close()
192
+ client = null
179
193
  }
180
194
 
181
195
  if (isErr) {
@@ -192,18 +206,21 @@ function WOrmMongodb(opt = {}) {
192
206
  * @param {Object|Array} data 輸入數據物件或陣列
193
207
  * @param {Object} [option={}] 輸入設定物件,預設為{}
194
208
  * @param {boolean} [option.autoInsert=true] 輸入是否於儲存時發現原本無數據,則自動改以插入處理,預設為true
195
- * @param {boolean} [option.atomic=false] 輸入是否於儲存時採用上鎖,避免同時操作互改問題,預設為false
196
209
  * @returns {Promise} 回傳Promise,resolve回傳儲存結果,reject回傳錯誤訊息
197
210
  */
198
211
  async function save(data, option = {}) {
199
212
  let isErr = false
200
213
 
214
+ //check
215
+ if (!iseobj(data) && !isearr(data)) {
216
+ return []
217
+ }
218
+
201
219
  //cloneDeep
202
220
  data = cloneDeep(data)
203
221
 
204
- //autoInsert, atomic
222
+ //autoInsert
205
223
  let autoInsert = get(option, 'autoInsert', true)
206
- let atomic = get(option, 'atomic', false)
207
224
 
208
225
  //client
209
226
  // let client = await MongoClient.connect(opt.url, optMGConn)
@@ -223,15 +240,13 @@ function WOrmMongodb(opt = {}) {
223
240
  data = [data]
224
241
  }
225
242
 
226
- //oper
227
- let oper = null
228
- if (atomic) {
229
- oper = 'findOneAndUpdate'
230
- }
231
- else {
232
- oper = 'updateOne'
233
- }
234
- // console.log('oper', oper)
243
+ //check id
244
+ data = map(data, function(v) {
245
+ if (!isestr(v.id)) {
246
+ v.id = genID()
247
+ }
248
+ return v
249
+ })
235
250
 
236
251
  //pmSeries
237
252
  res = await pmSeries(data, async(v) => {
@@ -240,16 +255,10 @@ function WOrmMongodb(opt = {}) {
240
255
  let rest = null
241
256
 
242
257
  //oper
243
- rest = await collection[oper]({ id: v.id }, { $set: v })
258
+ rest = await collection.findOneAndUpdate({ id: v.id }, { $set: v })
244
259
  // console.log('rest', rest)
245
260
 
246
- if (rest) {
247
- // rest {
248
- // _id: new ObjectId('66002783fdb8a009c825f716'),
249
- // id: 'id-peter',
250
- // name: 'peter',
251
- // value: 123
252
- // }
261
+ if (iseobj(rest)) {
253
262
  rest = {
254
263
  n: 1,
255
264
  nModified: 1,
@@ -264,36 +273,6 @@ function WOrmMongodb(opt = {}) {
264
273
  }
265
274
  }
266
275
 
267
- // //check
268
- // if (get(rest, 'lastErrorObject')) { //findOneAndUpdate
269
- // // console.log('rest.lastErrorObject', rest.lastErrorObject)
270
- // rest = {
271
- // // lastErrorObject: { n: 1, updatedExisting: true },
272
- // // value: {
273
- // // _id: new ObjectId("615c18bb6e5db9935b10d88e"),
274
- // // id: 'id-rosemary',
275
- // // name: 'rosemary',
276
- // // value: 123.456
277
- // // },
278
- // // ok: 1
279
- // n: get(rest, 'lastErrorObject.n', -1),
280
- // nModified: get(rest, 'lastErrorObject.updatedExisting', false) ? 1 : 0,
281
- // ok: 1,
282
- // }
283
- // }
284
- // else { //updateOne
285
- // rest = {
286
- // // acknowledged: true,
287
- // // modifiedCount: 1,
288
- // // upsertedId: null,
289
- // // upsertedCount: 0,
290
- // // matchedCount: 1
291
- // n: get(rest, 'matchedCount', -1),
292
- // nModified: get(rest, 'modifiedCount', -1),
293
- // ok: get(rest, 'acknowledged', false) ? 1 : 0,
294
- // }
295
- // }
296
-
297
276
  //autoInsert
298
277
  if (autoInsert && rest.n === 0) {
299
278
  rest = await insert(v)
@@ -312,6 +291,7 @@ function WOrmMongodb(opt = {}) {
312
291
  }
313
292
  finally {
314
293
  await client.close()
294
+ client = null
315
295
  }
316
296
 
317
297
  if (isErr) {
@@ -331,6 +311,11 @@ function WOrmMongodb(opt = {}) {
331
311
  async function del(data) {
332
312
  let isErr = false
333
313
 
314
+ //check
315
+ if (!iseobj(data) && !isearr(data)) {
316
+ return []
317
+ }
318
+
334
319
  //cloneDeep
335
320
  data = cloneDeep(data)
336
321
 
@@ -381,6 +366,7 @@ function WOrmMongodb(opt = {}) {
381
366
  }
382
367
  finally {
383
368
  await client.close()
369
+ client = null
384
370
  }
385
371
 
386
372
  if (isErr) {
@@ -433,8 +419,10 @@ function WOrmMongodb(opt = {}) {
433
419
  }
434
420
  finally {
435
421
  await client.close()
422
+ client = null
436
423
  }
437
424
 
425
+
438
426
  if (isErr) {
439
427
  return Promise.reject(res)
440
428
  }
@@ -520,6 +508,7 @@ function WOrmMongodb(opt = {}) {
520
508
  }
521
509
  finally {
522
510
  await client.close()
511
+ client = null
523
512
  }
524
513
 
525
514
  if (isErr) {
@@ -603,6 +592,7 @@ function WOrmMongodb(opt = {}) {
603
592
  }
604
593
  finally {
605
594
  await client.close()
595
+ client = null
606
596
  }
607
597
 
608
598
  if (isErr) {
@@ -706,6 +696,7 @@ function WOrmMongodb(opt = {}) {
706
696
  }
707
697
  finally {
708
698
  await client.close()
699
+ client = null
709
700
  }
710
701
 
711
702
  if (isErr) {
@@ -807,6 +798,7 @@ function WOrmMongodb(opt = {}) {
807
798
  }
808
799
  finally {
809
800
  await client.close()
801
+ client = null
810
802
  }
811
803
 
812
804
  if (isErr) {