emme
v0.5.0
Published
Automatically expose an Express api for database models.
Downloads
9
Readme
emme
Quickly and easily expose your Mongoose models to the world with an easy to customize API built on top of Express.
var express = require('express'),
mongoose = require('mongoose'),
bodyParser = require('body-parser'),
em = require('em-expose');
var UserModel = require('./models/User.js'),
CommentModel = require('./models/Comment.js');
em
.expose(UserModel, {
path: '/account', // would default to /user
protected: ['username', 'createdAt', 'updatedAt'], // not editable
private: ['password', 'auth._google_id', 'auth._facebook._id', '__v'] // never visible
})
var app = express();
app.use(bodyParser());
// expose user model
app.use('/user', em(UserModel, {
// these fields wont be viewable
private: {
'auth.password': true, // flatten paths
'createdAt': true,
'metadata': {
'awards': true // or keep them expanded
},
'comments._id': true // filter items in collections
},
// these fields wont be editable
protected: {
'username': true,
},
// custom CRUD
methods: {
remove: false // in this case don't let them be deleted
}
}));
// The following routes are now exposed
// /user GET(browse), POST(create)
// /user/:id GET(get), POST(save)
Documentation
Exposer
expose(Model, options) require(em-expose)(Model, options)
Builds an Express router that exposes the Mongoose model.
Arguments
Model
- a Mongoose modeloptions
private
- Object of paths that are not shown or editable.protected
- Object of paths that are shown but not editable.methods
- Object to override individual method settingsbrowse
,create
,retrieve
,update
, and/ordelete
private
- Override the object of paths that are not shown or editable.protected
- Override the object of paths that are shown but not editable. (only matters oncreate
andupdate
)preModel
-function(req, res, nex)
- Express style function, called before model is requestedpostModel
-function(req, res, next)
- Express style function, called after/if model has been found (access it withreq.doc
)preSend
-function(req, res, next)
- Express style function, called just before sending the model