forked from mongo-express/mongo-express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bson.js
61 lines (51 loc) · 1.5 KB
/
bson.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var mongodb = require('mongodb');
var vm = require('vm');
var json = require('./json');
//Adaptors for BSON types
var DBRef = function(namespace, oid, db) {
//Allow empty/undefined db value
if (db == undefined || db == null) {
db = '';
}
return mongodb.DBRef(namespace, oid, db);
}
var Timestamp = function(high, low) {
//Switch low/high bits to Timestamp constructor
return mongodb.Timestamp(low, high);
}
//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: Timestamp,
DBRef: DBRef,
Dbref: DBRef,
Binary: mongodb.Binary,
BinData: mongodb.Binary,
Code: mongodb.Code,
Symbol: mongodb.Symbol,
MinKey: mongodb.MinKey,
MaxKey: mongodb.MaxKey,
ISODate: Date,
Date: Date
};
};
//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.toBSON = function(string) {
var sandbox = exports.getSandbox();
string = string.replace(/ISODate\(/g, "new ISODate(");
vm.runInNewContext('doc = eval((' + string + '));', sandbox);
return sandbox.doc;
};
//Convert BSON documents to string
exports.toString = function(doc) {
//Use custom json stringify function from json.js
return json.stringify(doc, null, ' ');
};