forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.pug
525 lines (419 loc) · 20 KB
/
middleware.pug
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
extends layout
append style
link(rel="stylesheet", href="/docs/css/inlinecpc.css")
script(type="text/javascript" src="/docs/js/native.js")
block content
<a class="edit-docs-link" href="#{editLink}" target="_blank">
<img src="/docs/images/pencil.svg" />
</a>
:markdown
## Middleware
<script>
_native.init("CK7DT53U",{
targetClass: 'native-inline'
});
</script>
<div class="native-inline">
<a href="#native_link#"><span class="sponsor">Sponsor</span> #native_company# — #native_desc#</a>
</div>
Middleware (also called pre and post *hooks*) are functions which are passed
control during execution of asynchronous functions. Middleware is specified
on the schema level and is useful for writing [plugins](./plugins.html).
<ul class="toc">
<li><a href="#types-of-middleware">Types of Middleware</a></li>
<li><a href="#pre">Pre</a></li>
<li><a href="#error-handling">Errors in Pre Hooks</a></li>
<li><a href="#post">Post</a></li>
<li><a href="#post-async">Asynchronous Post Hooks</a></li>
<li><a href="#defining">Define Middleware Before Compiling Models</a></li>
<li><a href="#order">Save/Validate Hooks</a></li>
<li><a href="#naming">Naming Conflicts</a></li>
<li><a href="#notes">Notes on findAndUpdate() and Query Middleware</a></li>
<li><a href="#error-handling-middleware">Error Handling Middleware</a></li>
<li><a href="#aggregate">Aggregation Hooks</a></li>
<li><a href="#synchronous">Synchronous Hooks</a></li>
</ul>
### Types of Middleware
Mongoose has 4 types
of middleware: document middleware, model middleware, aggregate middleware, and query middleware.
Document middleware is supported for the following document functions.
In document middleware functions, `this` refers to the document.
* [validate](/docs/api/document.html#document_Document-validate)
* [save](/docs/api/model.html#model_Model-save)
* [remove](/docs/api/model.html#model_Model-remove)
* [updateOne](/docs/api/document.html#document_Document-updateOne)
* [deleteOne](/docs/api/model.html#model_Model-deleteOne)
* [init](/docs/api/document.html#document_Document-init) (note: init hooks are [synchronous](#synchronous))
Query middleware is supported for the following Model and Query functions.
In query middleware functions, `this` refers to the query.
* [count](./api.html#query_Query-count)
* [deleteMany](./api.html#query_Query-deleteMany)
* [deleteOne](./api.html#query_Query-deleteOne)
* [find](./api.html#query_Query-find)
* [findOne](./api.html#query_Query-findOne)
* [findOneAndDelete](./api.html#query_Query-findOneAndDelete)
* [findOneAndRemove](./api.html#query_Query-findOneAndRemove)
* [findOneAndUpdate](./api.html#query_Query-findOneAndUpdate)
* [remove](./api.html#model_Model.remove)
* [update](./api.html#query_Query-update)
* [updateOne](./api.html#query_Query-updateOne)
* [updateMany](./api.html#query_Query-updateMany)
Aggregate middleware is for `MyModel.aggregate()`. Aggregate middleware
executes when you call `exec()` on an aggregate object.
In aggregate middleware, `this` refers to the [aggregation object](./api.html#model_Model.aggregate).
* [aggregate](./api.html#model_Model.aggregate)
Model middleware is supported for the following model functions.
In model middleware functions, `this` refers to the model.
* [insertMany](./api.html#model_Model.insertMany)
All middleware types support pre and post hooks.
How pre and post hooks work is described in more detail below.
**Note:** If you specify `schema.pre('remove')`, Mongoose will register this
middleware for [`doc.remove()`](./api.html#model_Model-remove) by default. If you
want to your middleware to run on [`Query.remove()`](./api.html#query_Query-remove)
use [`schema.pre('remove', { query: true, document: false }, fn)`](./api.html#schema_Schema-pre).
**Note:** Unlike `schema.pre('remove')`, Mongoose registers `updateOne` and
`deleteOne` middleware on `Query#updateOne()` and `Query#deleteOne()` by default.
This means that both `doc.updateOne()` and `Model.updateOne()` trigger
`updateOne` hooks, but `this` refers to a query, not a document. To register
`updateOne` or `deleteOne` middleware as document middleware, use
`schema.pre('updateOne', { document: true, query: false })`.
**Note:** The [`create()`](./api.html#model_Model.create) function fires `save()` hooks.
<h3 id="pre"><a href="#pre">Pre</a></h3>
Pre middleware functions are executed one after another, when each
middleware calls `next`.
```javascript
var schema = new Schema(..);
schema.pre('save', function(next) {
// do stuff
next();
});
```
In [mongoose 5.x](http://thecodebarbarian.com/introducing-mongoose-5.html#promises-and-async-await-with-middleware), instead of calling `next()` manually, you can use a
function that returns a promise. In particular, you can use [`async/await`](http://thecodebarbarian.com/common-async-await-design-patterns-in-node.js.html).
```javascript
schema.pre('save', function() {
return doStuff().
then(() => doMoreStuff());
});
// Or, in Node.js >= 7.6.0:
schema.pre('save', async function() {
await doStuff();
await doMoreStuff();
});
```
If you use `next()`, the `next()` call does **not** stop the rest of the code in your middleware function from executing. Use
[the early `return` pattern](https://www.bennadel.com/blog/2323-use-a-return-statement-when-invoking-callbacks-especially-in-a-guard-statement.htm)
to prevent the rest of your middleware function from running when you call `next()`.
```javascript
var schema = new Schema(..);
schema.pre('save', function(next) {
if (foo()) {
console.log('calling next!');
// `return next();` will make sure the rest of this function doesn't run
/*return*/ next();
}
// Unless you comment out the `return` above, 'after next' will print
console.log('after next');
});
```
<h4 id="use-cases">Use Cases</h4>
Middleware are useful for atomizing model logic. Here are some other ideas:
* complex validation
* removing dependent documents (removing a user removes all his blogposts)
* asynchronous defaults
* asynchronous tasks that a certain action triggers
<h4 id="error-handling">Errors in Pre Hooks</h4>
If any pre hook errors out, mongoose will not execute subsequent middleware
or the hooked function. Mongoose will instead pass an error to the callback
and/or reject the returned promise. There are several ways to report an
error in middleware:
```javascript
schema.pre('save', function(next) {
const err = new Error('something went wrong');
// If you call `next()` with an argument, that argument is assumed to be
// an error.
next(err);
});
schema.pre('save', function() {
// You can also return a promise that rejects
return new Promise((resolve, reject) => {
reject(new Error('something went wrong'));
});
});
schema.pre('save', function() {
// You can also throw a synchronous error
throw new Error('something went wrong');
});
schema.pre('save', async function() {
await Promise.resolve();
// You can also throw an error in an `async` function
throw new Error('something went wrong');
});
// later...
// Changes will not be persisted to MongoDB because a pre hook errored out
myDoc.save(function(err) {
console.log(err.message); // something went wrong
});
```
Calling `next()` multiple times is a no-op. If you call `next()` with an
error `err1` and then throw an error `err2`, mongoose will report `err1`.
<h3 id="post"><a href="#post">Post middleware</a></h3>
[post](/docs/api.html#schema_Schema-post) middleware are executed _after_
the hooked method and all of its `pre` middleware have completed.
```javascript
schema.post('init', function(doc) {
console.log('%s has been initialized from the db', doc._id);
});
schema.post('validate', function(doc) {
console.log('%s has been validated (but not saved yet)', doc._id);
});
schema.post('save', function(doc) {
console.log('%s has been saved', doc._id);
});
schema.post('remove', function(doc) {
console.log('%s has been removed', doc._id);
});
```
<h3 id="post-async"><a href="#post-async">Asynchronous Post Hooks</a></h3>
If your post hook function takes at least 2 parameters, mongoose will
assume the second parameter is a `next()` function that you will call to
trigger the next middleware in the sequence.
```javascript
// Takes 2 parameters: this is an asynchronous post hook
schema.post('save', function(doc, next) {
setTimeout(function() {
console.log('post1');
// Kick off the second post hook
next();
}, 10);
});
// Will not execute until the first middleware calls `next()`
schema.post('save', function(doc, next) {
console.log('post2');
next();
});
```
<h3 id="defining"><a href="#defining">Define Middleware Before Compiling Models</a></h3>
Calling `pre()` or `post()` after [compiling a model](/docs/models.html#compiling)
does **not** work in Mongoose in general. For example, the below `pre('save')`
middleware will not fire.
```javascript
const schema = new mongoose.Schema({ name: String });
// Compile a model from the schema
const User = mongoose.model('User', schema);
// Mongoose will **not** call the middleware function, because
// this middleware was defined after the model was compiled
schema.pre('save', () => console.log('Hello from pre save'));
new User({ name: 'test' }).save();
```
This means that you must add all middleware and [plugins](/docs/plugins.html)
**before** calling [`mongoose.model()](/docs/api/mongoose.html#mongoose_Mongoose-model).
The below script will print out "Hello from pre save":
```javascript
const schema = new mongoose.Schema({ name: String });
// Mongoose will call this middleware function, because this script adds
// the middleware to the schema before compiling the model.
schema.pre('save', () => console.log('Hello from pre save'));
// Compile a model from the schema
const User = mongoose.model('User', schema);
new User({ name: 'test' }).save();
```
As a consequence, be careful about exporting Mongoose models from the same
file that you define your schema. If you choose to use this pattern, you
must define [global plugins](/docs/api/mongoose.html#mongoose_Mongoose-plugin)
**before** calling `require()` on your model file.
```javascript
const schema = new mongoose.Schema({ name: String });
// Once you `require()` this file, you can no longer add any middleware
// to this schema.
module.exports = mongoose.model('User', schema);
```
<h3 id="order"><a href="#order">Save/Validate Hooks</a></h3>
The `save()` function triggers `validate()` hooks, because mongoose
has a built-in `pre('save')` hook that calls `validate()`. This means
that all `pre('validate')` and `post('validate')` hooks get called
**before** any `pre('save')` hooks.
```javascript
schema.pre('validate', function() {
console.log('this gets printed first');
});
schema.post('validate', function() {
console.log('this gets printed second');
});
schema.pre('save', function() {
console.log('this gets printed third');
});
schema.post('save', function() {
console.log('this gets printed fourth');
});
```
<h3 id="naming"><a href="#naming">Naming Conflicts</a></h3>
Mongoose has both query and document hooks for `remove()`.
```javascript
schema.pre('remove', function() { console.log('Removing!'); });
// Prints "Removing!"
doc.remove();
// Does **not** print "Removing!". Query middleware for `remove` is not
// executed by default.
Model.remove();
```
You can pass options to [`Schema.pre()`](/docs/api.html#schema_Schema-pre)
and [`Schema.post()`](/docs/api.html#schema_Schema-post) to switch whether
Mongoose calls your `remove()` hook for [`Document.remove()`](/docs/api.html#model_Model-remove)
or [`Model.remove()`](/docs/api.html#model_Model.remove):
```javascript
// Only document middleware
schema.pre('remove', { document: true }, function() {
console.log('Removing doc!');
});
// Only query middleware. This will get called when you do `Model.remove()`
// but not `doc.remove()`.
schema.pre('remove', { query: true }, function() {
console.log('Removing!');
});
```
<h3 id="notes"><a href="#notes">Notes on findAndUpdate() and Query Middleware</a></h3>
Pre and post `save()` hooks are **not** executed on `update()`,
`findOneAndUpdate()`, etc. You can see a more detailed discussion why in
[this GitHub issue](http://github.com/Automattic/mongoose/issues/964).
Mongoose 4.0 introduced distinct hooks for these functions.
```javascript
schema.pre('find', function() {
console.log(this instanceof mongoose.Query); // true
this.start = Date.now();
});
schema.post('find', function(result) {
console.log(this instanceof mongoose.Query); // true
// prints returned documents
console.log('find() returned ' + JSON.stringify(result));
// prints number of milliseconds the query took
console.log('find() took ' + (Date.now() - this.start) + ' millis');
});
```
Query middleware differs from document middleware in a subtle but
important way: in document middleware, `this` refers to the document
being updated. In query middleware, mongoose doesn't necessarily have
a reference to the document being updated, so `this` refers to the
**query** object rather than the document being updated.
For instance, if you wanted to add an `updatedAt` timestamp to every
`updateOne()` call, you would use the following pre hook.
```javascript
schema.pre('updateOne', function() {
this.set({ updatedAt: new Date() });
});
```
You **cannot** access the document being updated in `pre('updateOne')` or
`pre('findOneAndUpdate')` query middleware. If you need to access the document
that will be updated, you need to execute an explicit query for the document.
```javascript
schema.pre('findOneAndUpdate', async function() {
const docToUpdate = await this.model.findOne(this.getQuery());
console.log(docToUpdate); // The document that `findOneAndUpdate()` will modify
});
```
However, if you define `pre('updateOne')` document middleware,
`this` will be the document being updated. That's because `pre('updateOne')`
document middleware hooks into [`Document#updateOne()`](/docs/api/document.html#document_Document-updateOne)
rather than `Query#updateOne()`.
```javascript
schema.pre('updateOne', { document: true, query: false }, function() {
console.log('Updating');
});
const Model = mongoose.model('Test', schema);
const doc = new Model();
await doc.updateOne({ $set: { name: 'test' } }); // Prints "Updating"
// Doesn't print "Updating", because `Query#updateOne()` doesn't fire
// document middleware.
await Model.updateOne({}, { $set: { name: 'test' } });
```
<h3 id="error-handling-middleware"><a href="#error-handling-middleware">Error Handling Middleware</a></h3>
_New in 4.5.0_
Middleware execution normally stops the first time a piece of middleware
calls `next()` with an error. However, there is a special kind of post
middleware called "error handling middleware" that executes specifically
when an error occurs. Error handling middleware is useful for reporting
errors and making error messages more readable.
Error handling middleware is defined as middleware that takes one extra
parameter: the 'error' that occurred as the first parameter to the function.
Error handling middleware can then transform the error however you want.
```javascript
var schema = new Schema({
name: {
type: String,
// Will trigger a MongoError with code 11000 when
// you save a duplicate
unique: true
}
});
// Handler **must** take 3 parameters: the error that occurred, the document
// in question, and the `next()` function
schema.post('save', function(error, doc, next) {
if (error.name === 'MongoError' && error.code === 11000) {
next(new Error('There was a duplicate key error'));
} else {
next();
}
});
// Will trigger the `post('save')` error handler
Person.create([{ name: 'Axl Rose' }, { name: 'Axl Rose' }]);
```
Error handling middleware also works with query middleware. You can
also define a post `update()` hook that will catch MongoDB duplicate key
errors.
```javascript
// The same E11000 error can occur when you call `update()`
// This function **must** take 3 parameters. If you use the
// `passRawResult` function, this function **must** take 4
// parameters
schema.post('update', function(error, res, next) {
if (error.name === 'MongoError' && error.code === 11000) {
next(new Error('There was a duplicate key error'));
} else {
next(); // The `update()` call will still error out.
}
});
var people = [{ name: 'Axl Rose' }, { name: 'Slash' }];
Person.create(people, function(error) {
Person.update({ name: 'Slash' }, { $set: { name: 'Axl Rose' } }, function(error) {
// `error.message` will be "There was a duplicate key error"
});
});
```
Error handling middleware can transform an error, but it can't remove the
error. Even if you call `next()` with no error as shown above, the
function call will still error out.
<h3 id="aggregate"><a href="#aggregate">Aggregation Hooks</a></h3>
You can also define hooks for the [`Model.aggregate()` function](api.html#model_Model.aggregate).
In aggregation middleware functions, `this` refers to the [Mongoose `Aggregate` object](api.html#Aggregate).
For example, suppose you're implementing soft deletes on a `Customer` model
by adding an `isDeleted` property. To make sure `aggregate()` calls only look
at customers that aren't soft deleted, you can use the below middleware to
add a [`$match` stage](api.html#aggregate_Aggregate-match) to the beginning
of each [aggregation pipeline](https://docs.mongodb.com/manual/core/aggregation-pipeline/).
```javascript
customerSchema.pre('aggregate', function() {
// Add a $match state to the beginning of each pipeline.
this.pipeline().unshift({ $match: { isDeleted: { $ne: true } } });
});
```
The [`Aggregate#pipeline()` function](api.html#aggregate_Aggregate-pipeline)
lets you access the MongoDB aggregation pipeline that Mongoose will send to
the MongoDB server. It is useful for adding stages to the beginning of the
pipeline from middleware.
<h3 id="synchronous"><a href="#synchronous">Synchronous Hooks</a></h3>
Certain Mongoose hooks are synchronous, which means they do **not** support
functions that return promises or receive a `next()` callback. Currently,
only `init` hooks are synchronous, because the [`init()` function](./api.html#document_Document-init)
is synchronous. Below is an example of using pre and post init hooks.
```javascript
[require:post init hooks.*success]
```
To report an error in an init hook, you must throw a **synchronous** error.
Unlike all other middleware, init middleware does **not** handle promise
rejections.
```javascript
[require:post init hooks.*error]
```
<h3 id="next">Next Up</h3>
Now that we've covered middleware, let's take a look at Mongoose's approach
to faking JOINs with its query [population](/docs/populate.html) helper.