Skip to content

Commit 86e5447

Browse files
Merge pull request #459 from T-vK/main
Fix a modelFactory bug
2 parents 934e66a + e7f330e commit 86e5447

File tree

4 files changed

+53
-15
lines changed

4 files changed

+53
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "express-restify-mongoose",
3-
"version": "9.0.2",
3+
"version": "9.0.3",
44
"description": "Easily create a flexible REST interface for mongoose models",
55
"keywords": [
66
"ReST",

src/express-restify-mongoose.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,16 @@ export function serve(
9696
app.delete = app.del;
9797
}
9898

99-
app.use(async (req, res, next) => {
99+
// @ts-expect-error restify
100+
const modelMiddleware = async (req, res, next) => {
100101
const getModel = serveOptions?.modelFactory?.getModel;
101102

102103
req.erm = {
103104
model: typeof getModel === 'function' ? await getModel(req) : model,
104105
};
105106

106107
next();
107-
});
108+
};
108109

109110
const accessMiddleware = serveOptions.access
110111
? getAccessHandler({
@@ -115,7 +116,7 @@ export function serve(
115116
: [];
116117

117118
const ensureContentType = getEnsureContentTypeHandler(serveOptions);
118-
const filterAndFindById = getFilterAndFindByIdHandler(serveOptions, model);
119+
const filterAndFindById = getFilterAndFindByIdHandler(serveOptions);
119120
const prepareQuery = getPrepareQueryHandler(serveOptions);
120121
const prepareOutput = getPrepareOutputHandler(
121122
serveOptions,
@@ -125,6 +126,7 @@ export function serve(
125126

126127
app.get(
127128
uriItems,
129+
modelMiddleware,
128130
prepareQuery,
129131
serveOptions.preMiddleware,
130132
serveOptions.preRead,
@@ -135,6 +137,7 @@ export function serve(
135137

136138
app.get(
137139
uriCount,
140+
modelMiddleware,
138141
prepareQuery,
139142
serveOptions.preMiddleware,
140143
serveOptions.preRead,
@@ -145,6 +148,7 @@ export function serve(
145148

146149
app.get(
147150
uriItem,
151+
modelMiddleware,
148152
prepareQuery,
149153
serveOptions.preMiddleware,
150154
serveOptions.preRead,
@@ -155,6 +159,7 @@ export function serve(
155159

156160
app.get(
157161
uriShallow,
162+
modelMiddleware,
158163
prepareQuery,
159164
serveOptions.preMiddleware,
160165
serveOptions.preRead,
@@ -165,6 +170,7 @@ export function serve(
165170

166171
app.post(
167172
uriItems,
173+
modelMiddleware,
168174
prepareQuery,
169175
ensureContentType,
170176
serveOptions.preMiddleware,
@@ -176,6 +182,7 @@ export function serve(
176182

177183
app.post(
178184
uriItem,
185+
modelMiddleware,
179186
deprecate(
180187
prepareQuery,
181188
"express-restify-mongoose: in a future major version, the POST method to update resources will be removed. Use PATCH instead."
@@ -191,6 +198,7 @@ export function serve(
191198

192199
app.put(
193200
uriItem,
201+
modelMiddleware,
194202
deprecate(
195203
prepareQuery,
196204
"express-restify-mongoose: in a future major version, the PUT method will replace rather than update a resource. Use PATCH instead."
@@ -206,6 +214,7 @@ export function serve(
206214

207215
app.patch(
208216
uriItem,
217+
modelMiddleware,
209218
prepareQuery,
210219
ensureContentType,
211220
serveOptions.preMiddleware,
@@ -218,6 +227,7 @@ export function serve(
218227

219228
app.delete(
220229
uriItems,
230+
modelMiddleware,
221231
prepareQuery,
222232
serveOptions.preMiddleware,
223233
serveOptions.preDelete,
@@ -227,6 +237,7 @@ export function serve(
227237

228238
app.delete(
229239
uriItem,
240+
modelMiddleware,
230241
prepareQuery,
231242
serveOptions.preMiddleware,
232243
serveOptions.findOneAndRemove ? [] : filterAndFindById,

src/middleware/filterAndFindById.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ export function getFilterAndFindByIdHandler(
88
options: Pick<
99
Options,
1010
"contextFilter" | "idProperty" | "onError" | "readPreference"
11-
>,
12-
model: mongoose.Model<unknown>
11+
>
1312
) {
1413
const errorHandler = getErrorHandler(options);
1514

1615
const fn: RequestHandler = function filterAndFindById(req, res, next) {
17-
const contextModel = model;
16+
const contextModel = req.erm.model;
17+
if (!contextModel) {
18+
return errorHandler(new Error('Model is undefined.'), req, res, next);
19+
}
1820

1921
if (!req.params.id) {
2022
return next();

src/operations.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ export function operations(
5151
}
5252

5353
const getItems: RequestHandler = function (req, res, next) {
54-
const contextModel = model;
54+
const contextModel = req.erm.model;
55+
if (!contextModel) {
56+
return errorHandler(new Error('Model is undefined.'), req, res, next);
57+
}
5558

5659
if (isDistinctExcluded(req)) {
5760
req.erm.result = [];
@@ -91,7 +94,10 @@ export function operations(
9194
};
9295

9396
const getCount: RequestHandler = function (req, res, next) {
94-
const contextModel = model;
97+
const contextModel = req.erm.model;
98+
if (!contextModel) {
99+
return errorHandler(new Error('Model is undefined.'), req, res, next);
100+
}
95101

96102
options.contextFilter(contextModel, req, (filteredContext) => {
97103
buildQuery(filteredContext.countDocuments(), req.erm.query)
@@ -106,7 +112,10 @@ export function operations(
106112
};
107113

108114
const getShallow: RequestHandler = function (req, res, next) {
109-
const contextModel = model;
115+
const contextModel = req.erm.model;
116+
if (!contextModel) {
117+
return errorHandler(new Error('Model is undefined.'), req, res, next);
118+
}
110119

111120
options.contextFilter(contextModel, req, (filteredContext) => {
112121
buildQuery<Record<string, unknown> | null>(
@@ -136,7 +145,10 @@ export function operations(
136145
};
137146

138147
const deleteItems: RequestHandler = function (req, res, next) {
139-
const contextModel = model;
148+
const contextModel = req.erm.model;
149+
if (!contextModel) {
150+
return errorHandler(new Error('Model is undefined.'), req, res, next);
151+
}
140152

141153
options.contextFilter(contextModel, req, (filteredContext) => {
142154
buildQuery(filteredContext.deleteMany(), req.erm.query)
@@ -150,7 +162,10 @@ export function operations(
150162
};
151163

152164
const getItem: RequestHandler = function (req, res, next) {
153-
const contextModel = model;
165+
const contextModel = req.erm.model;
166+
if (!contextModel) {
167+
return errorHandler(new Error('Model is undefined.'), req, res, next);
168+
}
154169

155170
if (isDistinctExcluded(req)) {
156171
req.erm.result = [];
@@ -179,7 +194,10 @@ export function operations(
179194
};
180195

181196
const deleteItem: RequestHandler = function (req, res, next) {
182-
const contextModel = model;
197+
const contextModel = req.erm.model;
198+
if (!contextModel) {
199+
return errorHandler(new Error('Model is undefined.'), req, res, next);
200+
}
183201

184202
if (options.findOneAndRemove) {
185203
options.contextFilter(contextModel, req, (filteredContext) => {
@@ -210,7 +228,10 @@ export function operations(
210228
};
211229

212230
const createObject: RequestHandler = function (req, res, next) {
213-
const contextModel = model;
231+
const contextModel = req.erm.model;
232+
if (!contextModel) {
233+
return errorHandler(new Error('Model is undefined.'), req, res, next);
234+
}
214235

215236
req.body = filter.filterObject(req.body || {}, {
216237
access: req.access,
@@ -245,7 +266,10 @@ export function operations(
245266
};
246267

247268
const modifyObject: RequestHandler = function (req, res, next) {
248-
const contextModel = model;
269+
const contextModel = req.erm.model;
270+
if (!contextModel) {
271+
return errorHandler(new Error('Model is undefined.'), req, res, next);
272+
}
249273

250274
req.body = filter.filterObject(req.body || {}, {
251275
access: req.access,
@@ -266,6 +290,7 @@ export function operations(
266290
const dst: Record<string, unknown> = {};
267291

268292
for (const [key, value] of Object.entries(src)) {
293+
// @ts-expect-error this is fine 🐶🔥
269294
const path = contextModel.schema.path(key);
270295

271296
// @ts-expect-error this is fine 🐶🔥

0 commit comments

Comments
 (0)