-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.js
More file actions
57 lines (50 loc) · 1.45 KB
/
models.js
File metadata and controls
57 lines (50 loc) · 1.45 KB
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
'use strict';
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// var SynonymSchema = new Schema({
// word: String,
// partOfSpeech: String,
// definitions: [String],
// createdAt: { type: Date, default: Date.now }
// });
var SuggestedSchema= new Schema({
word: String,
related: [String],
createdAt: {type: Date, default: Date.now}
})
SuggestedSchema.pre('save', function (next) {
var self = this
Suggested.findOne({word: self.word}, function(err, doc) {
if (!doc) {
next()
} else {
console.log('suggested exists')
next(new Error('suggested exists!'))
}
})
})
var WordSchema = new Schema({
word: String,
partOfSpeech: String,
definition: String,
synonyms: [String],
related: [String],
createdAt: { type: Date, default: Date.now }
});
WordSchema.pre('save', function (next) {
var self = this;
Word.find({word : self.word, partOfSpeech: self.partOfSpeech}, function (err, docs) {
if (!docs.length){
next();
} else{
console.log('word exists:',self.name);
next(new Error("word exists!"));
}
});
}) ;
// var Synonym = mongoose.model("Synonym", SynonymSchema);
var Suggested = mongoose.model('Suggested', SuggestedSchema)
var Word = mongoose.model("Word", WordSchema);
// module.exports.Synonym = Synonym;
module.exports.Word = Word;
module.exports.Suggested = Suggested;