|
| 1 | +--- |
| 2 | +id: version-3.2.x-middleware |
| 3 | +title: Middleware |
| 4 | +sidebar_label: Middleware |
| 5 | +original_id: middleware |
| 6 | +--- |
| 7 | + |
| 8 | +## CRUD |
| 9 | +Models can support middleware functions for CRUD operations. These exist under the ``routeOptions`` object. The following middleware functions are available: |
| 10 | + |
| 11 | +* list: |
| 12 | + - pre(query, request, Log) |
| 13 | + * returns: `query` |
| 14 | + - post(request, result, Log) |
| 15 | + * returns: `result` |
| 16 | +* find: |
| 17 | + - pre(\_id, query, request, Log) |
| 18 | + * returns: `query` |
| 19 | + - post(request, result, Log) |
| 20 | + * returns: `result` |
| 21 | +* create: |
| 22 | + - pre(payload, request, Log) |
| 23 | + * > **NOTE:** _For payloads with multiple documents, the pre function will be called for each document individually (passed in through the `payload` parameter) i.e. `request.payload` = array of documents, `payload` = single document_ |
| 24 | + |
| 25 | + * returns: `payload` |
| 26 | + - post(document, request, result, Log) |
| 27 | + * returns: `document` |
| 28 | +* update: |
| 29 | + - pre(\_id, payload, request, Log) |
| 30 | + * returns: `payload` |
| 31 | + - post(request, result, Log) |
| 32 | + * returns: `result` |
| 33 | +* delete: |
| 34 | + - pre(\_id, hardDelete, request, Log) |
| 35 | + * returns: `null` |
| 36 | + - post(hardDelete, deleted, request, Log) |
| 37 | + * returns: `null` |
| 38 | + |
| 39 | +For example, a ``create: pre`` function can be defined to encrypt a users password |
| 40 | +using a static method ``generatePasswordHash``. |
| 41 | + |
| 42 | +```javascript |
| 43 | +// models/user.model.js |
| 44 | +let bcrypt = require('bcrypt'); |
| 45 | + |
| 46 | +module.exports = function (mongoose) { |
| 47 | + let modelName = "user"; |
| 48 | + let Types = mongoose.Schema.Types; |
| 49 | + let Schema = new mongoose.Schema({ |
| 50 | + email: { |
| 51 | + type: Types.String, |
| 52 | + unique: true |
| 53 | + }, |
| 54 | + password: { |
| 55 | + type: Types.String, |
| 56 | + required: true, |
| 57 | + exclude: true, |
| 58 | + allowOnUpdate: false |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + Schema.statics = { |
| 63 | + collectionName:modelName, |
| 64 | + routeOptions: { |
| 65 | + create: { |
| 66 | + pre: function (payload, request, Log) { |
| 67 | + let hashedPassword = mongoose.model('user').generatePasswordHash(payload.password); |
| 68 | + |
| 69 | + payload.password = hashedPassword; |
| 70 | + |
| 71 | + return payload; |
| 72 | + } |
| 73 | + } |
| 74 | + }, |
| 75 | + |
| 76 | + generatePasswordHash: function(password) { |
| 77 | + let salt = bcrypt.genSaltSync(10); |
| 78 | + let hash = bcrypt.hashSync(password, salt); |
| 79 | + return hash; |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + return Schema; |
| 84 | +}; |
| 85 | +``` |
| 86 | + |
| 87 | +If a `Boom` error is thrown within a middleware function, that error will become the server response. Ex: |
| 88 | + |
| 89 | +```javascript |
| 90 | + create: { |
| 91 | + pre: function (payload, request, Log) { |
| 92 | + throw Boom.badRequest("TEST ERROR") |
| 93 | + } |
| 94 | + } |
| 95 | +``` |
| 96 | + |
| 97 | +will result in a response body of: |
| 98 | + |
| 99 | +```javascript |
| 100 | +{ |
| 101 | + "statusCode": 400, |
| 102 | + "error": "Bad Request", |
| 103 | + "message": "TEST ERROR" |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +## Association |
| 108 | +Support is being added for association middlware. Currently the following association middleware exist: |
| 109 | + |
| 110 | +* getAll: |
| 111 | + - post(request, result, Log) |
| 112 | + * returns: result |
| 113 | +* add: |
| 114 | + - pre(payload, request, Log) |
| 115 | + * returns: payload |
| 116 | + - post(payload, request, Log) |
| 117 | + * returns: void |
| 118 | +* remove: |
| 119 | + - pre(payload, request, Log) |
| 120 | + * returns: payload |
| 121 | + - post(payload, request, Log) |
| 122 | + * returns: void |
| 123 | + |
| 124 | +Association middleware is defined similar to CRUD middleware, with the only difference being the association name must be specified. See below for an example: |
| 125 | + |
| 126 | +```javascript |
| 127 | + routeOptions: { |
| 128 | + associations: { |
| 129 | + groups: { |
| 130 | + type: "MANY_MANY", |
| 131 | + model: "group" |
| 132 | + } |
| 133 | + } |
| 134 | + }, |
| 135 | + getAll: { |
| 136 | + groups: { //<---this must match the association name |
| 137 | + post: function(request, result, Log) { |
| 138 | + /** modify and return result **/ |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +``` |
0 commit comments