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'));
Errors
Common errors & fixes
TypeError: Cannot read property 'schema' of undefined
MongoModel was not instantiated correctly or model is undefined.
fixEnsure model is created before controller: new MongoModel(...).
MongoError: E11000 duplicate key error
Trying to insert a document with a duplicate unique field value.
fixCheck 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.
fixVerify app, controller, and auth are correctly instantiated before calling route.
Audit
Dependencies
mongooserequiredCore dependency for MongoDB schema modeling and queries
expressrequiredRequired for routing middleware