Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const mongoose = require('mongoose');
- const getURL = (options) => {
- const url = options.servers.reduce((accumulator, current) => {
- accumulator += current+',';
- return accumulator;
- },`mongodb://${options.user}:${options.pass}@`);
- return `${url.substr(0,url.length-1)}/${options.db}?replicaSet=${options.repl}&authSource=admin`;
- }
- const connect = (config, mediator) => {
- mediator.once('boot.ready', () => {
- const options = {
- native_parser: true,
- poolSize: 5,
- user: config.user,
- pass: config.pass,
- promiseLibrary: global.Promise,
- autoIndex: false, // Don't build indexes
- reconnectTries: 30, // Retry up to 30 times
- reconnectInterval: 500, // Reconnect every 500ms
- bufferMaxEntries: 0
- };
- mongoose.connect(getURL(config), options);
- mongoose.connection.on('error', (err) => {
- mediator.emit('db.error', err);
- });
- mongoose.connection.on('connected', () => {
- mediator.emit('db.ready', mongoose);
- });
- });
- };
- module.exports = Object.assign({},{connect});
Add Comment
Please, Sign In to add comment