Monk Middleware Options

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

A middleware plugin for Monk (a MongoDB driver) that parses options passed to collection methods. It currently supports the 'safe' option and is intended for use with Monk v7 and earlier. The package is part of the Monk middleware ecosystem and has not been actively developed, with the last release (0.2.1) occurring alongside Monk v4. For modern Monk versions (v7+), option parsing is handled internally or via other middleware such as monk-middleware-cast-ids.

error TypeError: db.get(...).use is not a function
cause Monk v6+ no longer supports .use() for middleware; middleware API was removed.
fix
Upgrade to Monk v6+ and remove calls to .use(). Options are passed directly to methods.
error Error: options.safe is not a function
cause The 'safe' option is deprecated and should be replaced with writeConcern.
fix
Use writeConcern instead: { writeConcern: { w: 1 } }.
deprecated The 'safe' option is deprecated in MongoDB driver 3.0 and later.
fix Use writeConcern instead. For Monk v7+, writeConcern is automatically set via connection URI or driver options.
gotcha This middleware only parses the 'safe' option; other options (e.g., writeConcern) are not handled.
fix Use monk-middleware-cast-ids or handle other options manually.
breaking Monk v6 removed the middleware option parsing mechanism; monk-middleware-options is no longer needed.
fix Remove usage of this middleware; options are now passed directly to Monk methods.
npm install monk-middleware-options
yarn add monk-middleware-options
pnpm add monk-middleware-options

Demonstrates basic usage: requiring monk, applying the options middleware to a collection, and using the 'safe' option.

const monk = require('monk');
const options = require('monk-middleware-options');

// Use with Monk's collection middleware
const db = monk('localhost/mydb');
const users = db.get('users', { castIds: false });
users.use(options());

// Now the 'safe' option will be parsed
users.insert({ name: 'John' }, { safe: true }).then(doc => {
  console.log('Inserted:', doc);
}).catch(err => {
  console.error(err);
});