horse-generator
Version:
horse generator for node framework
1 lines • 2.16 kB
JavaScript
;var _keys=require("babel-runtime/core-js/object/keys"),_keys2=_interopRequireDefault(_keys),_typeof2=require("babel-runtime/helpers/typeof"),_typeof3=_interopRequireDefault(_typeof2),_assign=require("babel-runtime/core-js/object/assign"),_assign2=_interopRequireDefault(_assign);function _interopRequireDefault(t){return t&&t.__esModule?t:{default:t}}function handlePrimitive(t,o){t&&this.populate(o)}function handleObject(t,o){null!==t.maxDepth&&(o.options=o.options||{},o.options.maxDepth=t.maxDepth,delete t.maxDepth),o=(0,_assign2.default)({},o,t),this.populate(o)}function handleFunction(t,o){var e=t.call(this);processOption.call(this,e,o)}function defaultOptions(t,o){var e={path:t,options:{maxDepth:10}};return o.ref&&(e.model=o.ref),e}function processOption(t,o){switch(void 0===t?"undefined":(0,_typeof3.default)(t)){case"function":handleFunction.call(this,t,o);break;case"object":handleObject.call(this,t,o);break;default:handlePrimitive.call(this,t,o)}}function eachPathRecursive(t,o,e){e||(e=[]),t.eachPath(function(t,i){e.push(t),i.schema?eachPathRecursive(i.schema,o,e):o(e.join("."),i),e.pop()})}module.exports=function(t){var o=[];eachPathRecursive(t,function(t,e){var i=void 0;e.options&&e.options.autopopulate?(i=e.options.autopopulate,o.push({options:defaultOptions(t,e.options),autopopulate:i})):e.options&&e.options.type&&e.options.type[0]&&e.options.type[0].autopopulate&&(i=e.options.type[0].autopopulate,o.push({options:defaultOptions(t,e.options.type[0]),autopopulate:i}))}),t.virtuals&&(0,_keys2.default)(t.virtuals).forEach(function(e){t.virtuals[e].options.autopopulate&&o.push({options:defaultOptions(e,t.virtuals[e].options),autopopulate:t.virtuals[e].options.autopopulate})});var e=function(){if(!this._mongooseOptions||!this._mongooseOptions.lean){var t=this.options||{};if(!1!==t.autopopulate){var e=null!==t._depth?t._depth:0;if(!(t.maxDepth>0&&e>=t.maxDepth))for(var i=o.length,p=0;p<i;++p)o[p].options=o[p].options||{},o[p].options.options=o[p].options.options||{},(0,_assign2.default)(o[p].options.options,{_depth:e+1}),processOption.call(this,o[p].autopopulate,o[p].options)}}};t.pre("find",e).pre("findOne",e)};