Skip to content

Commit

Permalink
Merge branch 'release/v0.15.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
andzdroid committed May 13, 2012
2 parents 1c5d6e5 + 90b3dc4 commit 89458ee
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 10 deletions.
7 changes: 6 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
0.15.0
------

* Added support for BSON data types when adding/editing docs

0.14.1
------

* Forgot to updated HISTORY file
* Forgot to update HISTORY file

0.14.0
------
Expand Down
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Current features:
* Database blacklist/whitelist
* View/add/rename/delete collections
* View/add/update/delete documents
* Supports BSON data types

Planned features:

Expand All @@ -26,8 +27,9 @@ Planned features:
Limitations
-----------

* Requires documents to have a document._id property
* Cannot edit document._id property
* 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)


Screenshots
Expand Down Expand Up @@ -67,6 +69,26 @@ Fill in your MongoDB connection details, and any other options you want to chang
Visit `http://localhost:8081` or whatever URL/port you entered into your config.


BSON Data Types
---------------

When adding/editing documents, you may want to use BSON data types.

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()
}

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
-------
MIT License
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Chun-hao Hu <hu.chunhao@gmail.com> (http://blog.huchunhao.com)",
"name": "mongo-express",
"description": "Web-based admin interface for MongoDB",
"version": "0.14.1",
"version": "0.15.0",
"repository": {
"type": "git",
"url": "git://github.com/andzdroid/mongo-express.git"
Expand Down
19 changes: 14 additions & 5 deletions routes/document.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var config = require('../config');
var mongodb = require('mongodb');
var utils = require('../utils');
var vm = require('vm');


exports.viewDocument = function(req, res, next) {
var ctx = {
Expand All @@ -20,13 +22,19 @@ exports.addDocument = function(req, res, next) {
}

var docJSON;
var sandbox = utils.getSandbox();

//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 {
docJSON = JSON.parse(doc);
vm.runInNewContext('doc = eval((' + doc + '));', sandbox);
} catch (err) {
req.session.error = "That document is not valid!";
console.error(err)
console.error(err);
return res.redirect('back');
}
var docJSON = sandbox.doc;

req.collection.insert(docJSON, {safe: true}, function(err, result) {
if (err) {
Expand All @@ -49,14 +57,15 @@ exports.updateDocument = function(req, res, next) {
return res.redirect('back');
}

var docJSON;
var sandbox = utils.getSandbox();
try {
docJSON = JSON.parse(doc);
vm.runInNewContext('doc = eval((' + doc + '));', sandbox);
} catch (err) {
req.session.error = "That document is not valid!";
console.error(err);
return res.redirect('back');
}
var docJSON = sandbox.doc;

docJSON._id = req.document._id;

Expand Down
23 changes: 23 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var mongodb = require('mongodb');

//Given a full collection namescpace, returns the database and collection
exports.parseCollectionName = function parseCollectionName(full_name) {
var coll_parts = full_name.split('.');
Expand All @@ -9,3 +11,24 @@ exports.parseCollectionName = function parseCollectionName(full_name) {
var database = coll_parts.splice(0,1);
return { name: coll_parts.join('.'), database: database.toString() };
};

//Create sandbox with BSON data types
exports.getSandbox = function() {
return {
Long: mongodb.Long,
NumberLong: mongodb.Long,
Double: mongodb.Double,
NumberDouble: mongodb.Double,
ObjectId: mongodb.ObjectID,
ObjectID: mongodb.ObjectID,
Timestamp: mongodb.Timestamp,
DBRef: mongodb.DBRef,
Binary: mongodb.Binary,
BinData: mongodb.Binary,
Code: mongodb.Code,
Symbol: mongodb.Symbol,
MinKey: mongodb.MinKey,
MaxKey: mongodb.MaxKey,
ISODate: Date
};
};
17 changes: 16 additions & 1 deletion views/document.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

<form method="POST" action="db/{{ dbName }}/{{ collectionName }}/{{ document._id }}">
<input type="hidden" name="_method" value="put">
<button type="submit" class="btn btn-success btn-large">
<button type="submit" class="btn btn-success btn-large"{# onclick="return checkJSON()"#}>
<i class="icon-pencil icon-white"></i>
Save
</button>
Expand All @@ -63,6 +63,21 @@
matchBrackets: true,
theme: "{{ editorTheme }}"
});

{# This can check for valid JSON on the client side
# Not used at the moment, since it doesn't allow BSON types
function checkJSON() {
var d = doc.getValue();
try {
docJSON = JSON.parse(d);
return true;
} catch (err) {
alert('This is not a valid document!');
return false;
}
}
#}
</script>

{% endblock %}

0 comments on commit 89458ee

Please sign in to comment.