diff --git a/Makefile b/Makefile index 7066043..25bec4f 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ EXPRESSO = support/expresso/bin/expresso -I lib --serial -TESTS = tests/*.test.js +TESTS = tests/useTimestamps.test.js test: @$(EXPRESSO) $(TESTS) $(TEST_FLAGS) diff --git a/lib/plugins/useTimestamps.js b/lib/plugins/useTimestamps.js index f61d0e2..d179ced 100644 --- a/lib/plugins/useTimestamps.js +++ b/lib/plugins/useTimestamps.js @@ -3,31 +3,34 @@ var mongoose = require('mongoose') , BinaryParser = mongoose.mongo.BinaryParser; exports.useTimestamps = function (schema, options) { - schema.date('updatedAt'); - if (schema.oid && schema.oid instanceof ObjectID) { - schema - .virtual('createdAt') - .get( function () { - if (this._.createdAt) return this._.createdAt; - var unixtime = BinaryParser.decodeInt(oid.id.slice(0, 4), 32, true, true); - return this._.createdAt = new Date(unixtime * 1000); - }) - .pre('save', function () { - if (this.isNew) { - this.updatedAt = this.createdAt; - } else { - this.updatedAt = new Date; - } + if (schema.path('_id')) { + schema.add({ + updatedAt: Date + }); + schema.virtual('createdAt') + .get( function () { + if (this._createdAt) return this._createdAt; + var unixtime = BinaryParser.decodeInt(this._id.id.slice(0, 4), 32, true, true); + return this._createdAt = new Date(unixtime * 1000); }); + schema.pre('save', function () { + if (this.isNew) { + this.updatedAt = this.createdAt; + } else { + this.updatedAt = new Date; + } + }); } else { - schema - .date('createdAt') - .pre('save', function () { - if (!this.createdAt) { - this.createdAt = this.updatedAt = new Date; - } else { - this.updatedAt = new Date; - } - }); + schema.add({ + createdAt: Date + , updatedAt: Date + }); + schema.pre('save', function () { + if (!this.createdAt) { + this.createdAt = this.updatedAt = new Date; + } else { + this.updatedAt = new Date; + } + }); } }; diff --git a/lib/types/counter.js b/lib/types/counter.js deleted file mode 100644 index 4bdbdde..0000000 --- a/lib/types/counter.js +++ /dev/null @@ -1,50 +0,0 @@ -module.exports.loadType = function (mongoose) { - mongoose.type('counter') - .extend('number') - .default(0) - .setup( function (key, path) { /** @scope is the Schema instance **/ - var schema = this; - ['incr', 'decr'].forEach( function (methodToAdd) { - var method = function (path, delta, fn) { /** @scope is a Document instance **/ - if (this.isNew || !this.id) throw new Error("You can only increment keys of documents that already have been persisted to mongodb."); - var arg1type = typeof arguments[1]; - if (arg1type === "undefined") { // incr(path) - delta = 1; - } else if (arg1type === "function") { // incr(path, fn) - fn = delta; - delta = 1; - } // Else we had invoked: incr(path, delta[, fn]); // i.e. arg1type === "number" - var mongooseDoc = this, - incObj = {}, fieldsObj = {}; - switch (methodToAdd) { - case ('incr'): - incObj[path] = delta; - break; - case('decr'): - incObj[path] = -1 * delta; - break; - } - fieldsObj[path] = 1; - this._collection.findAndModify({'_id': this._id}, [], {'$inc': incObj}, {'new': true, fields: fieldsObj}, function (err, doc) { - if (doc) mongooseDoc.set(path, doc[path], true); - if (fn) fn(err, mongooseDoc); - }); - }; - - // Add the `incr` and `decr` methods - if (!schema._hooks[methodToAdd]) { - schema.hook(methodToAdd, method); - } - - // If path is -- e.g., 'vote' -- - // then add the `incrVote` and `decrVote` methods - var ucasePath = path[0].toUpperCase() + path.substring(1); - schema.hook(methodToAdd + ucasePath, function (path) { - // Currying ftw! - return function (delta, fn) { /** @scope is a Document instance **/ - method.call(this, path, delta, fn); - }; - }(path)); - }); - }); -}; diff --git a/package.json b/package.json index 14d5ecf..7e5f4e3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mongoose-types" , "description": "More types for mongoose" -, "version": "0.0.2" +, "version": "1.0.0" , "author": "Brian Noguchi" , "dependencies": { "mongoose": "1.0.0"} , "keywords": [ "mongoose", "mongo", "mongodb", "types" ] diff --git a/tests/useTimestamps.test.js b/tests/useTimestamps.test.js index 5266235..0531afc 100644 --- a/tests/useTimestamps.test.js +++ b/tests/useTimestamps.test.js @@ -1,31 +1,35 @@ -var assert = require('assert') - , mongoose = require('mongoose').new() - , document = mongoose.define - , db = mongoose.connect('mongodb://localhost/mongoose_types_tests') +require('should'); +var mongoose = require('mongoose') + , Schema = mongoose.Schema + , db = mongoose.createConnection('mongodb://localhost/mongoose_types_tests') , loadTypes = require("../").loadTypes , useTimestamps = require("../").useTimestamps; -document('TimeCop') - .email('email') - .plugin(useTimestamps); +mongoose.plugin(useTimestamps); + +var TimeCopSchema = new Schema({ + email: String +}); + +mongoose.model('TimeCop', TimeCopSchema); +var TimeCop; module.exports = { - before: function(assert, done){ - db.on('connect', function () { - mongoose.TimeCop.remove({}, function () { - done(); - }); + before: function(done){ + TimeCop = db.model('TimeCop', TimeCopSchema); + TimeCop.remove({}, function () { + done(); }); }, - 'createdAt and updatedAt should be set to the same value on creation': function (assert, done) { - mongoose.TimeCop.create({ email: 'brian@brian.com' }, function (err, cop) { - assert.ok(cop.createdAt instanceof Date); - assert.equal(cop.updatedAt, cop.createdAt); + 'createdAt and updatedAt should be set to the same value on creation': function (done) { + TimeCop.create({ email: 'brian@brian.com' }, function (err, cop) { + cop.createdAt.should.be.an.instanceof(Date); + cop.updatedAt.should.be.an.instanceof(Date); done(); }); }, - 'updatedAt should be later than createdAt upon updating': function (assert, done) { - mongoose.TimeCop.first({email: 'brian@brian.com'}, function (err, found) { + 'updatedAt should be later than createdAt upon updating': function (done) { + TimeCop.findOne({email: 'brian@brian.com'}, function (err, found) { found.email = 'jeanclaude@vandamme.com'; setTimeout( function () { found.save( function (err, updated) { @@ -36,6 +40,6 @@ module.exports = { }); }, teardown: function(){ - mongoose.disconnect(); + db.close(); } };