express-restify-mongoose 9.0.1 β 9.0.3
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.
|
@@ -79,28 +79,32 @@ function serve(app, model, options = {}) {
|
|
|
79
79
|
// @ts-expect-error restify
|
|
80
80
|
app.delete = app.del;
|
|
81
81
|
}
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
// @ts-expect-error restify
|
|
83
|
+
const modelMiddleware = async (req, res, next)=>{
|
|
84
|
+
const getModel = serveOptions?.modelFactory?.getModel;
|
|
85
|
+
req.erm = {
|
|
86
|
+
model: typeof getModel === 'function' ? await getModel(req) : model
|
|
87
|
+
};
|
|
84
88
|
next();
|
|
85
|
-
}
|
|
89
|
+
};
|
|
86
90
|
const accessMiddleware = serveOptions.access ? (0, _access.getAccessHandler)({
|
|
87
91
|
access: serveOptions.access,
|
|
88
92
|
idProperty: serveOptions.idProperty,
|
|
89
93
|
onError: serveOptions.onError
|
|
90
94
|
}) : [];
|
|
91
95
|
const ensureContentType = (0, _ensureContentType.getEnsureContentTypeHandler)(serveOptions);
|
|
92
|
-
const filterAndFindById = (0, _filterAndFindById.getFilterAndFindByIdHandler)(serveOptions
|
|
96
|
+
const filterAndFindById = (0, _filterAndFindById.getFilterAndFindByIdHandler)(serveOptions);
|
|
93
97
|
const prepareQuery = (0, _prepareQuery.getPrepareQueryHandler)(serveOptions);
|
|
94
98
|
const prepareOutput = (0, _prepareOutput.getPrepareOutputHandler)(serveOptions, model.modelName, filter);
|
|
95
|
-
app.get(uriItems, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getItems, prepareOutput);
|
|
96
|
-
app.get(uriCount, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getCount, prepareOutput);
|
|
97
|
-
app.get(uriItem, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getItem, prepareOutput);
|
|
98
|
-
app.get(uriShallow, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getShallow, prepareOutput);
|
|
99
|
-
app.post(uriItems, prepareQuery, ensureContentType, serveOptions.preMiddleware, serveOptions.preCreate, accessMiddleware, ops.createObject, prepareOutput);
|
|
100
|
-
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);
|
|
101
|
-
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);
|
|
102
|
-
app.patch(uriItem, prepareQuery, ensureContentType, serveOptions.preMiddleware, serveOptions.findOneAndUpdate ? [] : filterAndFindById, serveOptions.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput);
|
|
103
|
-
app.delete(uriItems, prepareQuery, serveOptions.preMiddleware, serveOptions.preDelete, ops.deleteItems, prepareOutput);
|
|
104
|
-
app.delete(uriItem, prepareQuery, serveOptions.preMiddleware, serveOptions.findOneAndRemove ? [] : filterAndFindById, serveOptions.preDelete, ops.deleteItem, prepareOutput);
|
|
99
|
+
app.get(uriItems, modelMiddleware, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getItems, prepareOutput);
|
|
100
|
+
app.get(uriCount, modelMiddleware, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getCount, prepareOutput);
|
|
101
|
+
app.get(uriItem, modelMiddleware, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getItem, prepareOutput);
|
|
102
|
+
app.get(uriShallow, modelMiddleware, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, accessMiddleware, ops.getShallow, prepareOutput);
|
|
103
|
+
app.post(uriItems, modelMiddleware, prepareQuery, ensureContentType, serveOptions.preMiddleware, serveOptions.preCreate, accessMiddleware, ops.createObject, prepareOutput);
|
|
104
|
+
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);
|
|
105
|
+
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);
|
|
106
|
+
app.patch(uriItem, modelMiddleware, prepareQuery, ensureContentType, serveOptions.preMiddleware, serveOptions.findOneAndUpdate ? [] : filterAndFindById, serveOptions.preUpdate, accessMiddleware, ops.modifyObject, prepareOutput);
|
|
107
|
+
app.delete(uriItems, modelMiddleware, prepareQuery, serveOptions.preMiddleware, serveOptions.preDelete, ops.deleteItems, prepareOutput);
|
|
108
|
+
app.delete(uriItem, modelMiddleware, prepareQuery, serveOptions.preMiddleware, serveOptions.findOneAndRemove ? [] : filterAndFindById, serveOptions.preDelete, ops.deleteItem, prepareOutput);
|
|
105
109
|
return uriItems;
|
|
106
110
|
}
|
|
@@ -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
|
|
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
|
}
|
package/dist/operations.js
CHANGED
|
@@ -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
|