Skip to content

Commit

Permalink
Merge pull request #37 from linusbrolin/support-using-existing-fields
Browse files Browse the repository at this point in the history
Added support for using existing timestamp fields in the schema
  • Loading branch information
drudge authored Jan 12, 2017
2 parents 3ae7a97 + 02cd404 commit c75b365
Showing 1 changed file with 77 additions and 45 deletions.
122 changes: 77 additions & 45 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,62 +36,94 @@ function timestampsPlugin(schema, options) {
}
}

dataObj[updatedAt] = updatedAtOpts;
if (!schema.path(updatedAt) && updatedAt) {
dataObj[updatedAt] = updatedAtOpts;
}

if (schema.path(createdAt)) {
schema.add(dataObj);
schema.virtual(createdAt)
.get( function () {
if (this["_" + createdAt]) return this["_" + createdAt];
return this["_" + createdAt] = this._id.getTimestamp();
});
schema.pre('save', function (next) {
if (this.isNew) {
this[updatedAt] = this[createdAt];
} else if (this.isModified()) {
this[updatedAt] = new Date;
}
next();
});
if (!schema.path(updatedAt) && updatedAt) {
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.pre('save', function(next) {
if (this.isNew) {
var newDate = new Date;
if (createdAt) this[createdAt] = newDate;
if (updatedAt) this[updatedAt] = newDate;
} else if (this.isModified() && updatedAt) {
this[updatedAt] = new Date;
}
next();
});

} else {
dataObj[createdAt] = createdAtOpts;
schema.add(dataObj);
schema.pre('save', function (next) {
if (!this[createdAt]) {
this[createdAt] = this[updatedAt] = new Date;
} else if (this.isModified()) {
this[updatedAt] = new Date;
}
next();
});
if (createdAt) {
dataObj[createdAt] = createdAtOpts;
}
if (dataObj[createdAt] || dataObj[updatedAt]) {
schema.add(dataObj);
}
schema.pre('save', function(next) {
if (!this[createdAt]) {
var newDate = new Date;
if (createdAt) this[createdAt] = newDate;
if (updatedAt) this[updatedAt] = newDate;
} else if (this.isModified() && updatedAt) {
this[updatedAt] = new Date;
}
next();
});
}

schema.pre('findOneAndUpdate', function (next) {
if (this.op === 'findOneAndUpdate') {
this._update = this._update || {};
this._update[updatedAt] = new Date;
this._update['$setOnInsert'] = this._update['$setOnInsert'] || {};
this._update['$setOnInsert'][createdAt] = new Date;
}
next();
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];
}

this._update['$setOnInsert'] = this._update['$setOnInsert'] || {};
this._update['$setOnInsert'][createdAt] = newDate;
}
if (updatedAt) {
this._update[updatedAt] = newDate;
}
}
next();
});

schema.pre('update', function(next) {
if (this.op === 'update') {
this._update = this._update || {};
this._update[updatedAt] = new Date;
this._update['$setOnInsert'] = this._update['$setOnInsert'] || {};
this._update['$setOnInsert'][createdAt] = new Date;
}
next();
if (this.op === 'update') {
var newDate = new Date;
this._update = this._update || {};
if (createdAt) {
if (this._update[createdAt]) {
delete this._update[createdAt];
}

this._update['$setOnInsert'] = this._update['$setOnInsert'] || {};
this._update['$setOnInsert'][createdAt] = newDate;
}
if (updatedAt) {
this._update[updatedAt] = newDate;
}
}
next();
});

if(!schema.methods.hasOwnProperty('touch'))
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);
}

}

Expand Down

0 comments on commit c75b365

Please sign in to comment.