Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict'
- import Path from 'path'
- import fs from 'fs'
- import _ from '../../lodash-mixins'
- module.exports = ( Mongoose, verbose ) => {
- if( ! Mongoose )
- throw new Error('Need to pass a Mongoose object to the model index to load all models')
- // Get all the files found in the current directory
- const files = fs.readdirSync( __dirname )
- const models = {}
- // Loop through the files in this directory, loading them as Mongoose
- // models, and adding them to the models object to be returned
- _.forEach( files, f => {
- // skip this file
- if ( f === 'index.js' )
- return false
- // Get the model name from the file name
- const modelName = f.match( /(.*)\.js/ )[ 1 ]
- // Convert it to ucfirst
- const model = _( modelName ).chain().toLower().upperFirst().value()
- if( verbose )
- console.log( `# Loading file ${f} as Mongoose model ${model}`)
- // Load it as a model by passing the mongoose object
- models[ model ] = require( `./${modelName}` )( Mongoose, verbose )
- } )
- return models
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement