Monk Middleware Query

raw JSON →
0.2.0 verified Sat Apr 25 auth: no javascript maintenance

monk-middleware-query is a middleware for the Monk MongoDB library (version 0.2.0) that parses and transforms query objects. It allows you to define query processing logic that runs before database operations, such as modifying or validating query conditions. This package is part of the Monk ecosystem and is intended to be used with Monk's middleware pipeline. It is stable but minimally documented, with no recent updates. Key differentiator: it provides a structured way to intercept and manipulate queries in Monk applications.

error TypeError: db.use is not a function
cause Using the middleware before initializing the Monk database connection.
fix
Ensure db is a Monk database instance (e.g., const db = monk('uri')) before calling db.use().
error Cannot find module 'monk-middleware-query'
cause The package is not installed or the import path is incorrect.
fix
Run npm install monk-middleware-query and verify the import statement.
error Middleware must be a function
cause Passing the middleware module directly instead of calling it as a function.
fix
Use db.use(queryMiddleware()) instead of db.use(queryMiddleware).
gotcha The middleware must be called as a function (e.g., queryMiddleware()) before passing to .use(), not just passed as a reference.
fix Use db.use(queryMiddleware()) instead of db.use(queryMiddleware).
gotcha This package is deprecated or not actively maintained. It may not work with newer versions of Monk (>=7.0.0) due to breaking changes in Monk's middleware API.
fix Consider using Monk's built-in middleware or another middleware package compatible with your Monk version.
gotcha The package has no README or documentation. Understanding its behavior requires reading the source code.
fix Refer to the GitHub source for usage details.
npm install monk-middleware-query
yarn add monk-middleware-query
pnpm add monk-middleware-query

Demonstrates how to import and apply the query middleware to a Monk database instance.

import monk from 'monk';
import queryMiddleware from 'monk-middleware-query';

const db = monk('localhost/mydb');
db.use(queryMiddleware());

const users = db.get('users');
// With middleware, you can transform queries: for example, enforce a filter
// The middleware processes the query before it reaches the database.
const docs = await users.find({ age: { $gt: 20 } });
console.log(docs);