forked from Colm3na/chat-app-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemas.js
39 lines (32 loc) · 1.01 KB
/
schemas.js
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
const mongoose = require('mongoose');
const dbConfig = require('./config/secrets');
mongoose.connect(dbConfig.url, { useNewUrlParser: true } );
const Schema = mongoose.Schema;
const UserSchema = new Schema ({
username: String,
email: String,
password: String,
createdAt: { type: Date, default: Date.now() },
messages: {
sent: [{
type: Schema.Types.ObjectId,
ref: 'Message',
}],
received: [{
type: Schema.Types.ObjectId,
ref: 'Message',
}]
}
})
const User = mongoose.model('User', UserSchema);
const MessageSchema = new Schema ({
body: { type: String, default: '' },
senderId: { type: Schema.Types.ObjectId, ref: 'User' },
receiverId: { type: Schema.Types.ObjectId, ref: 'User' },
sender: String,
receiver: String,
isRead: { type: Boolean, default: false },
createdAt: { type: Date, default: Date.now }
})
const Message = mongoose.model('Message', MessageSchema);
module.exports = { User, Message };