-
Notifications
You must be signed in to change notification settings - Fork 1
/
persistence.js
46 lines (40 loc) · 1.36 KB
/
persistence.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
let mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/genealogy', {useNewUrlParser: true, useUnifiedTopology: true});
const urlPattern = /(http|https):\/\/(\w+:{0,1}\w*#)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-/]))?/;
const urlRegExp = new RegExp(urlPattern);
let PersonSchema = new mongoose.Schema({
name: { type: String, index: true, required: true },
surname: { type: String, index: true },
photosURLs: [
{
type: String,
validate: {
validator: function(value) {
return value.match(urlRegExp);
},
message: props => `${props.value} is not a valid URL`
}
}
],
yearBorn: { type: Number, min: -5000, max: (new Date).getFullYear() },
notes: { type: String, minlength: 5 },
mother: { type: mongoose.Schema.Types.ObjectId, ref: 'Person' },
father: { type: mongoose.Schema.Types.ObjectId, ref: 'Person' },
});
PersonSchema.virtual('fullName').
get(function() {
if(this.surname)
return this.name + ' ' + this.surname;
return this.name;
}).
set(function(fullName) {
fullName = fullName.split(' ');
this.name = fullName[0];
this.surname = fullName[1];
});
PersonSchema.pre('findOne', function(next) {
this.populate('mother').populate('father');
next();
});
module.exports.db = mongoose;
module.exports.Person = mongoose.model('Person', PersonSchema);