Create API Routes for a Mongoose Model
This module allows the easy creation of api routes to perform basic CRUD operations with advanced query functionality. To use, simply inject the mongoose model into this module and get a configured Express router as a return value. Mount this router at a path unique to the mongoose model.
An example:
var express = require('express');
var mongoose = require('mongoose');
var apiRoutes = require('mongoose-api-routes');
var app = express();
mongoose.connect('your connection string here');
// Instantiate mongoose models
var User = require('./user-model')(mongoose);
var Post = require('./post-model')(mongoose);
// Mount api routes
app.use('/users', apiRoutes(User));
app.use('/posts', apiRoutes(Post));
The Routes
The following routes are provided:
Get all documents
GET '/'
Create a new document
POST '/' with new document in req.body
Update an existing document
PUT '/:id' with updated document in req.body
Delete an existing document
DELETE '/:id'
id
Get an existing document by PUT 'find/:id' with query options in req.body
Query options have the following form:
{
lean: <Boolean> // defaults to true,
select: <Object, String> // same format as mongoose select query method
populate: <Object, String> // same format as mongoose populate query method 1st arg
pick: String[] // array of property names to pick after query returns (useful for selecting virtual paths)MM
}