Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const serviceLocator = require('../lib/service_locator');
  4. const logger = serviceLocator.get('logger');
  5.  
  6. class Database {
  7. constructor(port, host, name) {
  8. this.mongoose = serviceLocator.get('mongoose');
  9. this._connect(port, host, name);
  10. }
  11.  
  12. _connect(port, host, name) {
  13. this.mongoose.Promise = global.Promise;
  14. this.mongoose.connect(`mongodb://${host}:${port}/${name}`);
  15. const {connection} = this.mongoose;
  16. connection.on('connected', () =>
  17. logger.info('Database Connection was Successful')
  18. );
  19. connection.on('error', (err) =>
  20. logger.info('Database Connection Failed' + err)
  21. );
  22. connection.on('disconnected', () =>
  23. logger.info('Database Connection Disconnected')
  24. );
  25. process.on('SIGINT', () => {
  26. connection.close();
  27. logger.info(
  28. 'Database Connection closed due to NodeJs process termination'
  29. );
  30. process.exit(0);
  31. });
  32.  
  33. // initialize Model
  34. require('../models/Users'); // <--------------------- this line
  35. }
  36. }
  37.  
  38. module.exports = Database;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement