express-restify-mongoose 9.0.2 β†’ 9.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,6 +26,7 @@ type Options = {
26
26
  preRead: RequestHandler | RequestHandler[];
27
27
  preUpdate: RequestHandler | RequestHandler[];
28
28
  preDelete: RequestHandler | RequestHandler[];
29
+ updateDeep: boolean;
29
30
  access?: (req: Request) => Access | Promise<Access>;
30
31
  contextFilter: (model: mongoose.Model<unknown>, req: Request, done: (query: mongoose.Model<unknown> | mongoose.Query<unknown, unknown>) => void) => void;
31
32
  postCreate?: RequestHandler | RequestHandler[];
@@ -37,7 +37,8 @@ const defaultOptions = {
37
37
  preCreate: [],
38
38
  preRead: [],
39
39
  preUpdate: [],
40
- preDelete: []
40
+ preDelete: [],
41
+ updateDeep: true
41
42
  };
42
43
  const filter = new _resource_filter.Filter();
43
44
  function serve(app, model, options = {}) {
@@ -79,31 +80,32 @@ function serve(app, model, options = {}) {
79
80
  // @ts-expect-error restify
80
81
  app.delete = app.del;
81
82
  }
82
- app.use(async (req, res, next)=>{
83
+ // @ts-expect-error restify
84
+ const modelMiddleware = async (req, res, next)=>{
83
85
  const getModel = serveOptions?.modelFactory?.getModel;
84
86
  req.erm = {
85
87
  model: typeof getModel === 'function' ? await getModel(req) : model
86
88
  };
87
89
  next();
88
- });
90
+ };
89
91
  const accessMiddleware = serveOptions.access ? (0, _access.getAccessHandler)({
90
92
  access: serveOptions.access,
91
93
  idProperty: serveOptions.idProperty,
92
94
  onError: serveOptions.onError
93
95
  }) : [];
94
96
  const ensureContentType = (0, _ensureContentType.getEnsureContentTypeHandler)(serveOptions);
95
- const filterAndFindById = (0, _filterAndFindById.getFilterAndFindByIdHandler)(serveOptions, model);
97
+ const filterAndFindById = (0, _filterAndFindById.getFilterAndFindByIdHandler)(serveOptions);
96
98
  const prepareQuery = (0, _prepareQuery.getPrepareQueryHandler)(serveOptions);
97
99
  const prepareOutput = (0, _prepareOutput.getPrepareOutputHandler)(serveOptions, model.modelName, filter);
98
- app.get(uriItems, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getItems, prepareOutput);
99
- app.get(uriCount, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getCount, prepareOutput);
100
- app.get(uriItem, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getItem, prepareOutput);
101
- app.get(uriShallow, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getShallow, prepareOutput);
102
- app.post(uriItems, prepareQuery, ensureContentType, serveOptions.preMiddleware, serveOptions.preCreate, accessMiddleware, ops.createObject, prepareOutput);
103
- app.post(uriItem, (0, _util.deprecate)(prepareQuery, "express-restify-mongoose: in a future major version, the POST method to update resources will be removed. Use PATCH instead."), ensureContentType, serveOptions.preMiddleware, serveOptions.findOneAndUpdate ? [] : filterAndFindById, serveOptions.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput);
104
- app.put(uriItem, (0, _util.deprecate)(prepareQuery, "express-restify-mongoose: in a future major version, the PUT method will replace rather than update a resource. Use PATCH instead."), ensureContentType, serveOptions.preMiddleware, serveOptions.findOneAndUpdate ? [] : filterAndFindById, serveOptions.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput);
105
- app.patch(uriItem, prepareQuery, ensureContentType, serveOptions.preMiddleware, serveOptions.findOneAndUpdate ? [] : filterAndFindById, serveOptions.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput);
106
- app.delete(uriItems, prepareQuery, serveOptions.preMiddleware, serveOptions.preDelete, ops.deleteItems, prepareOutput);
107
- app.delete(uriItem, prepareQuery, serveOptions.preMiddleware, serveOptions.findOneAndRemove ? [] : filterAndFindById, serveOptions.preDelete, ops.deleteItem, prepareOutput);
100
+ app.get(uriItems, modelMiddleware, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getItems, prepareOutput);
101
+ app.get(uriCount, modelMiddleware, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getCount, prepareOutput);
102
+ app.get(uriItem, modelMiddleware, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getItem, prepareOutput);
103
+ app.get(uriShallow, modelMiddleware, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getShallow, prepareOutput);
104
+ app.post(uriItems, modelMiddleware, prepareQuery, ensureContentType, serveOptions.preMiddleware, serveOptions.preCreate, accessMiddleware, ops.createObject, prepareOutput);
105
+ app.post(uriItem, modelMiddleware, (0, _util.deprecate)(prepareQuery, "express-restify-mongoose: in a future major version, the POST method to update resources will be removed. Use PATCH instead."), ensureContentType, serveOptions.preMiddleware, serveOptions.findOneAndUpdate ? [] : filterAndFindById, serveOptions.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput);
106
+ app.put(uriItem, modelMiddleware, (0, _util.deprecate)(prepareQuery, "express-restify-mongoose: in a future major version, the PUT method will replace rather than update a resource. Use PATCH instead."), ensureContentType, serveOptions.preMiddleware, serveOptions.findOneAndUpdate ? [] : filterAndFindById, serveOptions.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput);
107
+ app.patch(uriItem, modelMiddleware, prepareQuery, ensureContentType, serveOptions.preMiddleware, serveOptions.findOneAndUpdate ? [] : filterAndFindById, serveOptions.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput);
108
+ app.delete(uriItems, modelMiddleware, prepareQuery, serveOptions.preMiddleware, serveOptions.preDelete, ops.deleteItems, prepareOutput);
109
+ app.delete(uriItem, modelMiddleware, prepareQuery, serveOptions.preMiddleware, serveOptions.findOneAndRemove ? [] : filterAndFindById, serveOptions.preDelete, ops.deleteItem, prepareOutput);
108
110
  return uriItems;
109
111
  }
@@ -10,10 +10,13 @@ Object.defineProperty(exports, "getFilterAndFindByIdHandler", {
10
10
  });
11
11
  const _http = require("http");
12
12
  const _errorHandler = require("../errorHandler.js");
13
- function getFilterAndFindByIdHandler(options, model) {
13
+ function getFilterAndFindByIdHandler(options) {
14
14
  const errorHandler = (0, _errorHandler.getErrorHandler)(options);
15
15
  const fn = function filterAndFindById(req, res, next) {
16
- const contextModel = model;
16
+ const contextModel = req.erm.model;
17
+ if (!contextModel) {
18
+ return errorHandler(new Error('Model is undefined.'), req, res, next);
19
+ }
17
20
  if (!req.params.id) {
18
21
  return next();
19
22
  }
@@ -38,7 +38,10 @@ function operations(model, options, filter) {
38
38
  }).includes(req.erm.query.distinct);
39
39
  }
40
40
  const getItems = function(req, res, next) {
41
- const contextModel = model;
41
+ const contextModel = req.erm.model;
42
+ if (!contextModel) {
43
+ return errorHandler(new Error('Model is undefined.'), req, res, next);
44
+ }
42
45
  if (isDistinctExcluded(req)) {
43
46
  req.erm.result = [];
44
47
  req.erm.statusCode = 200;
@@ -67,7 +70,10 @@ function operations(model, options, filter) {
67
70
  });
68
71
  };
69
72
  const getCount = function(req, res, next) {
70
- const contextModel = model;
73
+ const contextModel = req.erm.model;
74
+ if (!contextModel) {
75
+ return errorHandler(new Error('Model is undefined.'), req, res, next);
76
+ }
71
77
  options.contextFilter(contextModel, req, (filteredContext)=>{
72
78
  buildQuery(filteredContext.countDocuments(), req.erm.query).then((count)=>{
73
79
  req.erm.result = {
@@ -79,7 +85,10 @@ function operations(model, options, filter) {
79
85
  });
80
86
  };
81
87
  const getShallow = function(req, res, next) {
82
- const contextModel = model;
88
+ const contextModel = req.erm.model;
89
+ if (!contextModel) {
90
+ return errorHandler(new Error('Model is undefined.'), req, res, next);
91
+ }
83
92
  options.contextFilter(contextModel, req, (filteredContext)=>{
84
93
  buildQuery(// @ts-expect-error this is fine 🐢πŸ”₯
85
94
  findById(filteredContext, req.params.id), req.erm.query).then((item)=>{
@@ -96,7 +105,10 @@ function operations(model, options, filter) {
96
105
  });
97
106
  };
98
107
  const deleteItems = function(req, res, next) {
99
- const contextModel = model;
108
+ const contextModel = req.erm.model;
109
+ if (!contextModel) {
110
+ return errorHandler(new Error('Model is undefined.'), req, res, next);
111
+ }
100
112
  options.contextFilter(contextModel, req, (filteredContext)=>{
101
113
  buildQuery(filteredContext.deleteMany(), req.erm.query).then(()=>{
102
114
  req.erm.statusCode = 204;
@@ -105,7 +117,10 @@ function operations(model, options, filter) {
105
117
  });
106
118
  };
107
119
  const getItem = function(req, res, next) {
108
- const contextModel = model;
120
+ const contextModel = req.erm.model;
121
+ if (!contextModel) {
122
+ return errorHandler(new Error('Model is undefined.'), req, res, next);
123
+ }
109
124
  if (isDistinctExcluded(req)) {
110
125
  req.erm.result = [];
111
126
  req.erm.statusCode = 200;
@@ -124,7 +139,10 @@ function operations(model, options, filter) {
124
139
  });
125
140
  };
126
141
  const deleteItem = function(req, res, next) {
127
- const contextModel = model;
142
+ const contextModel = req.erm.model;
143
+ if (!contextModel) {
144
+ return errorHandler(new Error('Model is undefined.'), req, res, next);
145
+ }
128
146
  if (options.findOneAndRemove) {
129
147
  options.contextFilter(contextModel, req, (filteredContext)=>{
130
148
  // @ts-expect-error this is fine 🐢πŸ”₯
@@ -146,7 +164,10 @@ function operations(model, options, filter) {
146
164
  }
147
165
  };
148
166
  const createObject = function(req, res, next) {
149
- const contextModel = model;
167
+ const contextModel = req.erm.model;
168
+ if (!contextModel) {
169
+ return errorHandler(new Error('Model is undefined.'), req, res, next);
170
+ }
150
171
  req.body = filter.filterObject(req.body || {}, {
151
172
  access: req.access,
152
173
  modelName: model.modelName,
@@ -171,7 +192,10 @@ function operations(model, options, filter) {
171
192
  }).catch((err)=>errorHandler(err, req, res, next));
172
193
  };
173
194
  const modifyObject = function(req, res, next) {
174
- const contextModel = model;
195
+ const contextModel = req.erm.model;
196
+ if (!contextModel) {
197
+ return errorHandler(new Error('Model is undefined.'), req, res, next);
198
+ }
175
199
  req.body = filter.filterObject(req.body || {}, {
176
200
  access: req.access,
177
201
  modelName: model.modelName,
@@ -187,6 +211,7 @@ function operations(model, options, filter) {
187
211
  function depopulate(src) {
188
212
  const dst = {};
189
213
  for (const [key, value] of Object.entries(src)){
214
+ // @ts-expect-error this is fine 🐢πŸ”₯
190
215
  const path = contextModel.schema.path(key);
191
216
  // @ts-expect-error this is fine 🐢πŸ”₯
192
217
  // Add support for Mongoose 7 and 8 while keeping backwards-compatibility to 6 by allowing ObjectID and ObejctId
@@ -216,7 +241,10 @@ function operations(model, options, filter) {
216
241
  }
217
242
  return dst;
218
243
  }
219
- const cleanBody = (0, _moredots.moredots)(depopulate(req.body));
244
+ let cleanBody = depopulate(req.body);
245
+ if (options.updateDeep) {
246
+ cleanBody = (0, _moredots.moredots)(cleanBody);
247
+ }
220
248
  if (options.findOneAndUpdate) {
221
249
  options.contextFilter(contextModel, req, (filteredContext)=>{
222
250
  // @ts-expect-error this is fine 🐢πŸ”₯
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-restify-mongoose",
3
- "version": "9.0.2",
3
+ "version": "9.0.4",
4
4
  "description": "Easily create a flexible REST interface for mongoose models",
5
5
  "keywords": [
6
6
  "ReST",