monk-middleware-cast-ids
raw JSON → 0.2.1 verified Sat Apr 25 auth: no javascript deprecated
A middleware for the Monk MongoDB library that automatically casts string IDs to MongoDB ObjectId instances. Version 0.2.1 is the latest stable release. This package is specific to Monk's middleware system and is no longer actively maintained; it is superseded by Monk's built-in castIds option. Key differentiator: it allows automatic ID casting in older Monk versions that lack native support.
Common errors
error TypeError: castIds is not a function ↓
cause Using CommonJS require incorrectly: const castIds = require('monk-middleware-cast-ids') returns an object with a default property.
fix
Use const castIds = require('monk-middleware-cast-ids').default;
error Cannot find module 'monk-middleware-cast-ids' ↓
cause Package not installed; misspelled name; or used in Monk v7 where middleware is not supported.
fix
Install package: npm install monk-middleware-cast-ids; check spelling; confirm Monk version <7.
error CastError: Cast to ObjectId failed for value "invalid" at path "_id" ↓
cause The middleware attempted to cast a string that is not a valid 24-character hex ObjectId.
fix
Ensure IDs are valid 24-character hex strings (e.g., '507f1f77bcf86cd799439011') before passing them.
error Error: Cannot add middleware after first operation ↓
cause Middleware must be added before any database operations are performed on the collection.
fix
Add middleware immediately after getting the collection, before any find/insert/etc.
Warnings
deprecated This package is no longer maintained. Use Monk's built-in 'castIds' option instead. ↓
fix In Monk v6+, set { castIds: true } when connecting or on collection options.
breaking Monk v7 removed middleware support entirely; this package will not work. ↓
fix Upgrade to Monk's native castIds option or use Monk v6 compatibility.
gotcha If using ESM, the default export is a function, not an object. Using named import { castIds } will fail silently or throw. ↓
fix Use import castIds from 'monk-middleware-cast-ids' without braces.
gotcha The middleware only casts strings that are valid 24-character hex strings; invalid strings will cause a cast error. ↓
fix Validate or sanitize input IDs before passing to methods.
Install
npm install monk-middleware-cast-ids yarn add monk-middleware-cast-ids pnpm add monk-middleware-cast-ids Imports
- default wrong
const castIds = require('monk-middleware-cast-ids')correctimport castIds from 'monk-middleware-cast-ids' - castIds (in CommonJS) wrong
const castIds = require('monk-middleware-cast-ids')correctconst castIds = require('monk-middleware-cast-ids').default - TypeScript types wrong
import { castIds } from 'monk-middleware-cast-ids'correctimport castIds from 'monk-middleware-cast-ids'
Quickstart
import monk from 'monk';
import castIds from 'monk-middleware-cast-ids';
const db = monk('localhost/myapp', {
useUnifiedTopology: true,
});
const collection = db.get('users');
collection.addMiddleware(castIds);
// Now string IDs are automatically cast to ObjectId
const result = await collection.findOne({ _id: '507f1f77bcf86cd799439011' });
console.log(result);