Merge pull request #18 from strongloop/feature/add-connector-hooks

Add hooks to sql based connectors
This commit is contained in:
Raymond Feng 2015-05-26 13:31:43 -07:00
commit 1644a00f15
2 changed files with 36 additions and 2 deletions

View File

@ -397,7 +397,21 @@ SQLConnector.prototype.execute = function(sql, params, options, callback) {
self.execute(sql, params, options, callback);
});
}
this.executeSQL(sql, params, options, callback);
var context = {
req: {
sql: sql,
params: params
},
options: options
};
this.notifyObserversAround('execute', context, function(context, done) {
self.executeSQL(sql, params, options, function(err, info, extra) {
if (!err && info != null) {
context.res = info;
}
done(err, info, extra);
});
}, callback);
};
/**

View File

@ -9,13 +9,14 @@ var ds = new juggler.DataSource({
debug: true
});
var connector;
var Customer;
describe('sql connector', function() {
before(function() {
connector = ds.connector;
connector._tables = {};
connector._models = {};
ds.createModel('customer',
Customer = ds.createModel('customer',
{
name: {
id: true,
@ -307,4 +308,23 @@ describe('sql connector', function() {
connector.execute('SELECT * FROM `CUSTOMER`', [], {}, 'xyz');
}).to.throw('callback must be a function');
});
it('should invoke hooks', function(done) {
var events = [];
connector.observe('before execute', function(ctx, next) {
expect(ctx.req.sql).be.a('string');
expect(ctx.req.params).be.a('array');
events.push('before execute');
next();
});
connector.observe('after execute', function(ctx, next) {
expect(ctx.res).be.an('array');
events.push('after execute');
next();
});
Customer.find(function(err, results) {
expect(events).to.eql(['before execute', 'after execute']);
done(err, results);
});
});
});