-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtweetModel.js
More file actions
50 lines (42 loc) · 1.06 KB
/
Copy pathtweetModel.js
File metadata and controls
50 lines (42 loc) · 1.06 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
const { Schema, models, model } = require('mongoose');
const User = require('./userModel');
const tweetSchema = new Schema({
tweet: {
type: String,
trim: true,
// required: true,
maxlength: 500,
},
user: { // <= postedBy
type: Schema.Types.ObjectId,
ref: 'User',
required: true,
},
replyTo: { // tweet._id
type: Schema.Types.ObjectId,
ref: 'Tweet',
},
retweet: { // tweet._id
type: Schema.Types.ObjectId,
ref: 'Tweet',
},
retweetUsers: [{ // user._id
type: Schema.Types.ObjectId,
ref: 'User',
}],
pinned: {
type: Boolean,
default: false
},
likes: [{ // all the users likes the tweet: by clicking the heart of the tweet
type: Schema.Types.ObjectId,
ref: 'User',
}],
}, { timestamps: true })
tweetSchema.post('save', async function (doc) {
await this.populate('user replyTo')
// --- Why not it work here, but works on POST handler
// await User.populate(doc, 'replayTo.user')
})
const Tweet = models.Tweet || model('Tweet', tweetSchema)
module.exports = Tweet