-
Notifications
You must be signed in to change notification settings - Fork 13
/
models.js
36 lines (28 loc) · 986 Bytes
/
models.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
'use strict'
const mongoose = require('mongoose')
const inDevelopment = process.env.NODE_ENV !== 'production'
// Connect to the local MongoDB database named “testdb”
mongoose.connect(
inDevelopment ? 'mongodb://localhost/testdb' : process.env.MONGO_URL
)
// Create a Group schema to be stored in the MongoDB database
const GroupSchema = new mongoose.Schema({
_id: String,
name: String
})
// Turn that schema into a model that we can query
const Group = mongoose.model('Group', GroupSchema)
// Create a User schema to be stored in the MongoDB database
const UserSchema = new mongoose.Schema({
_id: String,
username: String,
groupId: String
})
// Retrieve the group associated with the user
UserSchema.methods.group = function() {
// Use .exec() to ensure a true Promise is returned
return Group.findById(this.groupId).exec()
}
// Turn that schema into a model that we can query
const User = mongoose.model('User', UserSchema)
module.exports = { User, Group }