Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const bcrypt = require('bcryptjs');
- const mongoose = require('mongoose');
- mongoose.Promise = global.Promise;
- const userSchema = new mongoose.Schema({
- username: {type: String, required: true, unique: true},
- password: {type: String, required: true},
- firstName: {type: String, default: ''},
- lastName: {type: String, default: ''}
- });
- userSchema.methods.apiRepr = function () {
- return {
- id: this._id,
- username: this.username,
- firstName: this.firstName,
- lastName: this.lastName
- };
- };
- userSchema.methods.validatePassword = function(password) {
- return bcrypt.compare(password, this.password);
- };
- userSchema.statics.hashPassword = function(password) {
- return bcrypt.hash(password, 10);
- };
- //-----------------------------------------//
- const blogPostSchema = mongoose.Schema({
- author: {
- firstName: String,
- lastName: String
- },
- title: {type: String, required: true},
- content: {type: String},
- // created: {type: Date, default: Date.now}
- });
- blogPostSchema.virtual('authorName').get(function() {
- return `${this.author.firstName} ${this.author.lastName}`.trim();
- });
- blogPostSchema.methods.apiRepr = function() {
- return {
- id: this._id,
- author: this.authorName,
- content: this.content,
- title: this.title,
- created: this.created
- };
- };
- const UserModel = mongoose.model('User', userSchema);
- const BlogPost = mongoose.model('BlogPost', blogPostSchema, 'blog-posts');
- module.exports = {BlogPost, UserModel};
Add Comment
Please, Sign In to add comment