brainlet/models/Category.js

22 lines
No EOL
580 B
JavaScript
Executable file

const mongoose = require("mongoose");
const Post = require("./Post");
const User = require("./User");
const categorySchema = new mongoose.Schema({
title: String,
creator: {type: mongoose.Schema.Types.ObjectId, ref: "User"},
posts: [Post.schema]
});
categorySchema.method("getPublicObject", function() {
return {
title: this.title,
creator: this.populate("creator", User.getPulicFields()).creator,
posts: this.posts,
_id: this._id
};
});
const Category = mongoose.model("Category", categorySchema);
module.exports = Category;