Files
modstagram-back/models/LikeModel.js
payacom 3f416a7a60 full
2026-08-31 20:45:41 +03:30

32 lines
1.1 KiB
JavaScript

const mongoose = require('mongoose')
const timestamp = require('mongoose-timestamp')
const likeSchema = new mongoose.Schema({
postId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Post',
required: true
},
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
likes: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}]
})
// postController.js هر بار جزئیاتِ یه پست رو می‌گیره LikeModel.findOne({userId, postId})
// می‌زنه — بدونِ ایندکس این یه scan کاملِ کالکشنه. عمداً unique نیست — دیتای فعلیِ
// production حداقل یه گروهِ تکراریِ {postId,userId} داره؛ ساختِ ایندکسِ unique روی
// این دیتا شکست می‌خوره. بعد از پاک‌سازیِ دستیِ تکراری‌ها می‌شه unique هم اضافه کرد
likeSchema.index({ postId: 1, userId: 1 })
// تعریف مدل LikeModel
likeSchema.plugin(timestamp)
const LikeModel = mongoose.model('Like', likeSchema)
module.exports = LikeModel