-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmodel.tmpl
163 lines (127 loc) · 3.65 KB
/
model.tmpl
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"use strict";
var db = require('../services/database').mongo;
var queue = require('../services/queue');
var collection = '<%= service %>s';
var debug = require('debug')(collection);
var schemaObject = {
// ++++++++++++++ Modify to your own schema ++++++++++++++++++
name: {
type: 'String'
},
someOtherStringData: {
type: 'String'
},
toPop: {
type: db._mongoose.Schema.Types.ObjectId,
ref: '<%= service %>s'
}
// ++++++++++++++ Modify to your own schema ++++++++++++++++++
};
schemaObject.createdAt = {
type: 'Date',
default: Date.now
};
schemaObject.updatedAt = {
type: 'Date'
// default: new Date().toISOString()
};
schemaObject.owner = {
type: db._mongoose.Schema.Types.ObjectId,
ref: 'Accounts'
};
schemaObject.createdBy = {
type: db._mongoose.Schema.Types.ObjectId,
ref: 'Accounts'
};
schemaObject.client = {
type: db._mongoose.Schema.Types.ObjectId,
ref: 'Clients'
};
schemaObject.developer = {
type: db._mongoose.Schema.Types.ObjectId,
ref: 'Users'
};
schemaObject.tags = {
type: [String],
index: 'text'
};
// Let us define our schema
var Schema = db._mongoose.Schema(schemaObject);
// Index all text for full text search
// MyModel.find({$text: {$search: searchString}})
// .skip(20)
// .limit(10)
// .exec(function(err, docs) { ... });
// Schema.index({'tags': 'text'});
Schema.statics.search = function(string) {
return this.find({$text: {$search: string}}, { score : { $meta: "textScore" } })
.sort({ score : { $meta : 'textScore' } });
};
// assign a function to the "methods" object of our Schema
// Schema.methods.someMethod = function (cb) {
// return this.model(collection).find({}, cb);
// };
// assign a function to the "statics" object of our Schema
// Schema.statics.someStaticFunction = function(query, cb) {
// eg. pagination
// this.find(query, null, { skip: 10, limit: 5 }, cb);
// };
// Adding hooks
Schema.pre('save', function(next) {
// Indexing for search
var ourDoc = this._doc;
ourDoc.model = collection;
// Dump it in the queue
queue.create('searchIndex', ourDoc)
.save();
next();
});
Schema.post('init', function(doc) {
debug('%s has been initialized from the db', doc._id);
});
Schema.post('validate', function(doc) {
debug('%s has been validated (but not saved yet)', doc._id);
});
Schema.post('save', function(doc) {
debug('%s has been saved', doc._id);
});
Schema.post('remove', function(doc) {
debug('%s has been removed', doc._id);
});
Schema.pre('validate', function(next) {
debug('this gets printed first');
next();
});
Schema.post('validate', function() {
debug('this gets printed second');
});
Schema.pre('find', function(next) {
debug(this instanceof db._mongoose.Query); // true
this.start = Date.now();
next();
});
Schema.post('find', function(result) {
debug(this instanceof db._mongoose.Query); // true
// prints returned documents
debug('find() returned ' + JSON.stringify(result));
// prints number of milliseconds the query took
debug('find() took ' + (Date.now() - this.start) + ' millis');
});
Schema.pre('update', function(next) {
// Indexing for search
var ourDoc = this._update;
ourDoc.model = collection;
ourDoc.update = true;
if(ourDoc.updatedAt || ourDoc.tags){ /* jslint ignore:line */
// Move along! Nothing to see here!!
}else{
// Dump it in the queue
queue.create('searchIndex', ourDoc)
.save();
}
ourDoc.updatedAt = new Date(Date.now()).toISOString();
next();
});
var Model = db.model(collection, Schema);
Model._mongoose = db._mongoose;
module.exports = Model;