{"id":18568,"library":"monk-middleware-cast-ids","title":"monk-middleware-cast-ids","description":"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.","status":"deprecated","version":"0.2.1","language":"javascript","source_language":"en","source_url":"git://github.com/Automattic/monk","tags":["javascript","monk","middleware","ids"],"install":[{"cmd":"npm install monk-middleware-cast-ids","lang":"bash","label":"npm"},{"cmd":"yarn add monk-middleware-cast-ids","lang":"bash","label":"yarn"},{"cmd":"pnpm add monk-middleware-cast-ids","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"This is a middleware for monk and requires it to be installed as a peer dependency.","package":"monk","optional":false}],"imports":[{"note":"Package exports a default function. CommonJS require might work but ESM-style import is recommended.","wrong":"const castIds = require('monk-middleware-cast-ids')","symbol":"default","correct":"import castIds from 'monk-middleware-cast-ids'"},{"note":"If using require in a CommonJS environment, note that the module might use ESM, requiring .default access. Check your bundler.","wrong":"const castIds = require('monk-middleware-cast-ids')","symbol":"castIds (in CommonJS)","correct":"const castIds = require('monk-middleware-cast-ids').default"},{"note":"No named export 'castIds', so braces will cause runtime error.","wrong":"import { castIds } from 'monk-middleware-cast-ids'","symbol":"TypeScript types","correct":"import castIds from 'monk-middleware-cast-ids'"}],"quickstart":{"code":"import monk from 'monk';\nimport castIds from 'monk-middleware-cast-ids';\n\nconst db = monk('localhost/myapp', {\n  useUnifiedTopology: true,\n});\nconst collection = db.get('users');\ncollection.addMiddleware(castIds);\n\n// Now string IDs are automatically cast to ObjectId\nconst result = await collection.findOne({ _id: '507f1f77bcf86cd799439011' });\nconsole.log(result);","lang":"typescript","description":"Demonstrates how to add the cast-ids middleware to a Monk collection, enabling automatic string-to-ObjectId conversion."},"warnings":[{"fix":"In Monk v6+, set { castIds: true } when connecting or on collection options.","message":"This package is no longer maintained. Use Monk's built-in 'castIds' option instead.","severity":"deprecated","affected_versions":">=0.2.1"},{"fix":"Upgrade to Monk's native castIds option or use Monk v6 compatibility.","message":"Monk v7 removed middleware support entirely; this package will not work.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"Use import castIds from 'monk-middleware-cast-ids' without braces.","message":"If using ESM, the default export is a function, not an object. Using named import { castIds } will fail silently or throw.","severity":"gotcha","affected_versions":">=0.2.1"},{"fix":"Validate or sanitize input IDs before passing to methods.","message":"The middleware only casts strings that are valid 24-character hex strings; invalid strings will cause a cast error.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use const castIds = require('monk-middleware-cast-ids').default;","cause":"Using CommonJS require incorrectly: const castIds = require('monk-middleware-cast-ids') returns an object with a default property.","error":"TypeError: castIds is not a function"},{"fix":"Install package: npm install monk-middleware-cast-ids; check spelling; confirm Monk version <7.","cause":"Package not installed; misspelled name; or used in Monk v7 where middleware is not supported.","error":"Cannot find module 'monk-middleware-cast-ids'"},{"fix":"Ensure IDs are valid 24-character hex strings (e.g., '507f1f77bcf86cd799439011') before passing them.","cause":"The middleware attempted to cast a string that is not a valid 24-character hex ObjectId.","error":"CastError: Cast to ObjectId failed for value \"invalid\" at path \"_id\""},{"fix":"Add middleware immediately after getting the collection, before any find/insert/etc.","cause":"Middleware must be added before any database operations are performed on the collection.","error":"Error: Cannot add middleware after first operation"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}