frontend-app / server /model /ChannelModel.js
Keys
fixes
2ecbb66
raw
history blame contribute delete
872 Bytes
import mongoose from "mongoose";
const channelSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
members: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Users",
required: true,
},
],
admin: {
type: mongoose.Schema.Types.ObjectId,
ref: "Users",
required: true,
},
messages: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Messages",
required: false,
},
],
createdAt: {
type: Date,
default: Date.now,
},
updatedAt: {
type: Date,
default: Date.now,
},
});
channelSchema.pre("save", function (next) {
this.updatedAt = Date.now();
next();
});
channelSchema.pre("findOneAndUpdate", function (next) {
this.set({ updatedAt: Date.now() });
next();
});
const Channel = mongoose.model("Channels", channelSchema);
export default Channel;