Merge pull request #18 from strongloop/feature/add-connector-hooks
Add hooks to sql based connectors
This commit is contained in:
commit
1644a00f15
16
lib/sql.js
16
lib/sql.js
|
@ -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);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue