event emitters everywhere, standard 'query' event

This commit is contained in:
Tim Griesser 2014-04-27 19:35:36 -04:00
parent 63b0246db0
commit 2f6a42dd5e
7 changed files with 32 additions and 6 deletions

12
knex.js
View File

@ -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';

View File

@ -1,4 +1,4 @@
// Util.js
// helpers.js
// -------
// Just some common functions needed in multiple places within the library.

View File

@ -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',

View File

@ -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) {

View File

@ -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);
});

View File

@ -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.

View File

@ -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) {