Created
January 26, 2016 20:37
-
-
Save trezy/56d4f7896f5a995acfdb to your computer and use it in GitHub Desktop.
auto docs for express / mongoose app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _, getRoutes, getSchemas; | |
_ = require( 'lodash' ); | |
getSchemas = function ( mongoose ) { | |
var docs, models; | |
docs = {}; | |
models = mongoose.models; | |
_.each( models, function ( model, modelType ) { | |
var paths; | |
docs[modelType] = {}; | |
//console.log( modelType ) | |
paths = model.schema.paths; | |
_.each( paths, function ( path ) { | |
var doc; | |
doc = docs[modelType]; | |
doc.name = path.path; | |
doc.required = path.options.required || false; | |
doc.type = path.instance; | |
if ( path.defaultValue && !path.options.auto ) { | |
doc.default = path.defaultValue; | |
} | |
}); | |
}); | |
return docs; | |
}; | |
getRoutes = function ( app ) { | |
var routes; | |
routes = []; | |
_.each( app._router.stack, function ( route ) { | |
var methods, path; | |
if ( !_.isUndefined( route.route ) ) { | |
route.route.method = Object.keys( route.route.methods ).toString() | |
routes.push( route ); | |
//methods = Object.keys( route.route.methods ); | |
//path = route.route.path; | |
//routes[path] = { | |
// methods: methods.join( ', ') | |
//}; | |
} else if ( route.handle.stack ) { | |
_.each( route.handle.stack, function ( route ) { | |
if ( !_.isUndefined( route.route ) ) { | |
route.route.method = Object.keys( route.route.methods ).toString() | |
routes.push( route ); | |
}; | |
}); | |
}; | |
}); | |
//console.log( routes ); | |
}; | |
module.exports = function docket ( app, mongoose ) { | |
var routes, schemas; | |
schemas = getSchemas( mongoose ); | |
routes = getRoutes( app ); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment