-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookmarkModel.js
37 lines (31 loc) · 1.03 KB
/
bookmarkModel.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
const mongoose = require('mongoose');
const bookmarkPattern = "[a-zA-Z0-9'_-]+"; // used in express.get(); limited subset of RegExp
const bookmarkPatternRegex = new RegExp('^' + bookmarkPattern + '$');
const Schema = mongoose.Schema;
const BookmarkSchema = new Schema({
_id: {
type: String,
maxLength: 2047,
validate: {
validator: (id) => bookmarkPatternRegex.test(id),
message: (props) => `${props.value} is not a valid URI, must be alphanumeric or ['_-]`
},
required: [true, 'Bookmark page URI is required']
},
bookmarks: {
type: [{
type: String,
maxLength: 2047
}],
required: true
}
});
BookmarkSchema.virtual('uri').get(() => this._id);
BookmarkSchema.pre('findOneAndUpdate', function(next) {
this.options.runValidators = true;
next();
});
module.exports = {
BookmarkModel: mongoose.model('bookmark', BookmarkSchema, {collection: 'bookmarks', timestamps: true}),
bookmarkPattern: bookmarkPattern
};