Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. 'use strict';
  2. const mongoose = require('mongoose');
  3. const config = require('./database');
  4.  
  5. //
  6. var options = {
  7. poolSize: 5,
  8. keepAlive: 300000,
  9. connectTimeoutMS: 30000,
  10. promiseLibrary: global.Promise,
  11. keepAlive: 1
  12. };
  13.  
  14. //
  15. mongoose.Promise = global.Promise;
  16.  
  17. // Connect To Database
  18. mongoose.connect(config.database, options);
  19. var conn = mongoose.connection;
  20.  
  21. conn.on('error', console.error.bind(console, 'connection error:'));
  22.  
  23. conn.once('open', function () {console.log('Connected to database ' + config.database)});
  24.  
  25. conn.on('disconnected', function () {
  26. console.log('Mongoose default connection to DB :' + config.database + ' disconnected');
  27. });
  28.  
  29. var gracefulExit = function() {
  30. conn.close(function () {
  31. console.log('Mongoose default connection with DB :' + config.database + ' is disconnected through app termination');
  32. process.exit(0);
  33. });
  34. }
  35.  
  36. // If the Node process ends, close the Mongoose connection
  37. process.on('SIGINT', gracefulExit).on('SIGTERM', gracefulExit);
  38.  
  39. try {
  40. mongoose.connect(config.database, options);
  41. console.log("Trying to connect to DB " + config.database);
  42. } catch (err) {
  43. console.log("Sever initialization failed " , err.message);
  44. }
  45.  
  46. // Exporting
  47. module.exports = mongoose;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement