Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.

Commit dbc1c0f

Browse files
authored
chore: enable debugging for all NativeClient methods (#266)
This makes it much easier to trace what database operations actually performs under the hood :)
1 parent 018b207 commit dbc1c0f

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

lib/native-client.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ class NativeClient extends EventEmitter {
177177
* @param {Function} callback - The callback.
178178
*/
179179
command(databaseName, comm, callback) {
180-
debug('running command', { databaseName, comm });
181180
var db = this._database(databaseName);
182181
db.command(comm, (error, result) => {
183182
if (error) {
@@ -497,7 +496,6 @@ class NativeClient extends EventEmitter {
497496
* Disconnect the client.
498497
*/
499498
disconnect(callback) {
500-
debug('disconnect()');
501499
this.client.close(true, callback);
502500
}
503501

@@ -759,7 +757,6 @@ class NativeClient extends EventEmitter {
759757
* @return {Stream} The sample stream.
760758
*/
761759
sample(ns, options) {
762-
debug('getting sample', { ns, options });
763760
var db = this._database(this._databaseName(ns));
764761
return createSampleStream(db, this._collectionName(ns), options);
765762
}
@@ -1174,4 +1171,36 @@ class NativeClient extends EventEmitter {
11741171
}
11751172
}
11761173

1177-
module.exports = NativeClient;
1174+
function addDebugToClass(cls) {
1175+
if (!debug.enabled) {
1176+
return cls;
1177+
}
1178+
1179+
const proto = cls.prototype;
1180+
for (const prop of Object.getOwnPropertyNames(proto)) {
1181+
if (prop.startsWith('_')) {
1182+
continue;
1183+
}
1184+
const descriptor = Object.getOwnPropertyDescriptor(proto, prop);
1185+
if (typeof descriptor.value !== 'function') {
1186+
continue;
1187+
}
1188+
1189+
const orig = descriptor.value;
1190+
descriptor.value = function(...args) {
1191+
debug(`${prop}()`, args);
1192+
if (args.length > 0 & typeof args[args.length - 1] === 'function') {
1193+
const origCallback = args[args.length - 1];
1194+
args[args.length - 1] = function(...callbackArgs) {
1195+
debug(`${prop}()`, args, 'finished ->', callbackArgs);
1196+
return origCallback.call(this, ...callbackArgs);
1197+
};
1198+
}
1199+
return orig.call(this, ...args);
1200+
};
1201+
Object.defineProperty(proto, prop, descriptor);
1202+
}
1203+
return cls;
1204+
}
1205+
1206+
module.exports = addDebugToClass(NativeClient);

0 commit comments

Comments
 (0)