|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +var db = require('../services/database/mongo'); |
| 4 | + |
| 5 | +var collection = 'Trash'; |
| 6 | + |
| 7 | +var service = 'Users'; |
| 8 | + |
| 9 | +var debug = require('debug')(collection); |
| 10 | + |
| 11 | +var schemaObject = { |
| 12 | + data: { |
| 13 | + type: db.Schema.Types.Mixed |
| 14 | + }, |
| 15 | + service: { |
| 16 | + type: 'String', |
| 17 | + default: service |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +schemaObject.createdAt = { |
| 22 | + type: 'Date', |
| 23 | + default: Date.now |
| 24 | +}; |
| 25 | + |
| 26 | +schemaObject.updatedAt = { |
| 27 | + type: 'Date' |
| 28 | + // default: Date.now |
| 29 | +}; |
| 30 | + |
| 31 | +schemaObject.owner = { |
| 32 | + type: db.Schema.Types.ObjectId, |
| 33 | + ref: 'Users' |
| 34 | +}; |
| 35 | + |
| 36 | +schemaObject.deletedBy = { |
| 37 | + type: db.Schema.Types.ObjectId, |
| 38 | + ref: 'Users' |
| 39 | +}; |
| 40 | + |
| 41 | +schemaObject.client = { |
| 42 | + type: db.Schema.Types.ObjectId, |
| 43 | + ref: 'Clients' |
| 44 | +}; |
| 45 | + |
| 46 | +schemaObject.developer = { |
| 47 | + type: db.Schema.Types.ObjectId, |
| 48 | + ref: 'Users' |
| 49 | +}; |
| 50 | + |
| 51 | +schemaObject.tags = { |
| 52 | + type: [String], |
| 53 | + index: 'text' |
| 54 | +}; |
| 55 | + |
| 56 | +// Let us define our schema |
| 57 | +var Schema = db.Schema(schemaObject); |
| 58 | + |
| 59 | +// Index all text for full text search |
| 60 | +// MyModel.find({$text: {$search: searchString}}) |
| 61 | +// .skip(20) |
| 62 | +// .limit(10) |
| 63 | +// .exec(function(err, docs) { ... }); |
| 64 | +// Schema.index({'tags': 'text'}); |
| 65 | + |
| 66 | +Schema.statics.search = function(string) { |
| 67 | + return this.find({$text: {$search: string}}, { score : { $meta: "textScore" } }) |
| 68 | + .sort({ score : { $meta : 'textScore' } }); |
| 69 | +}; |
| 70 | + |
| 71 | +// assign a function to the "methods" object of our Schema |
| 72 | +// Schema.methods.someMethod = function (cb) { |
| 73 | +// return this.model(collection).find({}, cb); |
| 74 | +// }; |
| 75 | + |
| 76 | +// assign a function to the "statics" object of our Schema |
| 77 | +// Schema.statics.someStaticFunction = function(query, cb) { |
| 78 | +// eg. pagination |
| 79 | + // this.find(query, null, { skip: 10, limit: 5 }, cb); |
| 80 | +// }; |
| 81 | + |
| 82 | +// Adding hooks |
| 83 | + |
| 84 | +Schema.pre('save', function(next) { |
| 85 | + // Indexing for search |
| 86 | + var ourDoc = this._doc; |
| 87 | + var split = []; |
| 88 | + for(var n in ourDoc){ |
| 89 | + if(typeof ourDoc[n] === 'string'){ |
| 90 | + split.push(ourDoc[n].split(' ')); |
| 91 | + } |
| 92 | + } |
| 93 | + this.tags = split; |
| 94 | + next(); |
| 95 | +}); |
| 96 | + |
| 97 | +Schema.post('init', function(doc) { |
| 98 | + debug('%s has been initialized from the db', doc._id); |
| 99 | +}); |
| 100 | + |
| 101 | +Schema.post('validate', function(doc) { |
| 102 | + debug('%s has been validated (but not saved yet)', doc._id); |
| 103 | +}); |
| 104 | + |
| 105 | +Schema.post('save', function(doc) { |
| 106 | + debug('%s has been saved', doc._id); |
| 107 | +}); |
| 108 | + |
| 109 | +Schema.post('remove', function(doc) { |
| 110 | + debug('%s has been removed', doc._id); |
| 111 | +}); |
| 112 | + |
| 113 | +Schema.pre('validate', function(next) { |
| 114 | + debug('this gets printed first'); |
| 115 | + next(); |
| 116 | +}); |
| 117 | + |
| 118 | +Schema.post('validate', function() { |
| 119 | + debug('this gets printed second'); |
| 120 | +}); |
| 121 | + |
| 122 | +Schema.pre('find', function(next) { |
| 123 | + debug(this instanceof db.Query); // true |
| 124 | + this.start = Date.now(); |
| 125 | + next(); |
| 126 | +}); |
| 127 | + |
| 128 | +Schema.post('find', function(result) { |
| 129 | + debug(this instanceof db.Query); // true |
| 130 | + // prints returned documents |
| 131 | + debug('find() returned ' + JSON.stringify(result)); |
| 132 | + // prints number of milliseconds the query took |
| 133 | + debug('find() took ' + (Date.now() - this.start) + ' millis'); |
| 134 | +}); |
| 135 | + |
| 136 | +Schema.pre('update', function(next) { |
| 137 | + // Adding updated date |
| 138 | + |
| 139 | + // Indexing for search |
| 140 | + var ourDoc = this._update.$set; |
| 141 | + var split = []; |
| 142 | + for(var n in ourDoc){ |
| 143 | + if(typeof ourDoc[n] === 'string'){ |
| 144 | + split.push(ourDoc[n].split(' ')); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + if(!split){ |
| 149 | + this.update(this._conditions,{ $set: { updatedAt: new Date()} }); |
| 150 | + }else{ |
| 151 | + this.update(this._conditions,{ $set: { updatedAt: new Date()}, $addToSet: {tags: {$each: split}} }); |
| 152 | + } |
| 153 | + |
| 154 | + next(); |
| 155 | +}); |
| 156 | + |
| 157 | +var Model = db.model(collection, Schema); |
| 158 | + |
| 159 | +module.exports = Model; |
0 commit comments