Take Advantage of Mongoose Schemas
Most of modelParser could be moved into the schemas as virtual properties.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;
var renderMd = require('../libs/markdown').renderMd;
var CommentSchema = new Schema({
author: {type: ObjectId, ref: 'User'},
contentRaw: {type: String},
});
CommentSchema.plugin(require('./mixins/trashable'));
CommentSchema.plugin(require('mongoose-findorcreate'));
CommentSchema.virtual('contentRendered')
.get(function(){
return renderMd(this.contentRaw);
});
CommentSchema.static.populateQuery = function(query) {
return query.populate('author', 'name role');
};
var CommentModel = mongoose.model('Comment', CommentSchema);
module.exports = CommentModel;
Schema's can have mixins called plugins, so we can define common properties/methods in one place too.
Starting to implement this: https://github.com/Zren/OpenUserJS.org/commit/4f33df7cbc0b47ba8957bf6a11f3961a90ff9cb9
@Zren With your announcement you should also "assign" yourself to this issue... I'll assign it for you this time but this will help automatically mediate further pr's. It only takes a couple of seconds to do. :)
Deassigned you for the time being... if mongoose ever has the ability to use .toObject with exporting the methods or creating setters with export... then we can move some of the code into the schema's... can't do it without it though at this point in time.