Registry / database / mongoose-rest-actions

mongoose-rest-actions

JSON →
library0.30.6jsnpmunverified

Mongoose plugin that adds RESTful methods (get, getById, post, put, patch, del) directly to Mongoose models for use with Express.js. Version 0.30.6 is the latest, with a stable API but infrequent releases. It requires Mongoose >=5.9.28 and is designed to work alongside express-mquery for query parsing. Unlike heavier frameworks like LoopBack, it provides a lightweight, plugin-based approach to scaffold CRUD endpoints with minimal boilerplate.

npm install mongoose-rest-actions
INSTALL
IMPORT
SIG · MONGOOSE-REST-ACTI
M
mongoose-rest-actions
databasejavascriptv0.30.6
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.

mongoose-rest-actions
const actions = require('mongoose-rest-actions');
import actions from 'mongoose-rest-actions';
Package only ships CommonJS; ESM import requires default interop but is not officially supported. Use require().
mongoose.Plugin usage
mongoose.plugin(actions);
mongoose.plugin(require('mongoose-rest-actions'));
Correct to assign to variable first then use, though inline also works if the plugin is the default export.
Model methods (get, post, etc.)
User.get(options, callback);
User.get(options).then(); // returns undefined, not a promise
Methods use callbacks, not promises. No promise-based overload exists.

Sets up an Express server with Mongoose REST actions, defining a User model and creating RESTful endpoints for GET, POST, PUT, PATCH, DELETE using model methods.

const express = require('express'); const mquery = require('express-mquery'); const mongoose = require('mongoose'); const actions = require('mongoose-rest-actions'); mongoose.plugin(actions); const UserSchema = new mongoose.Schema({ name: String, email: String }); const User = mongoose.model('User', UserSchema); mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true, useUnifiedTopology: true }) .then(() => console.log('connected')); const app = express(); app.use(express.json()); app.use(mquery({ limit: 10, maxLimit: 50 })); app.get('/users', (req, res, next) => { User.get(req.mquery, (err, users) => { if (err) return next(err); res.json(users); }); }); app.post('/users', (req, res, next) => { User.post(req.body, (err, user) => { if (err) return next(err); res.status(201).json(user); }); }); app.get('/users/:id', (req, res, next) => { User.getById(req.params.id, (err, user) => { if (err) return next(err); if (!user) return res.status(404).end(); res.json(user); }); }); app.put('/users/:id', (req, res, next) => { User.put(req.params.id, req.body, (err, user) => { if (err) return next(err); res.json(user); }); }); app.patch('/users/:id', (req, res, next) => { User.patch(req.params.id, req.body, (err, user) => { if (err) return next(err); res.json(user); }); }); app.delete('/users/:id', (req, res, next) => { User.del(req.params.id, (err, user) => { if (err) return next(err); res.json(user); }); }); app.listen(3000, () => console.log('Server running on port 3000'));
Debug
Known issues
breakingModel methods are callback-based; they do not return promises. Using .then() will cause TypeError: Cannot read property 'then' of undefined.
fix
Use callbacks or promisify with util.promisify() if needed.
affects: >=0.0.0
gotchaThe plugin must be applied before defining models, otherwise model methods will not be available.
fix
Call mongoose.plugin(actions) before mongoose.model().
affects: >=0.0.0
deprecatedThe 'del' method is named differently from the HTTP DELETE convention; 'delete' is a reserved word so 'del' is used, but may confuse developers.
fix
Use User.del() for delete operations.
affects: >=0.0.0
gotchaOptions passed to get() must match the structure expected by express-mquery; otherwise queries may not work correctly.
fix
Ensure request.mquery is provided (via express-mquery middleware) or manually pass an object with filter, sort, limit, skip, etc.
affects: >=0.0.0
breakingMongoose 6.x dropped support for callback-based Model methods, but this plugin still uses callbacks. In Mongoose 6+, callbacks are deprecated and may cause warnings.
fix
Consider using Mongoose 5.x or promisify the plugin methods.
affects: >=6.0.0
Errors
Common errors & fixes
TypeError: User.get is not a function
Plugin not applied or applied after model creation.
fix
Call mongoose.plugin(actions) before defining any models.
TypeError: Cannot read property 'then' of undefined
Trying to use promise methods on callback-based functions.
fix
Use callbacks or wrap with util.promisify().
MongooseError: Operation `users.get()` buffering timed out after 10000ms
Database not connected when method called.
fix
Ensure mongoose.connect() is called and connection is established before making requests.
Upgrade
Version history
0.30.6latest on npm
Audit
Dependencies
mongooserequiredPeer dependency: >=5.9.28 required for plugin functionality
express-mqueryoptionalRecommended to parse HTTP query parameters into Mongoose query criteria
Agent activity
5 hits · last 30 days
node
4
Resources
mongoose-rest-actions — npm install mongoose-rest-actions · libregistry