Skip to content

Instantly share code, notes, and snippets.

@timkuijsten
Created August 22, 2012 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timkuijsten/3426976 to your computer and use it in GitHub Desktop.
Save timkuijsten/3426976 to your computer and use it in GitHub Desktop.
Emit write event on connection.write
diff --git a/node_modules/mongodb/lib/mongodb/connection/connection.js b/node_modules/mongodb/lib/mongodb/connection/connection.js
index 4b65528..b3f4686 100644
--- a/node_modules/mongodb/lib/mongodb/connection/connection.js
+++ b/node_modules/mongodb/lib/mongodb/connection/connection.js
@@ -115,18 +115,35 @@ Connection.prototype.isConnected = function() {
return this.connected && !this.connection.destroyed && this.connection.writable && this.connection.readable;
}
+var emitDocuments = function(command, connection) {
+ var documents = [];
+ if (command.document) {
+ documents = [command.document];
+ } else if (command.documents) {
+ documents = command.documents;
+ }
+ if (documents.length) {
+ connection.emit('write', {
+ collection: command.collectionName,
+ documents: documents
+ });
+ }
+}
+
// Write the data out to the socket
Connection.prototype.write = function(command, callback) {
try {
// If we have a list off commands to be executed on the same socket
if(Array.isArray(command)) {
for(var i = 0; i < command.length; i++) {
+ emitDocuments(command[i], this);
var binaryCommand = command[i].toBinary()
if(binaryCommand.length > this.maxBsonSize) return callback(new Error("Document exceeds maximal allowed bson size of " + this.maxBsonSize + " bytes"));
if(this.logger != null && this.logger.doDebug) this.logger.debug("writing command to mongodb", binaryCommand);
var r = this.writeSteam.write(binaryCommand);
}
} else {
+ emitDocuments(command, this);
var binaryCommand = command.toBinary()
if(binaryCommand.length > this.maxBsonSize) return callback(new Error("Document exceeds maximal allowed bson size of " + this.maxBsonSize + " bytes"));
if(this.logger != null && this.logger.doDebug) this.logger.debug("writing command to mongodb", binaryCommand);
diff --git a/node_modules/mongodb/lib/mongodb/connection/connection_pool.js b/node_modules/mongodb/lib/mongodb/connection/connection_pool.js
index ee62d3f..af178ba 100644
--- a/node_modules/mongodb/lib/mongodb/connection/connection_pool.js
+++ b/node_modules/mongodb/lib/mongodb/connection/connection_pool.js
@@ -147,6 +147,10 @@ var _connect = function(_self) {
_self.emit("message", message);
});
+ connection.on("write", function(message) {
+ _self.emit("write", message);
+ });
+
// Start connection in the next tick
connection.start();
}();
diff --git a/node_modules/mongodb/lib/mongodb/connection/server.js b/node_modules/mongodb/lib/mongodb/connection/server.js
index a79c5fa..4ef8a62 100644
--- a/node_modules/mongodb/lib/mongodb/connection/server.js
+++ b/node_modules/mongodb/lib/mongodb/connection/server.js
@@ -395,6 +395,10 @@ Server.prototype.connect = function(dbInstance, options, callback) {
}
});
+ connectionPool.on("write", function(message) {
+ server.emit('write', message);
+ });
+
// Handle timeout
connectionPool.on("timeout", function(err) {
// If pool connection is already closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment