diff --git a/knex.js b/knex.js index df8798a4c..d9f9bdb1a 100644 --- a/knex.js +++ b/knex.js @@ -42,6 +42,7 @@ var QueryInterface = require('./lib/query/methods'); // Create a new "knex" instance with the appropriate configured client. Knex.initialize = function(config) { var Dialect, client; + var EventEmitter = require('events').EventEmitter; // The object we're potentially using to kick off an // initial chain. It is assumed that `knex` isn't a @@ -49,9 +50,20 @@ Knex.initialize = function(config) { // in case it's called with `new`. function knex(tableName) { var qb = new client.QueryBuilder; + + // Passthrough all "query" events to the knex object. + qb.on('query', function(data) { + knex.emit('query', data); + }); return tableName ? qb.table(tableName) : qb; } + // Hook up the "knex" object as an EventEmitter. + var ee = new EventEmitter(); + for (var key in ee) { + knex[key] = ee[key]; + } + // The `__knex__` is used if you need to duck-type check whether this // is a knex builder, without a full on `instanceof` check. knex.VERSION = knex.__knex__ = '0.6.0'; diff --git a/lib/helpers.js b/lib/helpers.js index 733eb71a0..a3bacd658 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -1,4 +1,4 @@ -// Util.js +// helpers.js // ------- // Just some common functions needed in multiple places within the library. diff --git a/lib/query/builder.js b/lib/query/builder.js index 41a477147..aa9a8f79f 100644 --- a/lib/query/builder.js +++ b/lib/query/builder.js @@ -1,9 +1,11 @@ // Builder // ------- -var _ = require('lodash'); +var _ = require('lodash'); +var inherits = require('inherits'); +var EventEmitter = require('events').EventEmitter; -var Raw = require('../raw'); -var helpers = require('../helpers'); +var Raw = require('../raw'); +var helpers = require('../helpers'); var JoinClause = require('./joinclause'); @@ -18,6 +20,7 @@ function QueryBuilder() { this._joinFlag = 'inner'; this._boolFlag = 'and'; } +inherits(QueryBuilder, EventEmitter); // All operators used in the `where` clause generation. var operators = ['=', '<', '>', '<=', '>=', '<>', '!=', 'like', 'not like', diff --git a/lib/raw.js b/lib/raw.js index 3d3095e3e..525c781c4 100644 --- a/lib/raw.js +++ b/lib/raw.js @@ -2,6 +2,8 @@ // ------- var SqlString = require('./sqlstring'); var _ = require('lodash'); +var inherits = require('inherits'); +var EventEmitter = require('events').EventEmitter; function Raw(sql, bindings) { if (sql.toSQL) { @@ -13,6 +15,7 @@ function Raw(sql, bindings) { this._debug = void 0; this._transacting = void 0; } +inherits(Raw, EventEmitter); // Wraps the current sql with `before` and `after`. Raw.prototype.wrap = function(before, after) { diff --git a/lib/runner.js b/lib/runner.js index f0ec37061..fc7bfe42a 100644 --- a/lib/runner.js +++ b/lib/runner.js @@ -69,6 +69,7 @@ Runner.prototype.stream = function(options, handler) { // to run in sequence, and on the same connection, especially helpful when schema building // and dealing with foreign key constraints, etc. Runner.prototype.query = Promise.method(function(obj) { + this.builder.emit('query', obj); return this._query(obj).bind(this).then(this.processResponse); }); diff --git a/lib/schema/builder.js b/lib/schema/builder.js index 61898f583..e647f6c03 100644 --- a/lib/schema/builder.js +++ b/lib/schema/builder.js @@ -1,14 +1,18 @@ var _ = require('lodash'); +var inherits = require('inherits'); +var EventEmitter = require('events').EventEmitter; + var SqlString = require('../sqlstring'); // Constructor for the builder instance, typically called from // `knex.builder`, accepting the current `knex` instance, // and pulling out the `client` and `grammar` from the current // knex instance. -var SchemaBuilder = function() { +function SchemaBuilder() { this._sequence = []; this._errors = []; -}; +} +inherits(SchemaBuilder, EventEmitter); // Each of the schema builder methods just add to the // "_sequence" array for consistency. diff --git a/lib/transaction.js b/lib/transaction.js index 4abbfeea3..532387fa8 100644 --- a/lib/transaction.js +++ b/lib/transaction.js @@ -1,6 +1,8 @@ // Transaction // ------- var Promise = require('./promise'); +var inherits = require('inherits'); +var EventEmitter = require('events').EventEmitter; // Creates a new wrapper object for constructing a transaction. // Called by the `knex.transaction`, which sets the correct client @@ -9,6 +11,7 @@ var Promise = require('./promise'); function Transaction(container) { this.container = container; } +inherits(Transaction, EventEmitter); // Build the object passed around inside the transaction container. Transaction.prototype.containerObject = function(runner) {