Guest User

Untitled

a guest
May 16th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. 'use strict';
  2. const mongoose = require('mongoose');
  3. const getURL = (options) => {
  4. const url = options.servers.reduce((accumulator, current) => {
  5. accumulator += current+',';
  6. return accumulator;
  7. },`mongodb://${options.user}:${options.pass}@`);
  8. return `${url.substr(0,url.length-1)}/${options.db}?replicaSet=${options.repl}&authSource=admin`;
  9. }
  10. const connect = (config, mediator) => {
  11. mediator.once('boot.ready', () => {
  12. const options = {
  13. native_parser: true,
  14. poolSize: 5,
  15. user: config.user,
  16. pass: config.pass,
  17. promiseLibrary: global.Promise,
  18. autoIndex: false, // Don't build indexes
  19. reconnectTries: 30, // Retry up to 30 times
  20. reconnectInterval: 500, // Reconnect every 500ms
  21. bufferMaxEntries: 0
  22. };
  23. mongoose.connect(getURL(config), options);
  24. mongoose.connection.on('error', (err) => {
  25. mediator.emit('db.error', err);
  26. });
  27. mongoose.connection.on('connected', () => {
  28. mediator.emit('db.ready', mongoose);
  29. });
  30. });
  31. };
  32. module.exports = Object.assign({},{connect});
Add Comment
Please, Sign In to add comment