-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
36 lines (31 loc) · 1.27 KB
/
db.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
const mongoose = require('mongoose');
// this schema models a user
// it's important to note that the String array `rooms` will contain IDs of rooms the user belongs to
// this can be used to somewhat quickly look up rooms from the `Room` collection
const User = new mongoose.Schema({
username: String,
key: String,
rooms: [String]
});
// this schema models a room
// it's important to note that the String array `members` will contain IDs of users in the room
// this can be used to somewhat quickly look up users form the `User` collection
// but for reasons pertaining to good database design, this String array will likely be excluded
const Room = new mongoose.Schema({
name: String,
users: [String]
});
// this schema models a message
// it's important to note that the String `room` will be an ID of the room the message was sent in
// this can be used to somewhat quickly look up messages that belong in the room
// the timestamp is also important for limiting the search for messages history
const Message = new mongoose.Schema({
room: String,
sender: String,
message: String,
timestamp: Number,
});
mongoose.model('User', User);
mongoose.model('Room', Room);
mongoose.model('Message', Message);
mongoose.connect('mongodb://localhost/roomchata');