Skip to content

Commit

Permalink
Added remove functionality to models.
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed May 22, 2010
1 parent 6a6a3b9 commit 7e99f31
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
35 changes: 35 additions & 0 deletions lib/mobius-js/db/mongodb.js
Expand Up @@ -261,6 +261,41 @@ MongoDBConnector.prototype.update = function(self, params, callback) {
}
exports.update = MongoDBConnector.prototype.update;

/**
* Remove a MongoDB document.
*
* @param {object} self reference to synchronous processing stack.
* @param {object} params parameters for creating new model.
* @param {function} callback callback for returning control to processing stack.
* @type void
* @public
*/
MongoDBConnector.prototype.remove = function(self, params, callback) {
// We must make sure that an operation completes after a set
// timeout to prevent a deadlock situation.
var success = false;
setTimeout(function() {
if (!success) {
self.reset();
setTimeout(function() {
self._processStack(self);
}, 10);
}
}, OPERATION_TIMEOUT);

self.db.collection(params['collectionName'], function(err, collection) {

var results = {};
// Insert the model.
collection.remove(params['restrict'], function(err, docs) {
success = true;
callback();
});

});
}
exports.remove = MongoDBConnector.prototype.remove;

/**
* Query for a MongoDB document.
*
Expand Down
30 changes: 29 additions & 1 deletion lib/mobius-js/model.js
Expand Up @@ -130,10 +130,38 @@ MobiusModel.prototype.update = function(restrictTemp, paramsTemp) {
params['restrict'] = restrictTemp;
params['collectionName'] = this.className;

// Build up the restrict structure for this update operation.
this.processingStack.update(params);
},

/**
* Remove an existing model.
*
* @param {object} restrictTemp Key value pairs representing the models to select.
* @throws ValidationException if model cannot be validated.
* @type void
* @public
*/
MobiusModel.prototype.remove = function(restrictTemp) {
// If restrictTemp is not indicated assume we are updating
// this specific instance of the model.
if (!restrictTemp) { // restrictTemp and paramsTemp should switch ordering.
restrictTemp = {};

// Map instance variables onto restrictTemp.
for (var key in this.constructor) {
if (key != 'constructor' && key != 'extend' && key != 'include') {
restrictTemp[key] = this[key];
}
}
}

params = {};
params['restrict'] = restrictTemp;
params['collectionName'] = this.className;

this.processingStack.remove(params);
},

/**
* Validate parameters.
*
Expand Down
16 changes: 16 additions & 0 deletions lib/mobius-js/processing-stack.js
Expand Up @@ -82,6 +82,22 @@ ProcessingStack.prototype.update = function(params, callback) {
}, params, callback]);
},


/**
* Called by a model to place a remove operation on the stack.
*
* @param {object} params key value pairs for creating a new model.
* @param {function} callback function to execute upon this operations completion.
* @type void
* @public
*/
ProcessingStack.prototype.remove = function(params, callback) {
callback = callback || function() {};
this.processingStack.push([function(self, params, callback) {
mongodb.remove(self, params, callback);
}, params, callback]);
},

/**
* Called by a model to place an operation on the stack which initializes model indexes.
*
Expand Down

0 comments on commit 7e99f31

Please sign in to comment.