Skip to content

Commit

Permalink
Adds modified and created dates to all collections automatically.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Mussell committed Oct 5, 2011
1 parent b7e38bf commit 0afcc6e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 51 deletions.
85 changes: 46 additions & 39 deletions lib/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ var Collection = LBBase.extend( function(collection, schema, reqData) {
var collectionObj = this;
this.presaveActions = [];

var that = this;
var self = this;

//Constructor for "model objects"
this.Model = function(obj) {
collectionObj.Model.schema['_id'] = {type:'String'};
collectionObj.Model.schema['createdDate'] = {type:'Date'};
collectionObj.Model.schema['modifiedDate'] = {type:'Date'};
for(var field in collectionObj.Model.schema) {
var that = this;
var self = this;
var type = collectionObj.Model.schema[field]['type'];
var converter = input.typeConverters[type];

Expand Down Expand Up @@ -58,53 +60,58 @@ var Collection = LBBase.extend( function(collection, schema, reqData) {
};

var insert = function(obj, callback) {
that.dbClient.insert(that.collection, obj.getDataObj()).next(function(done, results, err) {
self.dbClient.insert(self.collection, obj.getDataObj()).next(function(done, results, err) {
if(err) {
log.error(err.stack);
}
that.objectsWithResultArray(err, done, results, callback);
self.objectsWithResultArray(err, done, results, callback);
});
}

this.Model.schema = schema || {};

this.Model.prototype = {
save : function(callback) {
var that = this;
var self = this;
for(var i = 0; i < collectionObj.presaveActions.length; i++) {
collectionObj.presaveActions[i].call(that);
collectionObj.presaveActions[i].call(self);
}

if(!callback) { callback = function(){} };

that.fullValidate(function(valid) {
self.fullValidate(function(valid) {
if(valid) {
if(that.persisted) {
if(self.persisted) {
log.trace("UPDATING OBJECT")
collectionObj.update(that.getDataObj(), {'_id':mongo.getMongoId(that._id)}, function(results) {
that.dirty = false;
that.persisted = true;
if(callback) {
callback(results, null);
}
});
if(self.dirty) {
self.modifiedDate = new Date();
collectionObj.update(self.getDataObj(), {'_id':mongo.getMongoId(self._id)}, function(results) {
self.dirty = false;
self.persisted = true;
if(callback) {
callback(results, null);
}
});
}
} else {
insert(that, function(results) {
that.dirty = false;
that.persisted = true;
self.modifiedDate = new Date();
self.createdDate = new Date();
insert(self, function(results) {
self.dirty = false;
self.persisted = true;
if(callback) {
callback(results, null);
}
});
}
} else {
log.error(that.errors);
callback(null, that.errors);
log.error(self.errors);
callback(null, self.errors);
}
});
},
fullValidate : function(callback) {
var that = this;
var self = this;
if(this.validate()) {
log.trace('valid');
var sc = collectionObj.Model.schema;
Expand All @@ -117,9 +124,9 @@ var Collection = LBBase.extend( function(collection, schema, reqData) {
if(fieldDesc['unique'] && fieldDesc['unique'] === true) {
(function(fieldName) {
ah.next(function(done, arg, err) {
collectionObj.isUnique(that, fieldName, function(isUnique) {
collectionObj.isUnique(self, fieldName, function(isUnique) {
if(!isUnique) {
that.addError(fieldName + '.notUnique');
self.addError(fieldName + '.notUnique');
}
done(arg && isUnique);
})
Expand All @@ -130,7 +137,7 @@ var Collection = LBBase.extend( function(collection, schema, reqData) {
ah.next(function(done, arg, err) {
callback(arg);
if(arg) {
that.clearErrors();
self.clearErrors();
}
done(arg);
});
Expand All @@ -152,14 +159,14 @@ var Collection = LBBase.extend( function(collection, schema, reqData) {
return dataObj;
},
addDefaultSettersAndGetters : function(property, converter) {
var that = this;
that.__defineGetter__(property, function() {
return that['_' + property];
var self = this;
self.__defineGetter__(property, function() {
return self['_' + property];
});

that.__defineSetter__(property, function(val) {
that.dirty = true;
that['_' + property ] = converter(val);
self.__defineSetter__(property, function(val) {
self.dirty = true;
self['_' + property ] = converter(val);
});
},
addError : function(err) {
Expand Down Expand Up @@ -211,24 +218,24 @@ Collection.prototype.objectsWithResultArray = function(error, done, results, cal
}

Collection.prototype.findAll = function(callback){
var that = this;
that.dbClient.find(this.collection, {}, null).next( function(done, results, err) {
var self = this;
self.dbClient.find(this.collection, {}, null).next( function(done, results, err) {
if(err) {
log.error(err.stack);
}
that.objectsWithResultArray(err, done, results, callback);
self.objectsWithResultArray(err, done, results, callback);
});

}

Collection.prototype.findOne = function(search, callback){
var that = this;
var self = this;
this.dbClient.find(this.collection, search, {limit: 1}).next( function(done, result, err) {
var resultObj;
if(err) {
log.error(err.stack);
} else if(result.length === 1){
resultObj = new that.Model(result[0]);
resultObj = new self.Model(result[0]);
resultObj.dirty = false;
resultObj.persisted = true;
}
Expand All @@ -245,13 +252,13 @@ Collection.prototype.findOne = function(search, callback){
}

Collection.prototype.find = function(search, callback){
var that = this;
var self = this;

this.dbClient.find(this.collection, search, {}).next( function(done, result, err) {
if(err) {
log.error(err.stack);
}
that.objectsWithResultArray(err, done, result, callback);
self.objectsWithResultArray(err, done, result, callback);
});
}

Expand All @@ -278,15 +285,15 @@ Collection.prototype.isUnique = function(obj, field, callback) {
}

Collection.prototype.update = function(obj, selector, callback) {
var that = this;
var self = this;
this.dbClient.update(this.collection, selector, obj).next(function(done, result, err) {
log.trace('updating for selector ');
log.trace(selector);
if(err) {
log.error(err.stack);
}

//that.objectsWithResultArray(done, result, callback);
//self.objectsWithResultArray(done, result, callback);
if(callback) {
callback(result, err)
}
Expand Down
19 changes: 7 additions & 12 deletions tests/collectionTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var collectionTest = function() {
this.col = new Collection('testTable', {
post : {type : 'String'},
title : {type : 'String', required:true},
createdDate : {type : 'Date'},
updatedDate : {type : 'Date'},
published : {type : 'Bool', default : false},
tags : {type : 'Array'},
Expand All @@ -19,7 +18,6 @@ collectionTest.prototype.setUp = function() {
this.col = new Collection('testTable', {
post : {type : 'String'},
title : {type : 'String', required : true},
createdDate : {type : 'Date'},
updatedDate : {type : 'Date'},
published : {type : 'Bool', default : false},
tags : {type : 'Array'},
Expand All @@ -36,12 +34,10 @@ collectionTest.prototype.setUp = function() {
this.badSchema = new this.col.Model();
this.badSchema.post = 'blah blah blah';
this.badSchema.saId = '3';
this.badSchema.createdDate = 'May 12, 1984';

this.goodSchema = new this.col.Model();
this.goodSchema.post = 'test me awesome!';
this.goodSchema.saId = '3';
this.goodSchema.createdDate = 'May 12, 1984';
this.goodSchema.published = true;
this.goodSchema.title = 'my title';

Expand All @@ -57,7 +53,7 @@ collectionTest.prototype.testFullValidate = function() {
assert.ok(valid);
});

this.col.dbClient.insert('testTable', {post:'test post 3', createdDate:new Date(), published:true, title:'post 3 title', seo_url:'test'});
this.col.dbClient.insert('testTable', {post:'test post 3', published:true, title:'post 3 title', seo_url:'test'});
this.goodSchema.fullValidate(function(valid) {
assert.ok(!valid)
});
Expand All @@ -74,19 +70,18 @@ collectionTest.prototype.testSaveNew = function() {

collectionTest.prototype.testFind = function() {
var that = this;
that.col.dbClient.insert('testTable', {post:'test post 1', createdDate:new Date(), published:true, title:'post 1 title'});
that.col.dbClient.insert('testTable', {post:'test post 2', createdDate:new Date('5/12/1984'), published:true, title:'post 2 title'})
that.col.dbClient.insert('testTable', {post:'test post 1', published:true, title:'post 1 title'});
that.col.dbClient.insert('testTable', {post:'test post 2', published:true, title:'post 2 title'})

this.col.find({}, function(results) {
assert.strictEqual(results.length, 2, 'The result size was ' + results.length + ' should be 2');
assert.strictEqual(results[0].title, 'post 1 title', 'the title was incorrect');
assert.strictEqual(results[1].createdDate.toString(), new Date('5/12/1984').toString(), 'the date was not correct actual ' + results[1].createdDate + ' expected ' + new Date('5/12/1984'));
});
}

collectionTest.prototype.testRemove = function() {
this.col.dbClient.insert('testTable', {post:'test post 3', createdDate:new Date(), published:true, title:'post 3 title'});
this.col.dbClient.insert('testTable', {post:'test post 4', createdDate:new Date('5/12/1984'), published:true, title:'post 4 title'})
this.col.dbClient.insert('testTable', {post:'test post 3', published:true, title:'post 3 title'});
this.col.dbClient.insert('testTable', {post:'test post 4', published:true, title:'post 4 title'})

this.col.remove({post:'test post 3'}, function() {});

Expand All @@ -98,7 +93,7 @@ collectionTest.prototype.testRemove = function() {

collectionTest.prototype.testSaveExisting = function() {
var that = this;
this.col.dbClient.insert('testTable', {post:'test post 4', createdDate:new Date('5/12/1984'), published:true, title:'post 4 title'});
this.col.dbClient.insert('testTable', {post:'test post 4', published:true, title:'post 4 title'});

this.col.find({title:'post 4 title'}, function(results) {
console.log('found results');
Expand All @@ -125,7 +120,7 @@ collectionTest.prototype.testValidateSchema = function() {
collectionTest.prototype.done = function() {
var that = this;
setTimeout(function() {
that.col.dbClient.removeAll('testTable');
//that.col.dbClient.removeAll('testTable');
that.col.dbClient.closeConnection();
}, 1000);
}
Expand Down

0 comments on commit 0afcc6e

Please sign in to comment.