Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import mongoose, { Schema, Document, Model, Types } from "mongoose";
export interface IReaction extends Document {
postId: Types.ObjectId;
userId: Types.ObjectId;
type: string;
createdAt: Date;
}
const ReactionSchema = new Schema<IReaction>(
{
postId: {
type: Schema.Types.ObjectId,
ref: "Post",
required: [true, "Post is required"],
index: true,
},
userId: {
type: Schema.Types.ObjectId,
ref: "User",
required: [true, "User is required"],
index: true,
},
type: {
type: String,
required: [true, "Reaction type is required"],
enum: ["helpful", "support", "insightful", "inspiring"],
default: "helpful",
},
},
{
timestamps: { createdAt: true, updatedAt: false },
}
);
// Unique compound index to prevent duplicate reactions
ReactionSchema.index({ postId: 1, userId: 1 }, { unique: true });
const Reaction: Model<IReaction> =
mongoose.models.Reaction || mongoose.model<IReaction>("Reaction", ReactionSchema);
export default Reaction;
|