32 lines
1.1 KiB
JavaScript
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
|