/
plus.js
127 lines (115 loc) · 3.84 KB
/
plus.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
__plusPretty = false;
print('* MongoPlus is active (https://github.com/karlseguin/mongoplus)');
$ = function(str) {
if (!ObjectId.isValid(str)) {
throw('"' + str + '" is not a valid ObjectId');
}
return ObjectId(str);
}
$.originals = {
collection: {
find: DBCollection.prototype.find,
findOne: DBCollection.prototype.findOne,
insert: DBCollection.prototype.insert,
remove: DBCollection.prototype.remove,
update: DBCollection.prototype.update,
save: DBCollection.prototype.save
},
query: {
shellPrint: DBQuery.prototype.shellPrint
}
};
/* ObjectId Goodness */
$.implicit = function(b) {
$._implicit = b;
}
ObjectId.prototype.toString = function(){
return '$(' + tojson(this.str) + ')';
}
ObjectId.isValid = function(value) {return /^[0-9a-fA-F]{24}$/.test(value);}
DBCollection.prototype.find = function() {
if (arguments.length > 0) { arguments[0] = $.implicitObjectIdTransform(arguments[0]); }
return $.originals.collection.find.apply(this, arguments);
}
DBCollection.prototype.findOne = function() {
if (arguments.length > 0) { arguments[0] = $.implicitObjectIdTransform(arguments[0]); }
return $.originals.collection.findOne.apply(this, arguments);
}
DBCollection.prototype.insert = function() {
if (arguments.length > 0) { arguments[0] = $.implicitObjectIdTransform(arguments[0]); }
return $.originals.collection.insert.apply(this, arguments);
}
DBCollection.prototype.remove = function() {
if (arguments.length > 0) { arguments[0] = $.implicitObjectIdTransform(arguments[0]); }
return $.originals.collection.remove.apply(this, arguments);
}
DBCollection.prototype.update = function() {
if (arguments.length > 0) { arguments[0] = $.implicitObjectIdTransform(arguments[0]); }
if (arguments.length > 1) { arguments[1] = $.implicitObjectIdTransform(arguments[1]); }
return $.originals.collection.update.apply(this, arguments);
}
DBCollection.prototype.save = function() {
if (arguments.length > 0) { arguments[0] = $.implicitObjectIdTransform(arguments[0]); }
return $.originals.collection.save.apply(this, arguments);
}
$.implicitObjectIdTransform = function(q) {
if (!$._implicit) { return q; }
for (var key in q) {
if (!q.hasOwnProperty(key)) {continue;}
var value = q[key];
if (value == null) {continue;}
var type = typeof(value);
if (type == 'object' && !value.isObjectId) {
q[key] = $.implicitObjectIdTransform(value)
}
else if (type == 'string' && ObjectId.isValid(value)) {
q[key] = new ObjectId(value);
}
}
return q;
}
function pretty(b) { __plusPretty = b; }
/* Find Output */
DBQuery.prototype.shellPrint = function() {
var n = 0;
try {
while (this.hasNext() && n < (this._limit || DBQuery.shellBatchSize)) {
print(__plusPretty || this._prettyShell ? tojson( this.next() ) : tojson( this.next() , "" , true ));
n++;
}
var explain = this.explain()
console.log('Fetched %d of %d record%s in %dms via %s', n, this.count(), n == 1 ? '' : 's', explain.millis, explain.cursor);
if (this.hasNext()){
print('Type "it" for more');
___it___ = this;
}
else { ___it___ = null; }
}
catch (e){ print(e); }
}
/* Misc */
function limit(count) { DBQuery.shellBatchSize = count; }
var console = {
formatters: /%[sdj]/g,
log: function(f) {
if (arguments.length == 0) { print(''); return; }
var i = 1;
var args = arguments;
var str = String(f).replace(console.formatters, function(x) {
switch (x) {
case '%s': return String(args[i++]);
case '%d': return Number(args[i++]);
case '%j': return tojson(args[i++]);
default: return x;
}
});
for (var len = args.length, x = args[i]; i < len; x = args[++i]) {
if (x === null || typeof x !== 'object') {
str += ' ' + x;
} else {
str += ' ' + tojson(x);
}
}
print(str);
}
}