Registry / database / express-restify-mongoose

express-restify-mongoose

JSON →
library9.0.10jsnpmunverified

Express-restify-mongoose simplifies creating REST endpoints for Mongoose models in Express.js applications. Version 9.0.10 supports Mongoose 6.x-8.x and Node.js >=16. It provides flexible querying (sort, skip, limit, populate, select) via query parameters and uses Express routers. Different from express-restify-mongoose alternatives like loopback or json-server, it closely integrates with Mongoose schemas and offers middleware hooks. The library ships TypeScript types and is actively maintained with monthly releases.

npm install express-restify-mongoose
INSTALL
IMPORT
SIG · EXPRESS-RESTIFY-MO
E
express-restify-mongoose
databasejavascriptv9.0.10
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.

restify
import restify from 'express-restify-mongoose'
const restify = require('express-restify-mongoose')
Default import works for both ESM and CJS with TypeScript. Use esModuleInterop for require.
serve
restify.serve(router, model)
restify.serve(router, Model)
The second argument is a Mongoose model, not a constructor. Do not call it with 'new'.
version
import { version } from 'express-restify-mongoose'
Named export for library version. Available since v9.

Shows basic setup: connect Mongoose, define model, create Express app and router, serve REST endpoints.

import express from 'express'; import mongoose from 'mongoose'; import restify from 'express-restify-mongoose'; await mongoose.connect('mongodb://localhost:27017/test'); const Schema = new mongoose.Schema({ name: String, age: Number }); const Model = mongoose.model('Person', Schema); const app = express(); const router = express.Router(); restify.serve(router, Model); app.use(router); app.listen(3000, () => console.log('Ready')); // Now GET /persons lists all persons, POST /persons creates, etc.
Debug
Known issues
breakingv9.0 drops Node <16 and removes deprecated `restify.defaults` option. Use `restify.serve` with options object.
fix
Update to Node >=16 and migrate from `restify.defaults({...})` to passing options to each `restify.serve` call.
affects: <9.0.0
deprecatedThe `restify.serve` option `prefix` is deprecated in v8+. Use `restify.serve` with `name` parameter or rename the model.
fix
Instead of `{ prefix: '/api' }`, set the model name or use a custom route via `app.use('/api', router)`.
affects: >=8.0.0
gotchaQuery parameters like `?sort=-name` are case-sensitive and require exact field names from Mongoose schema.
fix
Ensure query field names match the schema exactly, e.g., `firstName` not `firstname`.
affects: *
breakingv7.0+ changed from Express 3 to Express 4 middleware syntax. `restify.serve` now expects a Router, not app.
fix
Update code to pass an Express Router instead of the app instance. Example: `restify.serve(express.Router(), Model)`.
affects: <7.0.0
gotchaDefault route is pluralized model name (e.g., `/persons`). If you want different path, specify `name` option.
fix
Pass `{ name: 'people' }` as third argument to serve: `restify.serve(router, Model, { name: 'people' })`.
affects: *
deprecatedThe `onError` callback for `restify.serve` is deprecated. Use the `error` event on the response or Express error middleware.
fix
Add Express error handler after mounting router: `app.use((err, req, res, next) => { ... })`.
affects: >=9.0.0
gotchaUsing `?populate=field` in queries may cause performance issues if field is deeply populated.
fix
Restrict allowed populate fields via `populate` option: `restify.serve(router, Model, { populate: 'allowedField' })`.
affects: *
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'find')
Model is undefined because Mongoose connection not established or model name typo.
fix
Ensure `await mongoose.connect(...)` is called before `restify.serve`, and model name matches exactly.
Cannot find module 'express-restify-mongoose'
Package not installed or incorrect import path.
fix
Run `npm install express-restify-mongoose` and ensure import is correct: `import restify from 'express-restify-mongoose'`.
restify.serve is not a function
Using old CJS require syntax without default import.
fix
Use `const restify = require('express-restify-mongoose').default;` or enable `esModuleInterop` in tsconfig.
Cast to ObjectId failed for value "abc" at path "_id"
Invalid ID format in request URL or query filter.
fix
Ensure IDs are valid MongoDB ObjectId strings. Use `mongoose.Types.ObjectId.isValid(id)` to validate.
Upgrade
Version history
9.0.10latest on npm
Audit
Dependencies
expressoptionalPeer dependency for creating REST routes
mongooseoptionalPeer dependency for model definition
Agent activity
8 hits · last 30 days
node
6
Resources