Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'
  2.  
  3. import Path from 'path'
  4. import fs from 'fs'
  5. import _ from 'lodash'
  6.  
  7. module.exports = ( Mongoose, verbose ) => {
  8.     if( ! Mongoose )
  9.         throw new Error('Need to pass a Mongoose object to the model index to load all models')
  10.  
  11.     // Get all the files found in the current directory
  12.     const files = fs.readdirSync( __dirname )
  13.  
  14.     const models = {}
  15.  
  16.     // Loop through the files in this directory, loading them as Mongoose
  17.     // models, and adding them to the models object to be returned
  18.     _.forEach( files, f => {
  19.  
  20.         // skip this file
  21.         if ( f === 'index.js' )
  22.             return false
  23.  
  24.         // Get the model name from the file name
  25.         const modelName = f.match( /(.*)\.js/ )[ 1 ]
  26.  
  27.         // Convert it to ucfirst
  28.         const model = _( modelName ).chain().toLower().upperFirst().value()
  29.  
  30.         if( verbose )
  31.             console.log( `# Loading file ${f} as Mongoose model ${model}`)
  32.  
  33.         // Load it as a model by passing the mongoose object
  34.         models[ model ] = require( `./${modelName}` )( Mongoose, verbose )
  35.     } )
  36.  
  37.     return models
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement