OpenUserJS / OpenUserJS.org

The home of FOSS user scripts.
https://openuserjs.org/
GNU General Public License v3.0
847 stars 301 forks source link

Take Advantage of Mongoose Schemas #170

Open Zren opened 10 years ago

Zren commented 10 years ago

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 commented 10 years ago

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

Martii commented 10 years ago

@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 commented 9 years ago

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.