mongoose-timestamp
Advanced tools
Comparing version 0.1.1 to 0.2.0
54
index.js
@@ -8,19 +8,37 @@ /*! | ||
var BinaryParser = require('bson').BinaryParser; | ||
function timestampsPlugin(schema, options) { | ||
var updatedAt = 'updatedAt'; | ||
var createdAt = 'createdAt'; | ||
var updatedAtType = Date; | ||
var createdAtType = Date; | ||
if (typeof options === 'object') { | ||
if (typeof options.updatedAt === 'string') { | ||
updatedAt = options.updatedAt; | ||
} else if (typeof options.updatedAt === 'object') { | ||
updatedAt = options.updatedAt.name || updatedAt; | ||
updatedAtType = options.updatedAt.type || updatedAtType; | ||
} | ||
if (typeof options.createdAt === 'string') { | ||
createdAt = options.createdAt; | ||
} else if (typeof options.createdAt === 'object') { | ||
createdAt = options.createdAt.name || createdAt; | ||
createdAtType = options.createdAt.type || createdAtType; | ||
} | ||
} | ||
function timestampsPlugin(schema, options) { | ||
if (schema.path('_id')) { | ||
schema.add({ | ||
updatedAt: Date | ||
}); | ||
schema.virtual('createdAt') | ||
var dataObj = {}; | ||
dataObj[updatedAt] = updatedAtType; | ||
if (schema.path('createdAt')) { | ||
schema.add(dataObj); | ||
schema.virtual(createdAt) | ||
.get( function () { | ||
if (this._createdAt) return this._createdAt; | ||
return this._createdAt = this._id.getTimestamp(); | ||
if (this["_" + createdAt]) return this["_" + createdAt]; | ||
return this["_" + createdAt] = this._id.getTimestamp(); | ||
}); | ||
schema.pre('save', function (next) { | ||
if (this.isNew) { | ||
this.updatedAt = this.createdAt; | ||
this[updatedAt] = this[createdAt]; | ||
} else { | ||
this.updatedAt = new Date; | ||
this[updatedAt] = new Date; | ||
} | ||
@@ -30,11 +48,9 @@ next(); | ||
} else { | ||
schema.add({ | ||
createdAt: Date | ||
, updatedAt: Date | ||
}); | ||
dataObj[createdAt] = createdAtType; | ||
schema.add(dataObj); | ||
schema.pre('save', function (next) { | ||
if (!this.createdAt) { | ||
this.createdAt = this.updatedAt = new Date; | ||
if (!this[createdAt]) { | ||
this[createdAt] = this[updatedAt] = new Date; | ||
} else { | ||
this.updatedAt = new Date; | ||
this[updatedAt] = new Date; | ||
} | ||
@@ -46,2 +62,2 @@ next(); | ||
module.exports = timestampsPlugin; | ||
module.exports = timestampsPlugin; |
{ | ||
"name": "mongoose-timestamp" | ||
, "description": "Mongoose plugin that adds createdAt and updatedAt auto-assigned date properties" | ||
, "version": "0.1.1" | ||
, "description": "Mongoose plugin that adds createdAt and updatedAt auto-assigned date properties" | ||
, "version": "0.2.0" | ||
, "author": "Nicholas Penree <nick@penree.com>" | ||
@@ -6,0 +6,0 @@ , "keywords": ["mongodb", "mongoose", "plugin", "timestamps", "createdAt", "updatedAt"] |
@@ -18,6 +18,2 @@ | ||
var TimeCopSchema = new Schema({ | ||
email: String | ||
}); | ||
mongoose.plugin(timestamps); | ||
@@ -31,7 +27,2 @@ | ||
after(function(done) { | ||
mongoose.connection.db.dropDatabase() | ||
done(); | ||
}) | ||
describe('timestamps', function() { | ||
@@ -38,0 +29,0 @@ it('should be set to the same value on creation', function(done) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11977
9
210