Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. /**
  2. * This file gives the equivalent of `rails console`
  3. * Does not consider any arguments
  4. * Does not support any configurations
  5. * Run the file using `node console.js` from parent directory
  6. * Heavily dependant on the file structure and naming conventions that exist now
  7. * Q: Why this when you can do everything using `mongo`?
  8. * A: Since callbacks like mongoose are not supported
  9. */
  10.  
  11. 'use strict';
  12.  
  13. // Set default node environment to development
  14. process.env.NODE_ENV = process.env.NODE_ENV || 'development';
  15. var mongoose = require('mongoose');
  16. var repl = require('repl'),
  17. fs = require('fs'),
  18. path = require('path');
  19. var config = require(path.join(__dirname, 'server', 'config', 'environment'));
  20.  
  21. mongoose.Promise = global.Promise;
  22. // Connect to database
  23. var db = mongoose.connect(config.mongo.uri, config.mongo.options);
  24. mongoose.connection.on('error', function(err) {
  25. console.error('MongoDB connection error: ' + err);
  26. process.exit(-1);
  27. });
  28.  
  29. var context = repl.start({}).context,
  30. ctx = {};
  31.  
  32. ctx.config = config;
  33.  
  34. fs.readdirSync(path.join(__dirname, 'server', 'api')).forEach(function (api) {
  35. var newPath = path.join(__dirname, 'server', 'api', api);
  36. if (fs.statSync(newPath).isDirectory()) {
  37. fs.readdirSync(newPath).filter(function (file) {
  38. return /(.*)\.(js$|coffee$)/.test(file) && file.indexOf('model') > -1 && file.indexOf('spec') === -1
  39. }).forEach(function (modelFile) {
  40. require(path.join(newPath, modelFile.replace(/\.js/, '')));
  41. });
  42. }
  43. });
  44.  
  45. for (var property in ctx) {
  46. context[property] = ctx[property];
  47. }
  48.  
  49. for (var model in mongoose.models) {
  50. context[model] = mongoose.models[model];
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement