Skip to content

Instantly share code, notes, and snippets.

@timoxley
Created October 25, 2011 22:41
Show Gist options
  • Save timoxley/1314581 to your computer and use it in GitHub Desktop.
Save timoxley/1314581 to your computer and use it in GitHub Desktop.
mongoose static/instance methods
/************ CLASS METHODS ***********************/
// TODO: Document use of promise here
StreamSchema.static('findActivities', function (streamId, callback) {
var promise = new mongoose.Promise;
if (callback) promise.addBack(callback);
require('./activity').find({'streamId': streamId}, promise.resolve.bind(promise))
return promise
})
// TODO: Document use of promise and Step here
StreamSchema.static('findWithActivities', function (streamId, callback) {
var promise = new mongoose.Promise;
if (callback) promise.addBack(callback);
var stream, activities
Step(
function() {
Stream.findById(streamId, this.parallel())
Stream.findActivities(streamId, this.parallel())
},
function(err, stream, activities) {
var result = stream.toJSON()
result.activities = activities
promise.resolve.bind(promise)(null, result)
}
)
return promise
})
/************ INSTANCE METHODS ***********************/
// Really strange bug, for some reason Activity is an empty
// object when this gets called if:
// test_activity is run before test_stream ?
// and the dropCollections helper is run :/
// also using alternative methods defn:
// Schema.methods.method = func... no work.
StreamSchema.method('findActivities', function (callback) {
var promise = new mongoose.Promise;
if (callback) promise.addBack(callback);
// TODO: remove require statement below. See comment above.
require('./activity').find({'streamId': this._id}, promise.resolve.bind(promise))
return promise
})
Stream = mongoose.model('Stream', StreamSchema)
module.exports = Stream
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment