BVB Source Codes

mongo-express Show document.js Source code

Return Download mongo-express: download document.js Source code - Download mongo-express Source code - Type:.js
  1. 'use strict';
  2.  
  3. var bson = require('../bson');
  4. const buildCollectionURL = require('../utils').buildCollectionURL;
  5.  
  6.  
  7. var routes = function (config) {
  8.   var exp = {};
  9.  
  10.   exp.getProperty = function (req, res) {
  11.     // Get a single property
  12.     // URI like database/collection/document/prop
  13.     res.send(req.prop);
  14.   };
  15.  
  16.   exp.viewDocument = function (req, res) {
  17.     var ctx = {
  18.       title: (config.options.readOnly ? 'Viewing' : 'Editing') + ' Document: ' + req.document._id,
  19.       editorTheme: config.options.editorTheme,
  20.       docLength: bson.toString(req.document).split(/\r\n|\r|\n/).length,
  21.       docString: bson.toString(req.document),
  22.     };
  23.  
  24.     res.render('document', ctx);
  25.   };
  26.  
  27.   exp.checkValid = function (req, res) {
  28.     var doc = req.body.document;
  29.     try {
  30.       bson.toBSON(doc);
  31.     } catch (err) {
  32.       console.error(err);
  33.       return res.send('Invalid');
  34.     }
  35.  
  36.     res.send('Valid');
  37.   };
  38.  
  39.   exp.addDocument = function (req, res) {
  40.     var doc = req.body.document;
  41.  
  42.     if (doc === undefined || doc.length === 0) {
  43.       req.session.error = 'You forgot to enter a document!';
  44.       return res.redirect('back');
  45.     }
  46.  
  47.     var docBSON;
  48.  
  49.     try {
  50.       docBSON = bson.toBSON(doc);
  51.     } catch (err) {
  52.       req.session.error = 'That document is not valid!';
  53.       console.error(err);
  54.       return res.redirect('back');
  55.     }
  56.  
  57.     req.collection.insert(docBSON, { safe: true }, function (err) {
  58.       if (err) {
  59.         req.session.error = 'Something went wrong: ' + err;
  60.         console.error(err);
  61.         return res.redirect('back');
  62.       }
  63.  
  64.       req.session.success = 'Document added!';
  65.       res.redirect(buildCollectionURL(res.locals.baseHref, req.dbName, req.collectionName));
  66.     });
  67.   };
  68.  
  69.   exp.updateDocument = function (req, res) {
  70.     var doc = req.body.document;
  71.  
  72.     if (doc === undefined || doc.length === 0) {
  73.       req.session.error = 'You forgot to enter a document!';
  74.       return res.redirect('back');
  75.     }
  76.  
  77.     var docBSON;
  78.     try {
  79.       docBSON = bson.toBSON(doc);
  80.     } catch (err) {
  81.       req.session.error = 'That document is not valid!';
  82.       console.error(err);
  83.       return res.redirect('back');
  84.     }
  85.  
  86.     docBSON._id = req.document._id;
  87.  
  88.     req.collection.update(req.document, docBSON, { safe: true }, function (err) {
  89.       if (err) {
  90.         //document was not saved
  91.         req.session.error = 'Something went wrong: ' + err;
  92.         console.error(err);
  93.         return res.redirect('back');
  94.       }
  95.  
  96.       req.session.success = 'Document updated!';
  97.       res.redirect(buildCollectionURL(res.locals.baseHref, req.dbName, req.collectionName));
  98.     });
  99.   };
  100.  
  101.   exp.deleteDocument = function (req, res) {
  102.     const skip            = req.query.skip        || '';
  103.     const key             = req.query.key         || '';
  104.     const value           = req.query.value       || '';
  105.     const type            = req.query.type        || '';
  106.     const jsonQuery       = req.query.query       || '';
  107.     const jsonProjection  = req.query.projection  || '';
  108.  
  109.     req.collection.remove(req.document, { safe: true }, function (err) {
  110.       if (err) {
  111.         req.session.error = 'Something went wrong! ' + err;
  112.         console.error(err);
  113.         return res.redirect('back');
  114.       }
  115.  
  116.       req.session.success = 'Document deleted! _id: ' + req.document._id;
  117.       res.redirect(
  118.         buildCollectionURL(res.locals.baseHref, req.dbName, req.collectionName) +
  119.         `?skip=${skip}&key=${key}&value=${value}&type=${type}&query=${jsonQuery}&projection=${jsonProjection}`
  120.       );
  121.     });
  122.   };
  123.  
  124.   return exp;
  125. };
  126.  
  127. module.exports = routes;
  128.  
downloaddocument.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