From e7f330e310ee7814d46dd5e96935905297c9d646 Mon Sep 17 00:00:00 2001 From: T-vK Date: Tue, 12 Mar 2024 11:19:32 +0100 Subject: [PATCH] Fix a modelFactory bug --- package.json | 2 +- src/express-restify-mongoose.ts | 17 +++++++++--- src/middleware/filterAndFindById.ts | 8 +++--- src/operations.ts | 41 +++++++++++++++++++++++------ 4 files changed, 53 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index d214161..64d207a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "express-restify-mongoose", - "version": "9.0.2", + "version": "9.0.3", "description": "Easily create a flexible REST interface for mongoose models", "keywords": [ "ReST", diff --git a/src/express-restify-mongoose.ts b/src/express-restify-mongoose.ts index a2409ca..d426cf3 100644 --- a/src/express-restify-mongoose.ts +++ b/src/express-restify-mongoose.ts @@ -96,7 +96,8 @@ export function serve( app.delete = app.del; } - app.use(async (req, res, next) => { + // @ts-expect-error restify + const modelMiddleware = async (req, res, next) => { const getModel = serveOptions?.modelFactory?.getModel; req.erm = { @@ -104,7 +105,7 @@ export function serve( }; next(); - }); + }; const accessMiddleware = serveOptions.access ? getAccessHandler({ @@ -115,7 +116,7 @@ export function serve( : []; const ensureContentType = getEnsureContentTypeHandler(serveOptions); - const filterAndFindById = getFilterAndFindByIdHandler(serveOptions, model); + const filterAndFindById = getFilterAndFindByIdHandler(serveOptions); const prepareQuery = getPrepareQueryHandler(serveOptions); const prepareOutput = getPrepareOutputHandler( serveOptions, @@ -125,6 +126,7 @@ export function serve( app.get( uriItems, + modelMiddleware, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, @@ -135,6 +137,7 @@ export function serve( app.get( uriCount, + modelMiddleware, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, @@ -145,6 +148,7 @@ export function serve( app.get( uriItem, + modelMiddleware, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, @@ -155,6 +159,7 @@ export function serve( app.get( uriShallow, + modelMiddleware, prepareQuery, serveOptions.preMiddleware, serveOptions.preRead, @@ -165,6 +170,7 @@ export function serve( app.post( uriItems, + modelMiddleware, prepareQuery, ensureContentType, serveOptions.preMiddleware, @@ -176,6 +182,7 @@ export function serve( app.post( uriItem, + modelMiddleware, deprecate( prepareQuery, "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( app.put( uriItem, + modelMiddleware, deprecate( prepareQuery, "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( app.patch( uriItem, + modelMiddleware, prepareQuery, ensureContentType, serveOptions.preMiddleware, @@ -218,6 +227,7 @@ export function serve( app.delete( uriItems, + modelMiddleware, prepareQuery, serveOptions.preMiddleware, serveOptions.preDelete, @@ -227,6 +237,7 @@ export function serve( app.delete( uriItem, + modelMiddleware, prepareQuery, serveOptions.preMiddleware, serveOptions.findOneAndRemove ? [] : filterAndFindById, diff --git a/src/middleware/filterAndFindById.ts b/src/middleware/filterAndFindById.ts index f1110d6..6511d7e 100644 --- a/src/middleware/filterAndFindById.ts +++ b/src/middleware/filterAndFindById.ts @@ -8,13 +8,15 @@ export function getFilterAndFindByIdHandler( options: Pick< Options, "contextFilter" | "idProperty" | "onError" | "readPreference" - >, - model: mongoose.Model + > ) { const errorHandler = getErrorHandler(options); const fn: RequestHandler = function filterAndFindById(req, res, next) { - const contextModel = model; + const contextModel = req.erm.model; + if (!contextModel) { + return errorHandler(new Error('Model is undefined.'), req, res, next); + } if (!req.params.id) { return next(); diff --git a/src/operations.ts b/src/operations.ts index 72fff20..245e165 100644 --- a/src/operations.ts +++ b/src/operations.ts @@ -51,7 +51,10 @@ export function operations( } const getItems: RequestHandler = function (req, res, next) { - const contextModel = model; + const contextModel = req.erm.model; + if (!contextModel) { + return errorHandler(new Error('Model is undefined.'), req, res, next); + } if (isDistinctExcluded(req)) { req.erm.result = []; @@ -91,7 +94,10 @@ export function operations( }; const getCount: RequestHandler = function (req, res, next) { - const contextModel = model; + const contextModel = req.erm.model; + if (!contextModel) { + return errorHandler(new Error('Model is undefined.'), req, res, next); + } options.contextFilter(contextModel, req, (filteredContext) => { buildQuery(filteredContext.countDocuments(), req.erm.query) @@ -106,7 +112,10 @@ export function operations( }; const getShallow: RequestHandler = function (req, res, next) { - const contextModel = model; + const contextModel = req.erm.model; + if (!contextModel) { + return errorHandler(new Error('Model is undefined.'), req, res, next); + } options.contextFilter(contextModel, req, (filteredContext) => { buildQuery | null>( @@ -136,7 +145,10 @@ export function operations( }; const deleteItems: RequestHandler = function (req, res, next) { - const contextModel = model; + const contextModel = req.erm.model; + if (!contextModel) { + return errorHandler(new Error('Model is undefined.'), req, res, next); + } options.contextFilter(contextModel, req, (filteredContext) => { buildQuery(filteredContext.deleteMany(), req.erm.query) @@ -150,7 +162,10 @@ export function operations( }; const getItem: RequestHandler = function (req, res, next) { - const contextModel = model; + const contextModel = req.erm.model; + if (!contextModel) { + return errorHandler(new Error('Model is undefined.'), req, res, next); + } if (isDistinctExcluded(req)) { req.erm.result = []; @@ -179,7 +194,10 @@ export function operations( }; const deleteItem: RequestHandler = function (req, res, next) { - const contextModel = model; + const contextModel = req.erm.model; + if (!contextModel) { + return errorHandler(new Error('Model is undefined.'), req, res, next); + } if (options.findOneAndRemove) { options.contextFilter(contextModel, req, (filteredContext) => { @@ -210,7 +228,10 @@ export function operations( }; const createObject: RequestHandler = function (req, res, next) { - const contextModel = model; + const contextModel = req.erm.model; + if (!contextModel) { + return errorHandler(new Error('Model is undefined.'), req, res, next); + } req.body = filter.filterObject(req.body || {}, { access: req.access, @@ -245,7 +266,10 @@ export function operations( }; const modifyObject: RequestHandler = function (req, res, next) { - const contextModel = model; + const contextModel = req.erm.model; + if (!contextModel) { + return errorHandler(new Error('Model is undefined.'), req, res, next); + } req.body = filter.filterObject(req.body || {}, { access: req.access, @@ -266,6 +290,7 @@ export function operations( const dst: Record = {}; for (const [key, value] of Object.entries(src)) { + // @ts-expect-error this is fine 🐢πŸ”₯ const path = contextModel.schema.path(key); // @ts-expect-error this is fine 🐢πŸ”₯