diff --git a/index.js b/index.js index 0dd97b6..4c7b2cd 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,84 @@ 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; + } + + 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; } - this._update['$setOnInsert'] = this._update['$setOnInsert'] || {}; - this._update['$setOnInsert'][createdAt] = newDate; - } - if (updatedAt) { - this._update[updatedAt] = newDate; } - } - 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