const mongoose = require("mongoose"); const Schema = mongoose.Schema; const MessageSchema = new Schema({ to: { type: String, required: true }, from: { type: String, required: true }, date: { type: Date, required: true }, updated: { type: Date, required: true }, text: { type: String, required: true }, _id: { type: mongoose.ObjectId, required: true }, }); // Virtual for message URL MessageSchema.virtual("url").get(function () { // We don't use an arrow function as we'll need the this object return `/msg/${this._id}`; }); // Export model module.exports = mongoose.model("Message", MessageSchema);