mongo-express
Advanced tools
Comparing version 0.15.0 to 0.16.1
27
app.js
@@ -228,4 +228,4 @@ /** | ||
if (!_.include(databases, id)) { | ||
//TODO: handle error | ||
return next('Error! Database not found!'); | ||
req.session.error = "Database not found!"; | ||
return res.redirect('/'); | ||
} | ||
@@ -250,4 +250,4 @@ | ||
if (!_.include(collections[req.dbName], id)) { | ||
//TODO: handle error | ||
return next('Error!'); | ||
req.session.error = "Collection not found!"; | ||
return res.redirect('/db/' + req.dbName); | ||
} | ||
@@ -259,5 +259,5 @@ | ||
connections[req.dbName].collection(id, function(err, coll) { | ||
if (err) { | ||
//TODO: handle error | ||
return next('Error! Collection not found!'); | ||
if (err || coll == null) { | ||
req.session.error = "Collection not found!"; | ||
return res.redirect('/db/' + req.dbName); | ||
} | ||
@@ -274,8 +274,13 @@ | ||
//Convert id string to mongodb object ID | ||
var id = new mongodb.ObjectID.createFromHexString(id); | ||
try { | ||
var id = new mongodb.ObjectID.createFromHexString(id); | ||
} catch (err) { | ||
req.session.error = "Document not found!"; | ||
return res.redirect('/db/' + req.dbName + '/' + req.collectionName); | ||
} | ||
req.collection.findOne({_id: id}, function(err, doc) { | ||
if (err) { | ||
//TODO: handle error | ||
return next('Error! Document not found!'); | ||
if (err || doc == null) { | ||
req.session.error = "Document not found!"; | ||
return res.redirect('/db/' + req.dbName + '/' + req.collectionName); | ||
} | ||
@@ -282,0 +287,0 @@ |
@@ -0,1 +1,12 @@ | ||
0.16.1 | ||
------ | ||
* Fixed bug: when trying to delete document, collection gets deleted | ||
0.16.0 | ||
------ | ||
* Added support for some BSON data types when viewing docs | ||
* Updated READme with list of supported data types | ||
0.15.0 | ||
@@ -2,0 +13,0 @@ ------ |
@@ -5,3 +5,3 @@ { | ||
"description": "Web-based admin interface for MongoDB", | ||
"version": "0.15.0", | ||
"version": "0.16.1", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -12,8 +12,9 @@ mongo-express | ||
* Supports multiple databases | ||
* Supports regular user authentication or admin authentication | ||
* Connect to multiple databases | ||
* Connect and authenticate to individual databases | ||
* Authenticate as admin to view all databases | ||
* Database blacklist/whitelist | ||
* View/add/rename/delete collections | ||
* View/add/update/delete documents | ||
* Supports BSON data types | ||
* Use BSON data types in documents | ||
@@ -31,5 +32,5 @@ Planned features: | ||
* Can only edit documents which have a document._id property | ||
* Cannot edit document._id property (will be fixed soon) | ||
* Converts all documents from BSON to JSON when viewing (will be fixed soon) | ||
* Documents must have document._id property to be edited | ||
* No GridFS support (might become a planned feature) | ||
* BSON data types are not all working correctly (Do not use mongo-express for editing complex docs for now!) | ||
@@ -77,19 +78,38 @@ | ||
When adding/editing documents, you may want to use BSON data types. | ||
Not all BSON data types are working correctly. This means that mongo-express cannot display or add these data types. | ||
The currently working data types: | ||
* Native Javascript types: strings, numbers, floats, lists, booleans, null, etc. | ||
* ObjectID: can also use ObjectId | ||
* ISODate: **Do not use Date, use ISODate** | ||
Not tested (probably broken): | ||
* Long/NumberLong | ||
* Double/NumberDouble (gets converted to Javascript number type) | ||
* Timestamp | ||
* DBRef | ||
* Binary/BinData | ||
* Code | ||
* Symbol | ||
* MinKey | ||
* MaxKey | ||
Here is an example of how to use them: | ||
{ | ||
_id: ObjectID(), // or ObjectId() | ||
long: Long(3000), // or NumberLong() | ||
double: Double(4.4), // or NumberDouble() | ||
ts: Timestamp() | ||
"_id": ObjectID(), // or ObjectId() | ||
"date": ISODate("2012-05-14T16:20:09.314Z"), | ||
"new_date": ISODate(), | ||
"bool": true, | ||
"string": "hello world!", | ||
"list of numbers": [ | ||
123, | ||
1234566789, | ||
4.4, | ||
-12345.765 | ||
] | ||
} | ||
Writing this in the document editor will automatically convert the document to the BSON format. | ||
See [https://github.com/mongodb/node-mongodb-native](https://github.com/mongodb/node-mongodb-native) for a full list of supported data types. | ||
At the moment, viewing documents do not show the BSON types, they are only used when adding/editing documents. | ||
License | ||
@@ -96,0 +116,0 @@ ------- |
var config = require('../config'); | ||
var utils = require('../utils'); | ||
@@ -40,5 +41,13 @@ //view all entries in a collection | ||
var docs = []; | ||
for(var i in items) { | ||
docs[i] = items[i]; | ||
items[i] = utils.docToString(items[i]); | ||
} | ||
var ctx = { | ||
title: 'Viewing Collection: ' + req.collectionName, | ||
documents: items, | ||
documents: items, //Docs converted to strings | ||
docs: docs, //Original docs | ||
stats: stats, | ||
@@ -45,0 +54,0 @@ editorTheme: config.options.editorTheme, |
var config = require('../config'); | ||
var utils = require('../utils'); | ||
var vm = require('vm'); | ||
@@ -9,3 +8,4 @@ | ||
title: 'Viewing Document: ' + req.document._id, | ||
editorTheme: config.options.editorTheme | ||
editorTheme: config.options.editorTheme, | ||
docString: utils.docToString(req.document) | ||
}; | ||
@@ -25,10 +25,6 @@ | ||
var docJSON; | ||
var sandbox = utils.getSandbox(); | ||
var docBSON; | ||
//JSON.parse doesn't support BSON data types | ||
//Document is evaluated in a vm in order to support BSON data types | ||
//Sandbox contains BSON data type functions from node-mongodb-native | ||
try { | ||
vm.runInNewContext('doc = eval((' + doc + '));', sandbox); | ||
docBSON = utils.stringToBSON(doc); | ||
} catch (err) { | ||
@@ -39,5 +35,4 @@ req.session.error = "That document is not valid!"; | ||
} | ||
var docJSON = sandbox.doc; | ||
req.collection.insert(docJSON, {safe: true}, function(err, result) { | ||
req.collection.insert(docBSON, {safe: true}, function(err, result) { | ||
if (err) { | ||
@@ -63,5 +58,5 @@ req.session.error = "Something went wrong: " + err; | ||
var sandbox = utils.getSandbox(); | ||
var docBSON; | ||
try { | ||
vm.runInNewContext('doc = eval((' + doc + '));', sandbox); | ||
docBSON = utils.stringToBSON(doc); | ||
} catch (err) { | ||
@@ -72,7 +67,6 @@ req.session.error = "That document is not valid!"; | ||
} | ||
var docJSON = sandbox.doc; | ||
docJSON._id = req.document._id; | ||
docBSON._id = req.document._id; | ||
req.collection.update(req.document, docJSON, {safe: true}, function(err, result) { | ||
req.collection.update(req.document, docBSON, {safe: true}, function(err, result) { | ||
if (err) { | ||
@@ -79,0 +73,0 @@ //document was not saved |
45
utils.js
var mongodb = require('mongodb'); | ||
var vm = require('vm'); | ||
@@ -26,2 +27,3 @@ //Given a full collection namescpace, returns the database and collection | ||
DBRef: mongodb.DBRef, | ||
Dbref: mongodb.DBRef, | ||
Binary: mongodb.Binary, | ||
@@ -36,1 +38,44 @@ BinData: mongodb.Binary, | ||
}; | ||
//JSON.parse doesn't support BSON data types | ||
//Document is evaluated in a vm in order to support BSON data types | ||
//Sandbox contains BSON data type functions from node-mongodb-native | ||
exports.stringToBSON = function(string) { | ||
var sandbox = exports.getSandbox(); | ||
string = string.replace(/ISODate\(/g, "new ISODate("); | ||
vm.runInNewContext('doc = eval((' + string + '));', sandbox); | ||
return sandbox.doc; | ||
}; | ||
//Function for converting BSON docs to string representation | ||
exports.docToString = function(doc) { | ||
//Let JSON.stringify do most of the hard work | ||
//Then use replacer function to replace the BSON data | ||
var replacer = function(key, value) { | ||
if (doc[key] instanceof mongodb.ObjectID) { | ||
return '""ObjectId($$replace$$' + value + '$$replace$$)""'; | ||
} else if (doc[key] instanceof mongodb.Long) { | ||
return '""Long($$replace$$' + value + '$$replace$$)""'; | ||
} else if (doc[key] instanceof mongodb.Double) { | ||
return '""Double($$replace$$' + value + '$$replace$$)""'; | ||
} else if (doc[key] instanceof mongodb.Timestamp) { | ||
return '""Timestamp($$replace$$' + value + '$$replace$$)""'; | ||
} else if (doc[key] instanceof Date) { | ||
return '""ISODate($$replace$$' + value + '$$replace$$)""'; | ||
} else { | ||
return value; | ||
} | ||
}; | ||
var newDoc = JSON.stringify(doc, replacer, ' '); | ||
newDoc = newDoc.replace(/"\\"\\"/gi, ""); | ||
newDoc = newDoc.replace(/\\"\\""/gi, ""); | ||
newDoc = newDoc.replace(/\$\$replace\$\$/gi, "\""); | ||
return newDoc; | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
615027
8445
123