Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a modelFactory bug #459

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
17 changes: 14 additions & 3 deletions src/express-restify-mongoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,16 @@ 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 = {
model: typeof getModel === 'function' ? await getModel(req) : model,
};

next();
});
};

const accessMiddleware = serveOptions.access
? getAccessHandler({
Expand All @@ -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,
Expand All @@ -125,6 +126,7 @@ export function serve(

app.get(
uriItems,
modelMiddleware,
prepareQuery,
serveOptions.preMiddleware,
serveOptions.preRead,
Expand All @@ -135,6 +137,7 @@ export function serve(

app.get(
uriCount,
modelMiddleware,
prepareQuery,
serveOptions.preMiddleware,
serveOptions.preRead,
Expand All @@ -145,6 +148,7 @@ export function serve(

app.get(
uriItem,
modelMiddleware,
prepareQuery,
serveOptions.preMiddleware,
serveOptions.preRead,
Expand All @@ -155,6 +159,7 @@ export function serve(

app.get(
uriShallow,
modelMiddleware,
prepareQuery,
serveOptions.preMiddleware,
serveOptions.preRead,
Expand All @@ -165,6 +170,7 @@ export function serve(

app.post(
uriItems,
modelMiddleware,
prepareQuery,
ensureContentType,
serveOptions.preMiddleware,
Expand All @@ -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."
Expand All @@ -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."
Expand All @@ -206,6 +214,7 @@ export function serve(

app.patch(
uriItem,
modelMiddleware,
prepareQuery,
ensureContentType,
serveOptions.preMiddleware,
Expand All @@ -218,6 +227,7 @@ export function serve(

app.delete(
uriItems,
modelMiddleware,
prepareQuery,
serveOptions.preMiddleware,
serveOptions.preDelete,
Expand All @@ -227,6 +237,7 @@ export function serve(

app.delete(
uriItem,
modelMiddleware,
prepareQuery,
serveOptions.preMiddleware,
serveOptions.findOneAndRemove ? [] : filterAndFindById,
Expand Down
8 changes: 5 additions & 3 deletions src/middleware/filterAndFindById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ export function getFilterAndFindByIdHandler(
options: Pick<
Options,
"contextFilter" | "idProperty" | "onError" | "readPreference"
>,
model: mongoose.Model<unknown>
>
) {
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();
Expand Down
41 changes: 33 additions & 8 deletions src/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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)
Expand All @@ -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<Record<string, unknown> | null>(
Expand Down Expand Up @@ -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)
Expand All @@ -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 = [];
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -266,6 +290,7 @@ export function operations(
const dst: Record<string, unknown> = {};

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 🐶🔥
Expand Down
Loading