forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReel.js
More file actions
57 lines (48 loc) · 1.58 KB
/
Copy pathReel.js
File metadata and controls
57 lines (48 loc) · 1.58 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// models/Reel.js
import mongoose from "mongoose";
const { Schema, Types, model } = mongoose;
const commentSchema = new Schema(
{
_id: { type: Schema.Types.ObjectId, auto: true },
user: { type: Schema.Types.ObjectId, ref: "User", required: true },
text: { type: String, required: true, trim: true, maxlength: 500 },
createdAt: { type: Date, default: Date.now },
},
{ _id: true }
);
const reelSchema = new Schema(
{
description: { type: String, required: true, trim: true, maxlength: 2000 },
category: { type: String, trim: true },
tags: [{ type: String, trim: true }],
video: { type: String, required: true },
videoPublicId: { type: String },
thumbnail: { type: String },
duration: { type: Number },
createdBy: {
type: Schema.Types.ObjectId,
ref: "User",
required: true,
},
likes: [{ type: Schema.Types.ObjectId, ref: "User" }],
loves: [{ type: Schema.Types.ObjectId, ref: "User" }],
comments: [commentSchema],
shareCount: { type: Number, default: 0 },
viewCount: { type: Number, default: 0 },
},
{ timestamps: true }
);
reelSchema.index({ createdAt: -1 });
reelSchema.index({ description: "text" });
reelSchema.virtual("likeCount").get(function () {
return this.likes?.length || 0;
});
reelSchema.virtual("loveCount").get(function () {
return this.loves?.length || 0;
});
reelSchema.virtual("commentCount").get(function () {
return this.comments?.length || 0;
});
reelSchema.set("toJSON", { virtuals: true });
reelSchema.set("toObject", { virtuals: true });
export default model("Reel", reelSchema);