Mongoose Model pattern to follow?

39 views
Skip to first unread message

Mike Fielden Jr.

unread,
Apr 15, 2014, 12:22:25 AM4/15/14
to mongoo...@googlegroups.com, Collin Estes
I am working on my first production level mongo/mongoose app for a client.

I am looking for a best practices so to speak about defining mongoose models' instance and static methods.
Is it best practice to return promise objects from your static methods? How about any query that uses either instance or static return promises or no?

For example: 

var orderSchema = new Schema({
 number
: { type: String, required: true },
 client
: { type: Schema.ObjectId, ref: 'Client' }
});


// define the model's statics
orderSchema
.statics = {
 getAll
: function () {
 
var dfd =  q.defer();


 
this.find({})
 
.exec(function (err, orders) {
 
if (err || !orders) {
 dfd
.reject(err);
 
}


 dfd
.resolve(orders);
 
});


 
return dfd.promise();
 
}
};

Reply all
Reply to author
Forward
0 new messages