-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.js
72 lines (65 loc) · 1.98 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const mongoose = require('mongoose');
// a schema to represent a religion
const religionSchema = mongoose.Schema({
name: {type: String, required: true},
address: {type: String},
synonyms: [String],
currentLeader: {type: String},
membership: {type: String},
historicalRoots: {type: String, required: true},
basicBeliefs: {type: String, required: true},
practices: {type: String, required: true},
organization: {type: String, required: true},
leadership: {type: String},
worship: {type: String},
diet: {type: String},
funerary: {type: String},
medical: {type: String},
other: {type: String},
books: {type: [String], required: true},
moreInfo: {type: String},
created: {type: Date, default: Date.now}
});
// a method used to return an object that
// exposes all of the fields from the underlying data
religionSchema.methods.apiRepr = function() {
return {
id: this._id,
name: this.name,
address: this.address,
synonyms: this.synonyms,
currentLeader: this.currentLeader,
membership: this.membership,
historicalRoots: this.historicalRoots,
basicBeliefs: this.basicBeliefs,
practices: this.practices,
organization: this.organization,
leadership: this.leadership,
worship: this.worship,
diet: this.diet,
funerary: this.funerary,
medical: this.medical,
other: this.other,
books: this.books,
moreInfo: this.moreInfo,
created: this.created
};
}
// a call made to `.model`.
const Religion = mongoose.model('Religion', religionSchema, 'religion');
// a schema to represent an authorized user
const userSchema = mongoose.Schema({
username: {type: String, required: true},
password: {type: String, required: true}
});
userSchema.methods.validPassword = function(password) {
return password === this.password
}
userSchema.methods.apiRepr = function() {
return {
id: this._id,
username: this.username
};
}
const User = mongoose.model('User', userSchema, 'user');
module.exports = {Religion, User};