Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongoose-timestamp

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-timestamp - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

test/custom_names_only_test.js

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) {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc