next/models/privMsg.ts

63 lines
1.2 KiB
TypeScript

import mongoose, { Schema, PopulatedDoc, Document, Model } from "mongoose";
import SequenceFactory from "mongoose-sequence";
import { hasMigrated } from "~/lib/dbconfig";
import { IUser } from "./user";
const AutoIncrement = SequenceFactory(mongoose);
export interface IPrivMsg {
_id: number;
from: PopulatedDoc<IUser & Document>;
to: PopulatedDoc<IUser & Document>;
subject: string;
content: string;
sentAt: Date;
read: boolean;
deletedBy: {
sender: boolean;
recipient: boolean;
};
}
const PMSchema = new mongoose.Schema<IPrivMsg>({
_id: { type: Number },
from: {
type: Number,
ref: "User",
},
to: {
type: Number,
ref: "User",
},
subject: { type: String },
content: { type: String },
sentAt: {
type: Date,
default: new Date(),
},
read: {
type: Boolean,
default: false,
},
deletedBy: {
sender: {
type: Boolean,
default: false,
},
recipient: {
type: Boolean,
default: false,
},
},
});
hasMigrated &&
!mongoose.models.PrivMsg &&
PMSchema.plugin(AutoIncrement, { id: "private_message" });
export const PrivMsg: Model<IPrivMsg> =
/* mongoose.models.PrivMsg || */ mongoose.model(
"PrivMsg",
PMSchema,
"private_messages",
);