Skip to content

Instantly share code, notes, and snippets.

@slayful
Created July 28, 2018 11:28
Creation method in Mongoose.js, alternative to custom constructor or hooks
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Article Schema
*/
var ArticleSchema = new Schema({
created: {
type: Date,
default: Date.now
},
title: {
type: String,
default: '',
trim: true,
required: 'Title cannot be blank'
},
url: {
type: String,
trim: true,
default: '',
required: 'Url cannot be blank'
},
content: {
type: String,
default: '',
trim: true
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
mongoose.model('Article', ArticleSchema);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment