Comments and a few minor changes to start event

This commit is contained in:
Tim Griesser 2014-06-22 15:11:01 -04:00
parent 712786bf95
commit 87e4cc9435
2 changed files with 25 additions and 2 deletions

View File

@ -120,7 +120,10 @@ Knex.initialize = function(config) {
Dialect = Clients[clientName]();
client = new Dialect(config);
// Passthrough all "query" events to the knex object.
// Passthrough all "start" and "query" events to the knex object.
client.on('start', function(obj) {
knex.emit('start', obj);
});
client.on('query', function(obj) {
knex.emit('query', obj);
});

View File

@ -28,14 +28,34 @@ Runner.prototype.run = Promise.method(function() {
.then(this.ensureConnection)
.then(function(connection) {
this.connection = connection;
// Emit a "start" event on both the builder and the client,
// allowing us to listen in on any events. We fire on the "client"
// before building the SQL, and on the builder after building the SQL
// in case we want to determine at how long it actually
// took to build the query.
this.client.emit('start', this.builder);
this.builder.emit('start');
var sql = this.builder.toSQL();
this.builder.emit('start', this.builder);
if (_.isArray(sql)) {
return this.queryArray(sql);
}
return this.query(sql);
})
// If there are any "error" listeners, we fire an error event
// and then re-throw the error to be eventually handled by
// the promise chain. Useful if you're wrapping in a custom `Promise`.
.catch(function(err) {
if (this.builder._events.error) {
this.builder.emit('error', err);
}
throw err;
})
// Fire a single "end" event on the builder when
// all queries have successfully completed.
.tap(function() {
this.builder.emit('end');
})