database-connector 2.4.12 → 2.4.13

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.
@@ -27,10 +27,9 @@ const recordSubscriptionHistory = async (subscriptionDoc) => {
27
27
 
28
28
  const historySnapshot = buildSubscriptionHistorySnapshot(subscriptionDoc);
29
29
 
30
- // Use Model.updateOne instead of doc.updateOne to avoid triggering middleware
31
- await mongoose.model('Subscription').updateOne(
32
- { _id: subscriptionDoc._id },
33
- { $push: { subscriptionsHistory: historySnapshot } }
30
+ await subscriptionDoc.updateOne(
31
+ { $push: { subscriptionsHistory: historySnapshot } },
32
+ { $locals: { skipHistory: true } }
34
33
  );
35
34
  };
36
35
 
@@ -428,60 +427,28 @@ const subscriptionSchema = new mongoose.Schema(
428
427
  { toJSON: { virtuals: true } }
429
428
  );
430
429
 
431
- // Pre-save middleware to check skipHistory flag
432
- subscriptionSchema.pre('save', function (next) {
433
- // Store the skipHistory flag on the document itself
434
- if (this.$locals && this.$locals.skipHistory) {
435
- this._skipHistory = true;
436
- }
437
- next();
438
- });
439
-
440
430
  // Auto-record history snapshots to embedded subscriptionsHistory array
441
431
  subscriptionSchema.post('save', async function (doc, next) {
442
- // Check if we should skip history
443
- if (doc._skipHistory) {
444
- delete doc._skipHistory; // Clean up the flag
445
- return next();
446
- }
447
-
432
+ // For save operations, check the $locals and save options where custom options are stored
433
+ if (doc?.$locals?.skipHistory) return next();
434
+ if (this?.$locals?.skipHistory) return next();
435
+ if (this?.$__?.saveOptions?.skipHistory) return next();
448
436
  await recordSubscriptionHistory(doc);
449
437
  next();
450
438
  });
451
439
 
452
- // Pre-middleware for findOneAndUpdate to capture options
453
- subscriptionSchema.pre('findOneAndUpdate', function (next) {
454
- if (this.getOptions && this.getOptions().skipHistory) {
455
- this._skipHistory = true;
456
- }
457
- next();
458
- });
459
-
460
- subscriptionSchema.post('findOneAndUpdate', async function (doc) {
461
- if (this._skipHistory) return;
462
-
463
- // Get the updated document
464
- const updatedDoc = doc || await this.model.findOne(this.getQuery());
465
- if (updatedDoc) {
466
- await recordSubscriptionHistory(updatedDoc);
467
- }
468
- });
469
-
470
- // Pre-middleware for updateOne to capture options
471
- subscriptionSchema.pre('updateOne', function (next) {
472
- if (this.getOptions && this.getOptions().skipHistory) {
473
- this._skipHistory = true;
474
- }
475
- next();
440
+ subscriptionSchema.post('findOneAndUpdate', async function () {
441
+ const options = this.getOptions ? this.getOptions() : this.options;
442
+ if (options?.skipHistory || options?.$locals?.skipHistory || this?.$locals?.skipHistory) return;
443
+ const updatedDoc = await this.model.findOne(this.getQuery());
444
+ await recordSubscriptionHistory(updatedDoc);
476
445
  });
477
446
 
478
447
  subscriptionSchema.post('updateOne', async function () {
479
- if (this._skipHistory) return;
480
-
448
+ const options = this.getOptions ? this.getOptions() : this.options;
449
+ if (options?.skipHistory || options?.$locals?.skipHistory || this?.$locals?.skipHistory) return;
481
450
  const updatedDoc = await this.model.findOne(this.getQuery());
482
- if (updatedDoc) {
483
- await recordSubscriptionHistory(updatedDoc);
484
- }
451
+ await recordSubscriptionHistory(updatedDoc);
485
452
  });
486
453
 
487
454
  // Indexes for performance optimization
@@ -496,4 +463,4 @@ subscriptionSchema.index({ sellerId: 1, status: 1 });
496
463
  subscriptionSchema.index({ status: 1, endDate: 1 });
497
464
  subscriptionSchema.index({ stores: 1 });
498
465
 
499
- module.exports = mongoose.model('Subscription', subscriptionSchema);
466
+ module.exports = mongoose.model('Subscription', subscriptionSchema);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "database-connector",
3
- "version": "2.4.12",
3
+ "version": "2.4.13",
4
4
  "description": "MongoDB models package with Mongoose schemas for e-commerce applications. Includes User, Product, Store, Order and more with built-in validation and virtual properties.",
5
5
  "main": "models/index.js",
6
6
  "scripts": {