OpenUserJS.org icon indicating copy to clipboard operation
OpenUserJS.org copied to clipboard

Take Advantage of Mongoose Schemas

Open Zren opened this issue 11 years ago • 3 comments

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.

Zren avatar Jun 15 '14 09:06 Zren

Starting to implement this: https://github.com/Zren/OpenUserJS.org/commit/4f33df7cbc0b47ba8957bf6a11f3961a90ff9cb9

Zren avatar Jul 01 '14 06:07 Zren

@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. :)

Martii avatar Jul 02 '14 19:07 Martii

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.

Martii avatar Aug 07 '15 06:08 Martii