Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed conflict for createdAt and __v when doing update or findOneAnd… #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 76 additions & 42 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;