alfirin/sept-web-radio

View on GitHub
backend/models/article.js

Summary

Maintainability
A
1 hr
Test Coverage
'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

/**
 * Article Schema
 */
var ArticleSchema = new Schema({
  created: {
    type: Date,
    default: Date.now
  },
  title: {
    type: String,
    default: '',
    trim: true
  },
  content: {
    type: String,
    default: '',
    trim: true
  },
  user: {
    type: Schema.ObjectId,
    ref: 'User'
  }
});

/**
 * Validations
 */
ArticleSchema.path('title').validate(function (title) {
  return title.length;
}, 'Title cannot be blank');

/**
 * Statics
 */
ArticleSchema.statics = {
  load: function (id, cb) {
    this.findOne({
      _id: id
    }).populate('user', 'name username').exec(cb);
  }
};

mongoose.model('Article', ArticleSchema);