Registry / database / rest-mongoose

rest-mongoose

JSON →
library2.4.3jsnpmunverified

An Express/Mongoose REST API generator that automatically creates CRUD routes, controllers, and authentication from Mongoose schemas. Version 2.4.3 is the latest stable release, with no recent updates since 2021. It provides MongoModel, MongoController, Auth, and MongoRouter classes to quickly scaffold REST endpoints. Unlike generic REST frameworks, it tightly couples with Mongoose models and offers per-action authorization callbacks.

npm install rest-mongoose
INSTALL
IMPORT
SIG · REST-MONGOOSE
R
rest-mongoose
databasejavascriptv2.4.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.

rest-mongoose
import RestMongoose from 'rest-mongoose';
const rest_mongoose = require('rest-mongoose');
Package is ESM-only? The README uses require, but package.json may support ESM. Use named imports for specific classes.
MongoModel
import { MongoModel } from 'rest-mongoose';
const MongoModel = require('rest-mongoose').MongoModel;
Named export from the main package.
MongoRouter
import { MongoRouter } from 'rest-mongoose';
const MongoRouter = require('rest-mongoose').MongoRouter;
Requires app, controller, and auth instances.

Creates an Express server with a mongoose-backed CRUD API for a 'users' model, including authentication and routing callbacks.

import express from 'express'; import mongoose from 'mongoose'; import bodyParser from 'body-parser'; import { MongoModel, MongoController, Auth, MongoRouter, valid_actions } from 'rest-mongoose'; const app = express(); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true, useUnifiedTopology: true }).then(() => console.log('Connected')).catch(err => { console.log(err); process.exit(); }); const model = new MongoModel('users', { name: { type: String, required: true }, email: { type: String, unique: true } }, true); const controller = new MongoController(model, valid_actions); const auth = new Auth(model, async (token, body, action, id) => { return true; }, []); const router = new MongoRouter(app, controller, auth); router.route((action, data) => { console.log(action, data); }); app.get('/', (req, res) => res.json({ message: 'API ready' })); app.listen(8000, () => console.log('Server on 8000'));
Debug
Known issues
deprecatedMongoose 5.x options like useFindAndModify and useCreateIndex are deprecated in Mongoose 6+
fix
Remove deprecated options; they are no longer needed in Mongoose 6+.
affects: >=2.0.0
gotchaThe third parameter to MongoModel constructor (verbose) must be a boolean; passing undefined may cause errors.
fix
Always pass true or false explicitly.
affects: >=2.0.0
gotchaAuth function receives raw (token, body, action, instance_id); forgetting to await async checks returns a Promise instead of boolean.
fix
Ensure the async callback resolves to a boolean.
affects: >=2.0.0
gotchavalid_actions is an array of strings; using invalid action names will throw silently.
fix
Only use actions from valid_actions or export array: ['CREATE','FINDALL','FINDONE','UPDATE','DELETE'].
affects: >=2.0.0
breakingMongoRouter constructor requires (app, controller, auth) - omitting auth or passing null might cause crashes.
fix
Always instantiate Auth even if no authentication logic is needed.
affects: >=2.0.0
Errors
Common errors & fixes
TypeError: Cannot read property 'schema' of undefined
MongoModel was not instantiated correctly or model is undefined.
fix
Ensure model is created before controller: new MongoModel(...).
MongoError: E11000 duplicate key error
Trying to insert a document with a duplicate unique field value.
fix
Check unique constraints on schema fields or handle duplicate errors in callback.
TypeError: router_1.route is not a function
MongoRouter constructor did not receive a valid app instance or controller.
fix
Verify app, controller, and auth are correctly instantiated before calling route.
Upgrade
Version history
2.4.3latest on npm
Audit
Dependencies
mongooserequiredCore dependency for MongoDB schema modeling and queries
expressrequiredRequired for routing middleware
Agent activity
12 hits · last 30 days
node
10
Meta
1
Resources
rest-mongoose — npm install rest-mongoose · libregistry