BVB Source Codes

mongo-express Show database.js Source code

Return Download mongo-express: download database.js Source code - Download mongo-express Source code - Type:.js
  1. 'use strict';
  2.  
  3. var utils = require('../utils');
  4.  
  5. var routes = function () {
  6.   var exp = {};
  7.  
  8.   exp.viewDatabase = function (req, res) {
  9.  
  10.     req.updateCollections(req.db, req.dbName, function (error) {
  11.       if (error) {
  12.         req.session.error = 'Could not refresh collections. ' + JSON.stringify(error);
  13.         console.error(error);
  14.         return res.redirect('back');
  15.       }
  16.  
  17.       req.db.stats(function (error, data) {
  18.         if (error) {
  19.           req.session.error = 'Could not get stats. ' + JSON.stringify(error);
  20.           console.error(error);
  21.           return res.redirect('back');
  22.         }
  23.  
  24.         var ctx = {
  25.           title: 'Viewing Database: ' + req.dbName,
  26.           databases:  req.databases,
  27.           colls:      req.collections[req.dbName],
  28.           grids:      req.gridFSBuckets[req.dbName],
  29.           stats: {
  30.             avgObjSize:         utils.bytesToSize(data.avgObjSize || 0),
  31.             collections:        data.collections,
  32.             dataFileVersion:    (data.dataFileVersion && data.dataFileVersion.major && data.dataFileVersion.minor ?
  33.               data.dataFileVersion.major + '.' + data.dataFileVersion.minor :
  34.               null),
  35.             dataSize:           utils.bytesToSize(data.dataSize),
  36.             extentFreeListNum:  (data.extentFreeList && data.extentFreeList.num ? data.extentFreeList.num : null),
  37.             fileSize:           (typeof data.fileSize !== 'undefined' ? utils.bytesToSize(data.fileSize) : null),
  38.             indexes:            data.indexes,
  39.             indexSize:          utils.bytesToSize(data.indexSize),
  40.             numExtents:         data.numExtents.toString(),
  41.             objects:            data.objects,
  42.             storageSize:        utils.bytesToSize(data.storageSize),
  43.           },
  44.         };
  45.         res.render('database', ctx);
  46.       });
  47.     });
  48.   };
  49.  
  50.   exp.addDatabase = function (req, res) {
  51.  
  52.     var name = req.body.database;
  53.  
  54.     if (name === undefined || name.length === 0) {
  55.       //TODO: handle error
  56.       console.error('That database name is invalid.');
  57.       req.session.error = 'That database name is invalid.';
  58.       return res.redirect('back');
  59.     }
  60.  
  61.     //Database names must begin with a letter or underscore, and can contain only letters, underscores, numbers or dots
  62.     if (!name.match(/^[a-zA-Z_][a-zA-Z0-9._]*$/)) {
  63.       //TODO: handle error
  64.       console.error('That database name is invalid.');
  65.       req.session.error = 'That database name is invalid.';
  66.       return res.redirect('back');
  67.     }
  68.  
  69.     var ndb = req.mainConn.db(name);
  70.  
  71.     ndb.createCollection('delete_me', function (err) {
  72.       if (err) {
  73.         //TODO: handle error
  74.         console.error('Could not create collection.');
  75.         req.session.error = 'Could not create collection.';
  76.         return res.redirect('back');
  77.       }
  78.  
  79.       res.redirect(res.locals.baseHref);
  80.  
  81.       // ndb.dropCollection('delete_me', function(err) {
  82.       //   if (err) {
  83.       //     //TODO: handle error
  84.       //     console.error('Could not delete collection.');
  85.       //     req.session.error = 'Could not delete collection.';
  86.       //     return res.redirect('back');
  87.       //   }
  88.       //   res.redirect(res.locals.baseHref + 'db/' + name);
  89.       // });
  90.     });
  91.  
  92.   };
  93.  
  94.   exp.deleteDatabase = function (req, res) {
  95.     req.db.dropDatabase(function (err) {
  96.       if (err) {
  97.         //TODO: handle error
  98.         console.error('Could not to delete database.');
  99.         req.session.error = 'Failed to delete database.';
  100.         return res.redirect('back');
  101.       }
  102.  
  103.       res.redirect(res.locals.baseHref);
  104.     });
  105.   };
  106.  
  107.   return exp;
  108. };
  109.  
  110. module.exports = routes;
  111.  
downloaddatabase.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