Skip to content

Commit

Permalink
Update Model.aggregate behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Nov 25, 2013
1 parent d34fcda commit 65dfa6f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
20 changes: 15 additions & 5 deletions lib/Instance.js
Expand Up @@ -70,10 +70,10 @@ function Instance(collection, doc, options, isNew) {
if (targetProperty === '_id' || targetProperty === "id") continue;

(function (sourceProperty) {
if (k === '_id')
if (sourceProperty === '_id' || sourceProperty === 'id')
Object.defineProperty($, targetProperty, {
get: function () {
return newDoc[sourceProperty];
return newDoc['_id'];
},
enumerable: true
});
Expand All @@ -100,8 +100,10 @@ function Instance(collection, doc, options, isNew) {
}

for (k in options.methods) {
if (k === '_id' || k === "id") continue;

(function (sourceProperty) {
Object.defineProperty($, k, {
Object.defineProperty($, sourceProperty, {
value: function () {
return options.methods[sourceProperty].apply($, arguments);
}
Expand All @@ -110,8 +112,10 @@ function Instance(collection, doc, options, isNew) {
}

for (k in options.virtuals) {
if (k === '_id' || k === "id") continue;

(function (sourceProperty) {
Object.defineProperty($, k, {
Object.defineProperty($, sourceProperty, {
get: function () {
return options.virtuals[sourceProperty].call($);
}
Expand Down Expand Up @@ -211,8 +215,13 @@ function Instance(collection, doc, options, isNew) {

Object.defineProperty($, 'update', {
value: function (callback) {
/// <signature>
/// <summary>Updates this object from the database, bringing it up to date</summary>
/// </signature>
/// <signature>
/// <summary>Updates this object from the database, bringing it up to date</summary>
/// <param name="callback" type="Function">A function to be called once the update is complete</param>
/// </signature>

var conditions = { _id: $.id };

Expand All @@ -224,7 +233,8 @@ function Instance(collection, doc, options, isNew) {
transform.down(options.transforms, updated);

oldDoc = newDoc = updated;
return callback(null, $);

if(callback) return callback(null, $);
});
}
});
Expand Down
8 changes: 5 additions & 3 deletions lib/Model.js
Expand Up @@ -227,12 +227,14 @@ Model.prototype.remove = function (conditions, callback) {
this.collection.remove(conditions, { w: callback ? 1 : 0 }, callback);
};

Model.prototype.aggregate = function (chain, callback) {
Model.prototype.aggregate = function () {
/// <signature>
/// <summary>Allows you to execute aggregation queries using MongoDB's aggregation framework</summary>
/// <param name="chain" type="Array" elementType="Object">The aggregation toolchain to run</param>
/// <param name="callback" type="Function">A function to be called once aggregation has been completed</param>

this.collection.aggregate(chain, callback);
/// </signature>

this.collection.aggregate.apply(this.collection, arguments);
};

Model.prototype.ensureIndex = function (spec, options, callback) {
Expand Down

0 comments on commit 65dfa6f

Please sign in to comment.