Registry / database / monk-middleware-cast-ids

monk-middleware-cast-ids

JSON →
library0.2.1jsnpmunverified

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.

npm install monk-middleware-cast-ids
INSTALL
IMPORT
SIG · MONK-MIDDLEWARE-CA
M
monk-middleware-cast-ids
databasejavascriptv0.2.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import castIds from 'monk-middleware-cast-ids'
const castIds = require('monk-middleware-cast-ids')
Package exports a default function. CommonJS require might work but ESM-style import is recommended.
castIds (in CommonJS)
const castIds = require('monk-middleware-cast-ids').default
const castIds = require('monk-middleware-cast-ids')
If using require in a CommonJS environment, note that the module might use ESM, requiring .default access. Check your bundler.
TypeScript types
import castIds from 'monk-middleware-cast-ids'
import { castIds } from 'monk-middleware-cast-ids'
No named export 'castIds', so braces will cause runtime error.

Demonstrates how to add the cast-ids middleware to a Monk collection, enabling automatic string-to-ObjectId conversion.

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);
Debug
Known issues
deprecatedThis 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.
affects: >=0.2.1
breakingMonk v7 removed middleware support entirely; this package will not work.
fix
Upgrade to Monk's native castIds option or use Monk v6 compatibility.
affects: >=7.0.0
gotchaIf 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.
affects: >=0.2.1
gotchaThe 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.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: castIds is not a function
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;
Cannot find module 'monk-middleware-cast-ids'
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.
CastError: Cast to ObjectId failed for value "invalid" at path "_id"
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: Cannot add middleware after first operation
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.
Upgrade
Version history
0.2.1latest on npm
Audit
Dependencies
monkrequiredThis is a middleware for monk and requires it to be installed as a peer dependency.
Agent activity
4 hits · last 30 days
node
4
Resources
monk-middleware-cast-ids — npm install monk-middleware-cast-ids · libregistry