Rename hook "query" to "access"

The name "query" creates incorrect assumption that hook handlers
may return the result of a query to bypass database access.
That is far from true, since this hook is called also by methods
like `deleteAll` or `updateAll` that don't perform any SELECT query.
This commit is contained in:
Miroslav Bajtoš 2015-02-02 10:31:45 +01:00
parent d3d0570348
commit fcaf19a1d2
2 changed files with 56 additions and 56 deletions

View File

@ -290,7 +290,7 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
return this.create(data, callback); return this.create(data, callback);
} }
Model.notifyObserversOf('query', { Model: Model, query: byIdQuery(Model, id) }, doUpdateOrCreate); Model.notifyObserversOf('access', { Model: Model, query: byIdQuery(Model, id) }, doUpdateOrCreate);
function doUpdateOrCreate(err, ctx) { function doUpdateOrCreate(err, ctx) {
if (err) return callback(err); if (err) return callback(err);
@ -828,7 +828,7 @@ DataAccessObject.find = function find(query, options, cb) {
// using all documents // using all documents
// TODO [fabien] use default scope here? // TODO [fabien] use default scope here?
self.notifyObserversOf('query', { Model: self, query: query }, function(err, ctx) { self.notifyObserversOf('access', { Model: self, query: query }, function(err, ctx) {
if (err) return cb(err); if (err) return cb(err);
self.getDataSource().connector.all(self.modelName, {}, function (err, data) { self.getDataSource().connector.all(self.modelName, {}, function (err, data) {
@ -916,7 +916,7 @@ DataAccessObject.find = function find(query, options, cb) {
if (options.notify === false) { if (options.notify === false) {
self.getDataSource().connector.all(self.modelName, query, allCb); self.getDataSource().connector.all(self.modelName, query, allCb);
} else { } else {
this.notifyObserversOf('query', { Model: this, query: query }, function(err, ctx) { this.notifyObserversOf('access', { Model: this, query: query }, function(err, ctx) {
if (err) return cb(err); if (err) return cb(err);
var query = ctx.query; var query = ctx.query;
self.getDataSource().connector.all(self.modelName, query, allCb); self.getDataSource().connector.all(self.modelName, query, allCb);
@ -991,7 +991,7 @@ DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyA
doDelete(where); doDelete(where);
} else { } else {
query = { where: whereIsEmpty(where) ? {} : where }; query = { where: whereIsEmpty(where) ? {} : where };
Model.notifyObserversOf('query', Model.notifyObserversOf('access',
{ Model: Model, query: query }, { Model: Model, query: query },
function(err, ctx) { function(err, ctx) {
if (err) return cb(err); if (err) return cb(err);
@ -1099,7 +1099,7 @@ DataAccessObject.count = function (where, cb) {
} }
var Model = this; var Model = this;
this.notifyObserversOf('query', { Model: Model, query: { where: where } }, function(err, ctx) { this.notifyObserversOf('access', { Model: Model, query: { where: where } }, function(err, ctx) {
if (err) return cb(err); if (err) return cb(err);
where = ctx.query.where; where = ctx.query.where;
Model.getDataSource().connector.count(Model.modelName, cb, where); Model.getDataSource().connector.count(Model.modelName, cb, where);
@ -1242,7 +1242,7 @@ DataAccessObject.updateAll = function (where, data, cb) {
var Model = this; var Model = this;
Model.notifyObserversOf('query', { Model: Model, query: { where: where } }, function(err, ctx) { Model.notifyObserversOf('access', { Model: Model, query: { where: where } }, function(err, ctx) {
if (err) return cb && cb(err); if (err) return cb && cb(err);
Model.notifyObserversOf( Model.notifyObserversOf(
'before save', 'before save',
@ -1311,7 +1311,7 @@ DataAccessObject.prototype.remove =
var id = getIdValue(this.constructor, this); var id = getIdValue(this.constructor, this);
Model.notifyObserversOf( Model.notifyObserversOf(
'query', 'access',
{ Model: Model, query: byIdQuery(Model, id) }, { Model: Model, query: byIdQuery(Model, id) },
function(err, ctx) { function(err, ctx) {
if (err) return cb(err); if (err) return cb(err);

View File

@ -43,8 +43,8 @@ module.exports = function(dataSource, should) {
}); });
describe('PersistedModel.find', function() { describe('PersistedModel.find', function() {
it('triggers `query` hook', function(done) { it('triggers `access` hook', function(done) {
TestModel.observe('query', pushContextAndNext()); TestModel.observe('access', pushContextAndNext());
TestModel.find({ where: { id: '1' } }, function(err, list) { TestModel.find({ where: { id: '1' } }, function(err, list) {
if (err) return done(err); if (err) return done(err);
@ -55,8 +55,8 @@ module.exports = function(dataSource, should) {
}); });
}); });
it('aborts when `query` hook fails', function(done) { it('aborts when `access` hook fails', function(done) {
TestModel.observe('query', nextWithError(expectedError)); TestModel.observe('access', nextWithError(expectedError));
TestModel.find(function(err, list) { TestModel.find(function(err, list) {
[err].should.eql([expectedError]); [err].should.eql([expectedError]);
@ -64,8 +64,8 @@ module.exports = function(dataSource, should) {
}); });
}); });
it('applies updates from `query` hook', function(done) { it('applies updates from `access` hook', function(done) {
TestModel.observe('query', function(ctx, next) { TestModel.observe('access', function(ctx, next) {
ctx.query = { where: { id: existingInstance.id } }; ctx.query = { where: { id: existingInstance.id } };
next(); next();
}); });
@ -77,8 +77,8 @@ module.exports = function(dataSource, should) {
}); });
}); });
it('triggers `query` hook for geo queries', function(done) { it('triggers `access` hook for geo queries', function(done) {
TestModel.observe('query', pushContextAndNext()); TestModel.observe('access', pushContextAndNext());
TestModel.find({ where: { geo: { near: '10,20' }}}, function(err, list) { TestModel.find({ where: { geo: { near: '10,20' }}}, function(err, list) {
if (err) return done(err); if (err) return done(err);
@ -89,8 +89,8 @@ module.exports = function(dataSource, should) {
}); });
}); });
it('applies updates from `query` hook for geo queries', function(done) { it('applies updates from `access` hook for geo queries', function(done) {
TestModel.observe('query', function(ctx, next) { TestModel.observe('access', function(ctx, next) {
ctx.query = { where: { id: existingInstance.id } }; ctx.query = { where: { id: existingInstance.id } };
next(); next();
}); });
@ -256,8 +256,8 @@ module.exports = function(dataSource, should) {
}); });
describe('PersistedModel.findOrCreate', function() { describe('PersistedModel.findOrCreate', function() {
it('triggers `query` hook', function(done) { it('triggers `access` hook', function(done) {
TestModel.observe('query', pushContextAndNext()); TestModel.observe('access', pushContextAndNext());
TestModel.findOrCreate( TestModel.findOrCreate(
{ where: { name: 'new-record' } }, { where: { name: 'new-record' } },
@ -339,7 +339,7 @@ module.exports = function(dataSource, should) {
function(err, record, created) { function(err, record, created) {
if (err) return done(err); if (err) return done(err);
triggered.should.eql([ triggered.should.eql([
'query', 'access',
'before save', 'before save',
'after save' 'after save'
]); ]);
@ -347,8 +347,8 @@ module.exports = function(dataSource, should) {
}); });
}); });
it('aborts when `query` hook fails', function(done) { it('aborts when `access` hook fails', function(done) {
TestModel.observe('query', nextWithError(expectedError)); TestModel.observe('access', nextWithError(expectedError));
TestModel.findOrCreate( TestModel.findOrCreate(
{ where: { id: 'does-not-exist' } }, { where: { id: 'does-not-exist' } },
@ -403,8 +403,8 @@ module.exports = function(dataSource, should) {
}); });
describe('PersistedModel.count', function(done) { describe('PersistedModel.count', function(done) {
it('triggers `query` hook', function(done) { it('triggers `access` hook', function(done) {
TestModel.observe('query', pushContextAndNext()); TestModel.observe('access', pushContextAndNext());
TestModel.count({ id: existingInstance.id }, function(err, count) { TestModel.count({ id: existingInstance.id }, function(err, count) {
if (err) return done(err); if (err) return done(err);
@ -415,8 +415,8 @@ module.exports = function(dataSource, should) {
}); });
}); });
it('applies updates from `query` hook', function(done) { it('applies updates from `access` hook', function(done) {
TestModel.observe('query', function(ctx, next) { TestModel.observe('access', function(ctx, next) {
ctx.query.where = { id: existingInstance.id }; ctx.query.where = { id: existingInstance.id };
next(); next();
}); });
@ -614,8 +614,8 @@ module.exports = function(dataSource, should) {
}); });
describe('PersistedModel.updateOrCreate', function() { describe('PersistedModel.updateOrCreate', function() {
it('triggers `query` hook on create', function(done) { it('triggers `access` hook on create', function(done) {
TestModel.observe('query', pushContextAndNext()); TestModel.observe('access', pushContextAndNext());
TestModel.updateOrCreate( TestModel.updateOrCreate(
{ id: 'not-found', name: 'not found' }, { id: 'not-found', name: 'not found' },
@ -628,8 +628,8 @@ module.exports = function(dataSource, should) {
}); });
}); });
it('triggers `query` hook on update', function(done) { it('triggers `access` hook on update', function(done) {
TestModel.observe('query', pushContextAndNext()); TestModel.observe('access', pushContextAndNext());
TestModel.updateOrCreate( TestModel.updateOrCreate(
{ id: existingInstance.id, name: 'new name' }, { id: existingInstance.id, name: 'new name' },
@ -642,8 +642,8 @@ module.exports = function(dataSource, should) {
}); });
}); });
it('does not trigger `query` on missing id', function(done) { it('does not trigger `access` on missing id', function(done) {
TestModel.observe('query', pushContextAndNext()); TestModel.observe('access', pushContextAndNext());
TestModel.updateOrCreate( TestModel.updateOrCreate(
{ name: 'new name' }, { name: 'new name' },
@ -654,8 +654,8 @@ module.exports = function(dataSource, should) {
}); });
}); });
it('applies updates from `query` hook when found', function(done) { it('applies updates from `access` hook when found', function(done) {
TestModel.observe('query', function(ctx, next) { TestModel.observe('access', function(ctx, next) {
ctx.query = { where: { id: { neq: existingInstance.id } } }; ctx.query = { where: { id: { neq: existingInstance.id } } };
next(); next();
}); });
@ -675,8 +675,8 @@ module.exports = function(dataSource, should) {
}); });
}); });
it('applies updates from `query` hook when not found', function(done) { it('applies updates from `access` hook when not found', function(done) {
TestModel.observe('query', function(ctx, next) { TestModel.observe('access', function(ctx, next) {
ctx.query = { where: { id: 'not-found' } }; ctx.query = { where: { id: 'not-found' } };
next(); next();
}); });
@ -698,10 +698,10 @@ module.exports = function(dataSource, should) {
}); });
it('triggers hooks only once', function(done) { it('triggers hooks only once', function(done) {
TestModel.observe('query', pushNameAndNext('query')); TestModel.observe('access', pushNameAndNext('access'));
TestModel.observe('before save', pushNameAndNext('before save')); TestModel.observe('before save', pushNameAndNext('before save'));
TestModel.observe('query', function(ctx, next) { TestModel.observe('access', function(ctx, next) {
ctx.query = { where: { id: { neq: existingInstance.id } } }; ctx.query = { where: { id: { neq: existingInstance.id } } };
next(); next();
}); });
@ -710,7 +710,7 @@ module.exports = function(dataSource, should) {
{ id: 'ignored', name: 'new name' }, { id: 'ignored', name: 'new name' },
function(err, instance) { function(err, instance) {
if (err) return done(err); if (err) return done(err);
observersCalled.should.eql(['query', 'before save']); observersCalled.should.eql(['access', 'before save']);
done(); done();
}); });
}); });
@ -855,8 +855,8 @@ module.exports = function(dataSource, should) {
}); });
describe('PersistedModel.deleteAll', function() { describe('PersistedModel.deleteAll', function() {
it('triggers `query` hook with query', function(done) { it('triggers `access` hook with query', function(done) {
TestModel.observe('query', pushContextAndNext()); TestModel.observe('access', pushContextAndNext());
TestModel.deleteAll({ name: existingInstance.name }, function(err) { TestModel.deleteAll({ name: existingInstance.name }, function(err) {
if (err) return done(err); if (err) return done(err);
@ -867,8 +867,8 @@ module.exports = function(dataSource, should) {
}); });
}); });
it('triggers `query` hook without query', function(done) { it('triggers `access` hook without query', function(done) {
TestModel.observe('query', pushContextAndNext()); TestModel.observe('access', pushContextAndNext());
TestModel.deleteAll(function(err) { TestModel.deleteAll(function(err) {
if (err) return done(err); if (err) return done(err);
@ -877,8 +877,8 @@ module.exports = function(dataSource, should) {
}); });
}); });
it('applies updates from `query` hook', function(done) { it('applies updates from `access` hook', function(done) {
TestModel.observe('query', function(ctx, next) { TestModel.observe('access', function(ctx, next) {
ctx.query = { where: { id: { neq: existingInstance.id } } }; ctx.query = { where: { id: { neq: existingInstance.id } } };
next(); next();
}); });
@ -977,8 +977,8 @@ module.exports = function(dataSource, should) {
}); });
describe('PersistedModel.prototype.delete', function() { describe('PersistedModel.prototype.delete', function() {
it('triggers `query` hook', function(done) { it('triggers `access` hook', function(done) {
TestModel.observe('query', pushContextAndNext()); TestModel.observe('access', pushContextAndNext());
existingInstance.delete(function(err) { existingInstance.delete(function(err) {
if (err) return done(err); if (err) return done(err);
@ -989,8 +989,8 @@ module.exports = function(dataSource, should) {
}); });
}); });
it('applies updated from `query` hook', function(done) { it('applies updated from `access` hook', function(done) {
TestModel.observe('query', function(ctx, next) { TestModel.observe('access', function(ctx, next) {
ctx.query = { where: { id: { neq: existingInstance.id } } }; ctx.query = { where: { id: { neq: existingInstance.id } } };
next(); next();
}); });
@ -1080,24 +1080,24 @@ module.exports = function(dataSource, should) {
}); });
it('triggers hooks only once', function(done) { it('triggers hooks only once', function(done) {
TestModel.observe('query', pushNameAndNext('query')); TestModel.observe('access', pushNameAndNext('access'));
TestModel.observe('after delete', pushNameAndNext('after delete')); TestModel.observe('after delete', pushNameAndNext('after delete'));
TestModel.observe('query', function(ctx, next) { TestModel.observe('access', function(ctx, next) {
ctx.query = { where: { id: { neq: existingInstance.id } } }; ctx.query = { where: { id: { neq: existingInstance.id } } };
next(); next();
}); });
existingInstance.delete(function(err) { existingInstance.delete(function(err) {
if (err) return done(err); if (err) return done(err);
observersCalled.should.eql(['query', 'after delete']); observersCalled.should.eql(['access', 'after delete']);
done(); done();
}); });
}); });
}); });
describe('PersistedModel.updateAll', function() { describe('PersistedModel.updateAll', function() {
it('triggers `query` hook', function(done) { it('triggers `access` hook', function(done) {
TestModel.observe('query', pushContextAndNext()); TestModel.observe('access', pushContextAndNext());
TestModel.updateAll( TestModel.updateAll(
{ name: 'searched' }, { name: 'searched' },
@ -1111,8 +1111,8 @@ module.exports = function(dataSource, should) {
}); });
}); });
it('applies updates from `query` hook', function(done) { it('applies updates from `access` hook', function(done) {
TestModel.observe('query', function(ctx, next) { TestModel.observe('access', function(ctx, next) {
ctx.query = { where: { id: { neq: existingInstance.id } } }; ctx.query = { where: { id: { neq: existingInstance.id } } };
next(); next();
}); });