nodeclub/models/topic.js

41 lines
1.1 KiB
JavaScript

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var config = require('../config');
var _ = require('lodash');
var TopicSchema = new Schema({
title: { type: String },
content: { type: String },
author_id: { type: ObjectId },
top: { type: Boolean, default: false },
reply_count: { type: Number, default: 0 },
visit_count: { type: Number, default: 0 },
collect_count: { type: Number, default: 0 },
create_at: { type: Date, default: Date.now },
update_at: { type: Date, default: Date.now },
last_reply: { type: ObjectId },
last_reply_at: { type: Date, default: Date.now },
content_is_html: { type: Boolean },
tab: {type: String},
});
TopicSchema.index({create_at: -1});
TopicSchema.index({top: -1, last_reply_at: -1});
TopicSchema.index({last_reply_at: -1});
TopicSchema.index({author_id: 1, create_at: -1});
TopicSchema.virtual('tabName').get(function () {
var tab = this.tab;
var pair = _.find(config.tabs, function (_pair) {
return _pair[0] === tab;
});
if (pair) {
return pair[1];
} else {
return '';
}
});
mongoose.model('Topic', TopicSchema);