@e22m4u/js-repository-mongodb-adapter 0.0.22 → 0.0.24

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e22m4u/js-repository-mongodb-adapter",
3
- "version": "0.0.22",
3
+ "version": "0.0.24",
4
4
  "description": "MongoDB адаптер для @e22m4u/js-repository",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -38,22 +38,22 @@
38
38
  "peerDependencies": {
39
39
  "@e22m4u/js-format": "*",
40
40
  "@e22m4u/js-service": "*",
41
- "@e22m4u/js-repository": "~0.0.36"
41
+ "@e22m4u/js-repository": "~0.0.41"
42
42
  },
43
43
  "devDependencies": {
44
- "@commitlint/cli": "^17.7.1",
45
- "@commitlint/config-conventional": "^17.7.0",
44
+ "@commitlint/cli": "^17.8.0",
45
+ "@commitlint/config-conventional": "^17.8.0",
46
46
  "c8": "^8.0.1",
47
- "chai": "^4.3.7",
47
+ "chai": "^4.3.10",
48
48
  "chai-as-promised": "^7.1.1",
49
49
  "chai-spies": "^1.0.0",
50
50
  "dotenv": "^16.3.1",
51
- "eslint": "^8.47.0",
51
+ "eslint": "^8.51.0",
52
52
  "eslint-config-prettier": "^9.0.0",
53
53
  "eslint-plugin-chai-expect": "^3.0.0",
54
- "eslint-plugin-mocha": "^10.1.0",
54
+ "eslint-plugin-mocha": "^10.2.0",
55
55
  "husky": "^8.0.3",
56
56
  "mocha": "^10.2.0",
57
- "prettier": "^3.0.1"
57
+ "prettier": "^3.0.3"
58
58
  }
59
59
  }
@@ -160,6 +160,20 @@ export class MongodbAdapter extends Adapter {
160
160
  return value;
161
161
  }
162
162
 
163
+ /**
164
+ * Coerce date.
165
+ *
166
+ * @param value
167
+ * @returns {Date|*}
168
+ * @private
169
+ */
170
+ _coerceDate(value) {
171
+ if (value == null) return value;
172
+ if (value instanceof Date) return value;
173
+ if (isIsoDate(value)) return new Date(value);
174
+ return value;
175
+ }
176
+
163
177
  /**
164
178
  * To database.
165
179
  *
@@ -401,6 +415,7 @@ export class MongodbAdapter extends Adapter {
401
415
  // string
402
416
  if (typeof cond === 'string') {
403
417
  query[key] = this._coerceId(cond);
418
+ query[key] = this._coerceDate(query[key]);
404
419
  return;
405
420
  }
406
421
  // ObjectId
@@ -413,27 +428,35 @@ export class MongodbAdapter extends Adapter {
413
428
  const opConds = [];
414
429
  // eq
415
430
  if ('eq' in cond) {
416
- opConds.push({$eq: this._coerceId(cond.eq)});
431
+ let eq = this._coerceId(cond.eq);
432
+ eq = this._coerceDate(eq);
433
+ opConds.push({$eq: eq});
417
434
  }
418
435
  // neq
419
436
  if ('neq' in cond) {
420
- opConds.push({$ne: this._coerceId(cond.neq)});
437
+ let neq = this._coerceId(cond.neq);
438
+ neq = this._coerceDate(neq);
439
+ opConds.push({$ne: neq});
421
440
  }
422
441
  // gt
423
442
  if ('gt' in cond) {
424
- opConds.push({$gt: cond.gt});
443
+ const gt = this._coerceDate(cond.gt);
444
+ opConds.push({$gt: gt});
425
445
  }
426
446
  // lt
427
447
  if ('lt' in cond) {
428
- opConds.push({$lt: cond.lt});
448
+ const lt = this._coerceDate(cond.lt);
449
+ opConds.push({$lt: lt});
429
450
  }
430
451
  // gte
431
452
  if ('gte' in cond) {
432
- opConds.push({$gte: cond.gte});
453
+ const gte = this._coerceDate(cond.gte);
454
+ opConds.push({$gte: gte});
433
455
  }
434
456
  // lte
435
457
  if ('lte' in cond) {
436
- opConds.push({$lte: cond.lte});
458
+ const lte = this._coerceDate(cond.lte);
459
+ opConds.push({$lte: lte});
437
460
  }
438
461
  // inq
439
462
  if ('inq' in cond) {
@@ -443,7 +466,12 @@ export class MongodbAdapter extends Adapter {
443
466
  'an Array of possible values',
444
467
  cond.inq,
445
468
  );
446
- opConds.push({$in: cond.inq.map(v => this._coerceId(v))});
469
+ const inq = cond.inq.map(v => {
470
+ v = this._coerceId(v);
471
+ v = this._coerceDate(v);
472
+ return v;
473
+ });
474
+ opConds.push({$in: inq});
447
475
  }
448
476
  // nin
449
477
  if ('nin' in cond) {
@@ -453,7 +481,12 @@ export class MongodbAdapter extends Adapter {
453
481
  'an Array of possible values',
454
482
  cond,
455
483
  );
456
- opConds.push({$nin: cond.nin.map(v => this._coerceId(v))});
484
+ const nin = cond.nin.map(v => {
485
+ v = this._coerceId(v);
486
+ v = this._coerceDate(v);
487
+ return v;
488
+ });
489
+ opConds.push({$nin: nin});
457
490
  }
458
491
  // between
459
492
  if ('between' in cond) {
@@ -463,7 +496,9 @@ export class MongodbAdapter extends Adapter {
463
496
  'an Array of 2 elements',
464
497
  cond.between,
465
498
  );
466
- opConds.push({$gte: cond.between[0], $lte: cond.between[1]});
499
+ const gte = this._coerceDate(cond.between[0]);
500
+ const lte = this._coerceDate(cond.between[1]);
501
+ opConds.push({$gte: gte, $lte: lte});
467
502
  }
468
503
  // exists
469
504
  if ('exists' in cond) {
@@ -615,6 +650,24 @@ export class MongodbAdapter extends Adapter {
615
650
  return this._fromDatabase(modelName, replacedData);
616
651
  }
617
652
 
653
+ /**
654
+ * Patch.
655
+ *
656
+ * @param {string} modelName
657
+ * @param {object} modelData
658
+ * @param {object|undefined} where
659
+ * @return {Promise<number>}
660
+ */
661
+ async patch(modelName, modelData, where = undefined) {
662
+ const idPropName = this._getIdPropName(modelName);
663
+ delete modelData[idPropName];
664
+ const query = this._buildQuery(modelName, where) || {};
665
+ const tableData = this._toDatabase(modelName, modelData);
666
+ const table = this._getCollection(modelName);
667
+ const {matchedCount} = await table.updateMany(query, {$set: tableData});
668
+ return matchedCount;
669
+ }
670
+
618
671
  /**
619
672
  * Patch by id.
620
673
  *