Skip to content

Instantly share code, notes, and snippets.

@dprea
Last active January 17, 2016 15:39
Show Gist options
  • Save dprea/bd7a444d3019ecf55ed0 to your computer and use it in GitHub Desktop.
Save dprea/bd7a444d3019ecf55ed0 to your computer and use it in GitHub Desktop.
MEAN.js 0.4.2 - Mongoose.js Discriminator Schemas
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Article Schema
*/
// options creates the keys for each schema
var options = { discriminatorKey: 'kind' };
var ArticleSchema = new Schema({
created: {
type: Date,
default: Date.now
},
title: {
type: String,
default: '',
trim: true,
required: 'Title cannot be blank'
},
content: {
type: String,
default: '',
trim: true
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
}, options);
var Article = mongoose.model('Article', ArticleSchema);
// Special Article Schema
// USAGE: modules/articles/server/controllers/article.server.controller.js
// USAGE: In Dependencies: SpecialArticle = mongoose.model('SpecialArticle'),
var SpecialArticleSchema = new Schema({
specialreason: {
type: String,
default: 'Its SPECIAL!'
}
},
options);
var SpecialArticle = Article.discriminator('SpecialArticle', SpecialArticleSchema);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment