20 lines
617 B
JavaScript
20 lines
617 B
JavaScript
const mongoose = require("mongoose");
|
|
const Schema = mongoose.Schema;
|
|
|
|
const PostSchema = new Schema({
|
|
title: { type: String, required: true },
|
|
date: { type: Date, required: true },
|
|
text: { type: String, required: true },
|
|
author: { type: String, required: true },
|
|
published: { type: Boolean, required: true },
|
|
_id: { type: mongoose.ObjectId, required: true },
|
|
});
|
|
|
|
// Virtual for message URL
|
|
PostSchema.virtual("url").get(function () {
|
|
// We don't use an arrow function as we'll need the this object
|
|
return `/api/${this._id}`;
|
|
});
|
|
|
|
// Export model
|
|
module.exports = mongoose.model("Post", PostSchema);
|