BVB Source Codes

mongo-express Show db.js Source code

Return Download mongo-express: download db.js Source code - Download mongo-express Source code - Type:.js
  1. 'use strict';
  2.  
  3. const _ = require('lodash');
  4. const async = require('async');
  5. const mongodb = require('mongodb');
  6.  
  7. let connect = function (config) {
  8.   // set up database stuff
  9.   let host = config.mongodb.server  || 'localhost';
  10.   let port = config.mongodb.port    || mongodb.Connection.DEFAULT_PORT;
  11.  
  12.   if (config.mongodb.useSSL) {
  13.     console.error('Please update config file to use mongodb.ssl instead of mongodb.useSSL. Copying value for now.');
  14.     config.mongodb.ssl = config.mongodb.useSSL;
  15.   }
  16.  
  17.   let dbOptions = {
  18.     auto_reconnect: config.mongodb.autoReconnect,
  19.     poolSize:       config.mongodb.poolSize,
  20.     ssl:            config.mongodb.ssl,
  21.     sslValidate:    config.mongodb.sslValidate,
  22.     sslCA:          config.mongodb.sslCA,
  23.   };
  24.  
  25.   let db;
  26.  
  27.   if (Array.isArray(host)) {
  28.     host = host.map(function (host) {
  29.       return new mongodb.Server(host, port, dbOptions);
  30.     });
  31.  
  32.     db = new mongodb.Db('local', new mongodb.ReplSet(host), { safe: true, w: 0 });
  33.   } else {
  34.     db = new mongodb.Db('local', new mongodb.Server(host, port, dbOptions), { safe: true });
  35.   }
  36.  
  37.   let collections     = {};
  38.   let connections     = {};
  39.   let databases       = [];
  40.  
  41.   let adminDb = db.admin();
  42.   let mainConn  = db; // main db connection
  43.  
  44.   // update the collections list
  45.   let updateCollections = function (db, dbName, callback) {
  46.     db.listCollections().toArray(function (err, result) {
  47.       let names = [];
  48.  
  49.       for (let r in result) {
  50.         names.push(result[r].name);
  51.       }
  52.  
  53.       collections[dbName] = names.sort();
  54.  
  55.       if (callback) {
  56.         callback(err);
  57.       }
  58.     });
  59.   };
  60.  
  61.   // update database list
  62.   let updateDatabases = function (admin, callback) {
  63.  
  64.     admin.listDatabases(function (err, dbs) {
  65.       databases = [];
  66.       if (err) {
  67.         //TODO: handle error
  68.         console.error(err);
  69.         databases = _.map(config.mongodb.auth, 'database');
  70.       } else {
  71.         for (let i = 0; i < dbs.databases.length; i++) {
  72.           let dbName = dbs.databases[i].name;
  73.  
  74.           if (config.mongodb.whitelist.length !== 0) {
  75.             if (!_.includes(config.mongodb.whitelist, dbName)) {
  76.               continue;
  77.             }
  78.           }
  79.  
  80.           if (config.mongodb.blacklist.length !== 0) {
  81.             if (_.includes(config.mongodb.blacklist, dbName)) {
  82.               continue;
  83.             }
  84.           }
  85.  
  86.           connections[dbName] = mainConn.db(dbName);
  87.           databases.push(dbName);
  88.           updateCollections(connections[dbName], dbName);
  89.         }
  90.       }
  91.  
  92.       //Sort database names
  93.       databases = databases.sort();
  94.  
  95.       if (callback) {
  96.         callback(databases);
  97.       }
  98.     });
  99.   };
  100.  
  101.   // connect to mongodb database
  102.   db.open(function (err, db) {
  103.     if (err) {
  104.       throw err;
  105.     }
  106.  
  107.     if (config.options.console) console.log('Database connected');
  108.  
  109.     mainConn = db;
  110.  
  111.     //Check if admin features are on
  112.     if (config.mongodb.admin === true) {
  113.  
  114.       if (config.mongodb.adminUsername.length === 0) {
  115.         if (config.options.console) console.log('Admin Database connected');
  116.         updateDatabases(adminDb);
  117.       } else {
  118.         //auth details were supplied, authenticate admin account with them
  119.         adminDb.authenticate(config.mongodb.adminUsername, config.mongodb.adminPassword, function (err) {
  120.           if (err) {
  121.             //TODO: handle error
  122.             console.error(err);
  123.           }
  124.  
  125.           if (config.options.console) console.log('Admin Database connected');
  126.           updateDatabases(adminDb);
  127.         });
  128.       }
  129.     } else {
  130.       //Regular user authentication
  131.       if (typeof config.mongodb.auth === 'undefined' || config.mongodb.auth.length === 0) {
  132.         throw new Error('Add auth details to config or turn on admin!');
  133.       }
  134.  
  135.       async.forEachSeries(config.mongodb.auth, function (auth, callback) {
  136.         if (config.options.console) console.log('Connecting to ' + auth.database + '...');
  137.         connections[auth.database] = mainConn.db(auth.database);
  138.         databases.push(auth.database);
  139.  
  140.         if (typeof auth.username !== 'undefined' && auth.username.length !== 0) {
  141.           connections[auth.database].authenticate(auth.username, auth.password, function (err, success) {
  142.             if (err) {
  143.               //TODO: handle error
  144.               console.error(err);
  145.             }
  146.  
  147.             if (!success) {
  148.               console.error('Could not authenticate to database "' + auth.database + '"');
  149.             }
  150.  
  151.             updateCollections(connections[auth.database], auth.database);
  152.             if (config.options.console) console.log('Database ' + auth.database + ' connected');
  153.             callback();
  154.           });
  155.         } else {
  156.           updateCollections(connections[auth.database], auth.database);
  157.           if (config.options.console) console.log('Database ' + auth.database + ' connected');
  158.           callback();
  159.         }
  160.       });
  161.     }
  162.   });
  163.  
  164.   return {
  165.     adminDb:            adminDb,
  166.     collections:        collections,
  167.     connections:        connections,
  168.     databases:          databases,
  169.     mainConn:           mainConn,
  170.     updateCollections:  updateCollections,
  171.     updateDatabases:    updateDatabases,
  172.   };
  173. };
  174.  
  175. module.exports = connect;
  176.  
downloaddb.js Source code - Download mongo-express Source code
Related Source Codes/Software:
XCL-Charts - Android charting libraries (XCL-Charts is a free c... 2017-05-17
scrollMonitor - A simple and fast API to monitor elements as you s... 2017-05-16
XCDYouTubeKit - YouTube video player for iOS, tvOS and macOS 2017-05-16
rails-erd - Generate Entity-Relationship Diagrams for Rails ap... 2017-05-16
np - A better `npm publish` 2017-05-16
kaminari - A Scope & Engine based, clean, powerful, custo... 2017-06-04
python-fire - Python Fire is a library for automatically generat... 2017-06-04
Dapper - Dapper - a simple object mapper for .Net 2017-06-05
pkg - Package your Node.js project into an executable ... 2017-06-05
packer - Packer is a tool for creating identical machine im... 2017-06-05
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top