From cfd434239f76aecb65a06006979af95897f227b7 Mon Sep 17 00:00:00 2001 From: Abhijit Date: Wed, 24 Oct 2018 11:53:00 +0530 Subject: [PATCH 1/2] Fixed conflict for createdAt and __v when doing update or findOneAndUpdate --- index.js | 119 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 77 insertions(+), 42 deletions(-) diff --git a/index.js b/index.js index 0dd97b6..1a39167 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,7 @@ function timestampsPlugin(schema, options) { if (typeof options === 'object') { if (typeof options.updatedAt === 'string') { - updatedAt = options.updatedAt; + updatedAt = options.updatedAt; } else if (typeof options.updatedAt === 'object') { updatedAtOpts = defaults(options.updatedAt, { name: updatedAt, @@ -45,13 +45,13 @@ function timestampsPlugin(schema, options) { schema.add(dataObj); } if (schema.virtual(createdAt).get) { - schema.virtual(createdAt) - .get( function () { - if (this["_" + createdAt]) return this["_" + createdAt]; - return this["_" + createdAt] = this._id.getTimestamp(); - }); + schema.virtual(createdAt) + .get(function () { + if (this["_" + createdAt]) return this["_" + createdAt]; + return this["_" + createdAt] = this._id.getTimestamp(); + }); } - schema.pre('save', function(next) { + schema.pre('save', function (next) { if (this.isNew) { var newDate = new Date; if (createdAt) this[createdAt] = newDate; @@ -69,7 +69,7 @@ function timestampsPlugin(schema, options) { if (dataObj[createdAt] || dataObj[updatedAt]) { schema.add(dataObj); } - schema.pre('save', function(next) { + schema.pre('save', function (next) { if (!this[createdAt]) { var newDate = new Date; if (createdAt) this[createdAt] = newDate; @@ -81,50 +81,85 @@ function timestampsPlugin(schema, options) { }); } - schema.pre('findOneAndUpdate', function(next) { - if (this.op === 'findOneAndUpdate') { - var newDate = new Date; - this._update = this._update || {}; - if (createdAt) { - if (this._update[createdAt]) { - delete this._update[createdAt]; + schema.pre('findOneAndUpdate', function (next) { + if (this.op === 'findOneAndUpdate') { + var newDate = new Date; + this._update = this._update || {}; + this._update['$setOnInsert'] = this._update['$setOnInsert'] || {}; + + if (createdAt) { + if (this._update[createdAt]) { + delete this._update[createdAt]; + } + + if (this._update["$set"] && this._update["$set"][createdAt]) { + delete this._update["$set"][createdAt]; + } + + this._update['$setOnInsert'][createdAt] = newDate; + } + + if (this._update.hasOwnProperty("__v")) { + this._update['$setOnInsert'].__v = this._update.__v || 0; + delete this.update.__v; } - this._update['$setOnInsert'] = this._update['$setOnInsert'] || {}; - this._update['$setOnInsert'][createdAt] = newDate; - } - if (updatedAt) { - this._update[updatedAt] = newDate; + if (this._update["$set"] && this._update["$set"].hasOwnProperty("__v")) { + this._update['$setOnInsert'].__v = this._update["$set"].__v || 0; + delete this._update["$set"].__v; + } + + if (updatedAt) { + this._update[updatedAt] = newDate; + } + + console.log(this._update); } - } - next(); + next(); }); - schema.pre('update', function(next) { - if (this.op === 'update') { - var newDate = new Date; - this._update = this._update || {}; - if (createdAt) { - if (this._update[createdAt]) { - delete this._update[createdAt]; - } + schema.pre('update', function (next) { + if (this.op === 'update') { + var newDate = new Date; + this._update = this._update || {}; this._update['$setOnInsert'] = this._update['$setOnInsert'] || {}; - this._update['$setOnInsert'][createdAt] = newDate; - } - if (updatedAt) { - this._update[updatedAt] = newDate; + + if (createdAt) { + if (this._update[createdAt]) { + delete this._update[createdAt]; + } + + if (this._update["$set"] && this._update["$set"][createdAt]) { + delete this._update["$set"][createdAt]; + } + + this._update['$setOnInsert'][createdAt] = newDate; + } + + if (this._update.hasOwnProperty("__v")) { + this._update['$setOnInsert'].__v = this._update.__v || 0; + delete this.update.__v; + } + + if (this._update["$set"] && this._update["$set"].hasOwnProperty("__v")) { + this._update['$setOnInsert'].__v = this._update["$set"].__v || 0; + delete this._update["$set"].__v; + } + + if (updatedAt) { + this._update[updatedAt] = newDate; + } } - } - next(); + next(); }); - if(!schema.methods.hasOwnProperty('touch') && updatedAt) - schema.methods.touch = function(callback){ - this[updatedAt] = new Date; - this.save(callback); - } + if (!schema.methods.hasOwnProperty('touch') && updatedAt) + schema.methods.touch = function (callback) { + this[updatedAt] = new Date; + this.save(callback); + } } -module.exports = timestampsPlugin; +module.exports = timestampsPlugin; \ No newline at end of file From b315e22e685f8a77dee268fa200b1f87702ebb29 Mon Sep 17 00:00:00 2001 From: Abhijit Date: Wed, 24 Oct 2018 14:47:41 +0530 Subject: [PATCH 2/2] minor bug fix --- index.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 1a39167..4c7b2cd 100644 --- a/index.js +++ b/index.js @@ -95,13 +95,13 @@ function timestampsPlugin(schema, options) { if (this._update["$set"] && this._update["$set"][createdAt]) { delete this._update["$set"][createdAt]; } - + this._update['$setOnInsert'][createdAt] = newDate; } - + if (this._update.hasOwnProperty("__v")) { this._update['$setOnInsert'].__v = this._update.__v || 0; - delete this.update.__v; + delete this._update.__v; } if (this._update["$set"] && this._update["$set"].hasOwnProperty("__v")) { @@ -113,7 +113,6 @@ function timestampsPlugin(schema, options) { this._update[updatedAt] = newDate; } - console.log(this._update); } next(); }); @@ -133,13 +132,13 @@ function timestampsPlugin(schema, options) { if (this._update["$set"] && this._update["$set"][createdAt]) { delete this._update["$set"][createdAt]; } - + this._update['$setOnInsert'][createdAt] = newDate; } - + if (this._update.hasOwnProperty("__v")) { this._update['$setOnInsert'].__v = this._update.__v || 0; - delete this.update.__v; + delete this._update.__v; } if (this._update["$set"] && this._update["$set"].hasOwnProperty("__v")) {