Registry / database / mongoose-express-middleware

mongoose-express-middleware

JSON →
library3.0.3jsnpmunverified

Opinionated Express middleware that auto-generates CRUD endpoints (find, findById, create, update, delete, count, aggregate) from Mongoose schemas. Stable v3.0.3, actively maintained, ships TypeScript types. Key differentiator: minimal configuration for rapid REST API prototyping with built-in support for default filters, bulk operations, and multi-status responses for array inputs. Requires peer dependency mongoose ^8.20.0.

npm install mongoose-express-middleware
INSTALL
IMPORT
SIG · MONGOOSE-EXPRESS-M
M
mongoose-express-middleware
databasejavascriptv3.0.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

MongooseExpressMiddleware
import MongooseExpressMiddleware from 'mongoose-express-middleware'
const MongooseExpressMiddleware = require('mongoose-express-middleware').default
Default export; no named exports. ESM works directly; CJS with default property is incorrect.
MongooseExpressMiddleware class
const mw = new MongooseExpressMiddleware(modelName, schema, options)
const mw = new MongooseExpressMiddleware.default(modelName, schema, options)
Constructor expects exactly 2-3 arguments. Do not destructure.
TypeScript types
import type { MongooseExpressMiddlewareOptions } from 'mongoose-express-middleware'
import { MongooseExpressMiddlewareOptions } from 'mongoose-express-middleware'
Type-only import to avoid runtime side effects. Options interface is exported.

Initializes MongooseExpressMiddleware with a custom schema and options, then attaches CRUD middleware to an Express app.

import express from 'express'; import Mongoose from 'mongoose'; import MongooseExpressMiddleware from 'mongoose-express-middleware'; const schema = new Mongoose.Schema({ _id: { type: String }, name: { type: String }, description: { type: String }, age: { type: Number } }); const modelName = 'foobar'; const options = { collectionName: 'foobar', defaultFilter: { age: { $gte: 10 } } }; const crud = new MongooseExpressMiddleware(modelName, schema, options); const app = express(); app.use(express.json()); app.get('/', crud.find); app.get('/:id', crud.findById); app.get('/utils/count', crud.count); app.post('/', crud.create); app.put('/:id', crud.update); app.delete('/', crud.deleteMany); app.delete('/:id', crud.deleteById); app.post('/utils/aggregate', crud.aggregate); app.listen(8080);
Debug
Known issues
breakingVersion 3.x requires mongoose >=8.20.0. Mongoose 7.x is not compatible.
fix
Upgrade mongoose to 8.20.0 or later.
affects: >=3.0.0
deprecatedMethod names were changed in v2 (e.g., 'index' -> 'find'). Check v1 docs if upgrading legacy code.
fix
Rename method calls: index -> find, show -> findById, bulkShow -> findMany, bulkUpdate -> updateMany, bulkDestroy -> deleteMany, destroy -> deleteById.
affects: >=1.0.0 <2.0.0
gotchaUsing `crud.find` conflicts with Express route ordering. If you define `app.get('/:id')` before `app.get('/')`, the `/` route never matches.
fix
Place `app.get('/', crud.find)` BEFORE `app.get('/:id', crud.findById)`.
affects: >=1.0.0
gotchaDefault filter applies to all GET requests including findById and count, potentially causing unexpected empty results.
fix
Ensure defaultFilter uses valid MongoDB query syntax; test with findById using an ID that matches the filter.
affects: >=1.0.0
deprecatedCJS `require` returns a default object. Using `require('mongoose-express-middleware').default` is wrong in v3 (ESM-compatible).
fix
Use `import MongooseExpressMiddleware from 'mongoose-express-middleware'` or `const MongooseExpressMiddleware = require('mongoose-express-middleware')`.
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: MongooseExpressMiddleware is not a constructor
Using `require` and then accessing `.default` as a constructor.
fix
Use `const MongooseExpressMiddleware = require('mongoose-express-middleware');` directly.
MongooseError: `model` is not registered
Model name mismatch or schema not compiled before instantiation.
fix
Ensure the same model name is used and the schema is valid. Register model if needed: `Mongoose.model(modelName, schema)`.
Cannot read property 'find' of undefined
Instance methods (e.g., `crud.find`) are called before instantiation or missing middleware path.
fix
Verify `crud` is an instance of MongooseExpressMiddleware: `new MongooseExpressMiddleware(...)`.
Route.get() requires a callback function but got a [object Object]
Passing the middleware instance instead of its method.
fix
Use `app.get('/', crud.find)` not `app.get('/', crud)`.
MongooseError: Operation `foobars.insertOne()` buffering timed out after 10000ms
Mongoose not connected to MongoDB.
fix
Call `Mongoose.connect('mongodb://localhost:27017/test')` before handling requests.
Upgrade
Version history
3.0.3latest on npm
Audit
Dependencies
mongooserequiredPeer dependency for schema/model definitions. Must be installed separately.
Agent activity
2 hits · last 30 days
node
2
Resources
mongoose-express-middleware — npm install mongoose-express-middleware · libregistry