/
db.js
61 lines (54 loc) · 1.55 KB
/
db.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
const { toTinsel } = require('./utils.js');
module.exports = function (baseClass) {
class TinselDb extends baseClass {
get __emulated() {
return true;
}
collection(name, options, callback) {
if (arguments.length === 1) {
return super.collection(name)[toTinsel]();
}
if (arguments.length === 2) {
if ((typeof options) !== 'function') {
return super.collection(name, options)[toTinsel]();
} else {
callback = options;
return super.collection(name, {}, function(err, collection) {
if (err) {
return callback(err);
}
const tinselled = collection[toTinsel]();
return callback(null, tinselled);
});
}
}
return super.collection(name, options, function(err, collection) {
if (err) {
return callback(err);
}
return callback(null, collection[toTinsel]());
});
};
// Reintroduce the "db" method of db objects, for talking to a second
// database via the same connection
db(name) {
return this.client.db(name);
};
// Reintroduce the "close" method of db objects, yes it closes
// the entire client, did that before too
close(force, callback) {
return this.client.close(force, callback);
};
}
Object.defineProperty(
baseClass.prototype,
toTinsel,
{
enumerable: false,
value: function () {
return Object.setPrototypeOf(this, TinselDb.prototype);
}
}
);
return TinselDb;
};