Merge pull request #485 from fabien/feature/op-hooks-state

Implement #484 : hookState
This commit is contained in:
Raymond Feng 2015-03-05 08:38:38 -08:00
commit 6b04568707
2 changed files with 112 additions and 53 deletions

View File

@ -216,6 +216,7 @@ DataAccessObject.create = function (data, options, cb) {
var enforced = {};
var obj;
var idValue = getIdValue(this, data);
var hookState = {};
// if we come from save
if (data instanceof Model && !idValue) {
@ -230,7 +231,8 @@ DataAccessObject.create = function (data, options, cb) {
Model = this.lookupModel(data); // data-specific
if (Model !== obj.constructor) obj = new Model(data);
Model.notifyObserversOf('before save', { Model: Model, instance: obj }, function(err) {
var context = { Model: Model, instance: obj, hookState: hookState };
Model.notifyObserversOf('before save', context, function(err) {
if (err) return cb(err);
data = obj.toObject(true);
@ -268,7 +270,8 @@ DataAccessObject.create = function (data, options, cb) {
if (err) {
return cb(err, obj);
}
Model.notifyObserversOf('after save', { Model: Model, instance: obj }, function(err) {
var context = { Model: Model, instance: obj, hookState: hookState };
Model.notifyObserversOf('after save', context, function(err) {
cb(err, obj);
if(!err) Model.emit('changed', obj);
});
@ -345,20 +348,27 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
var self = this;
var Model = this;
var hookState = {};
var id = getIdValue(this, data);
if (!id) {
return this.create(data, options, cb);
}
Model.notifyObserversOf('access', { Model: Model, query: byIdQuery(Model, id) }, doUpdateOrCreate);
var context = { Model: Model, query: byIdQuery(Model, id), hookState: hookState };
Model.notifyObserversOf('access', context, doUpdateOrCreate);
function doUpdateOrCreate(err, ctx) {
if (err) return cb(err);
var isOriginalQuery = isWhereByGivenId(Model, ctx.query.where, id)
if (Model.getDataSource().connector.updateOrCreate && isOriginalQuery) {
var context = { Model: Model, where: ctx.query.where, data: data };
var context = {
Model: Model,
where: ctx.query.where,
data: data,
hookState: hookState
};
Model.notifyObserversOf('before save', context, function(err, ctx) {
if (err) return cb(err);
@ -394,7 +404,8 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
Model.emit('changed', inst);
}
} else {
Model.notifyObserversOf('after save', { Model: Model, instance: obj }, function(err) {
var context = { Model: Model, instance: obj, hookState: hookState };
Model.notifyObserversOf('after save', context, function(err) {
cb(err, obj);
if(!err) {
Model.emit('changed', inst);
@ -478,6 +489,7 @@ DataAccessObject.findOrCreate = function findOrCreate(query, data, options, cb)
var Model = this;
var self = this;
var hookState = {};
function _findOrCreate(query, data) {
var modelName = self.modelName;
@ -493,8 +505,8 @@ DataAccessObject.findOrCreate = function findOrCreate(query, data, options, cb)
}
if (created) {
Model.notifyObserversOf('after save', { Model: Model, instance: obj },
function(err) {
var context = { Model: Model, instance: obj, hookState: hookState };
Model.notifyObserversOf('after save', context, function(err) {
if (cb.promise) {
cb(err, [obj, created]);
} else {
@ -526,8 +538,8 @@ DataAccessObject.findOrCreate = function findOrCreate(query, data, options, cb)
this.applyScope(query);
Model.notifyObserversOf('access', { Model: Model, query: query },
function (err, ctx) {
var context = { Model: Model, query: query, hookState: hookState };
Model.notifyObserversOf('access', context, function (err, ctx) {
if (err) return cb(err);
var query = ctx.query;
@ -539,8 +551,8 @@ DataAccessObject.findOrCreate = function findOrCreate(query, data, options, cb)
Model.applyProperties(enforced, obj);
obj.setAttributes(enforced);
Model.notifyObserversOf('before save', { Model: Model, instance: obj },
function(err, ctx) {
var context = { Model: Model, instance: obj, hookState: hookState };
Model.notifyObserversOf('before save', context, function(err, ctx) {
if (err) return cb(err);
var obj = ctx.instance;
@ -1081,6 +1093,7 @@ DataAccessObject.find = function find(query, options, cb) {
assert(typeof cb === 'function', 'The cb argument must be a function');
var self = this;
var hookState = {};
try {
this._normalize(query);
@ -1105,7 +1118,8 @@ DataAccessObject.find = function find(query, options, cb) {
// using all documents
// TODO [fabien] use default scope here?
self.notifyObserversOf('access', { Model: self, query: query }, function(err, ctx) {
var context = { Model: self, query: query, hookState: hookState };
self.notifyObserversOf('access', context, function(err, ctx) {
if (err) return cb(err);
self.getDataSource().connector.all(self.modelName, {}, function (err, data) {
@ -1199,7 +1213,8 @@ DataAccessObject.find = function find(query, options, cb) {
if (options.notify === false) {
self.getDataSource().connector.all(self.modelName, query, allCb);
} else {
this.notifyObserversOf('access', { Model: this, query: query }, function(err, ctx) {
var context = { Model: this, query: query, hookState: hookState };
this.notifyObserversOf('access', context, function(err, ctx) {
if (err) return cb(err);
var query = ctx.query;
self.getDataSource().connector.all(self.modelName, query, allCb);
@ -1296,16 +1311,20 @@ DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyA
this.applyScope(query);
where = query.where;
var context = { Model: Model, where: whereIsEmpty(where) ? {} : where };
var hookState = {};
var context = {
Model: Model, where: whereIsEmpty(where) ? {} : where, hookState: hookState
};
if (options.notify === false) {
doDelete(where);
} else {
query = { where: whereIsEmpty(where) ? {} : where };
Model.notifyObserversOf('access',
{ Model: Model, query: query },
function(err, ctx) {
var context = { Model: Model, query: query, hookState: hookState };
Model.notifyObserversOf('access', context, function(err, ctx) {
if (err) return cb(err);
var context = { Model: Model, where: ctx.query.where };
var context = { Model: Model, where: ctx.query.where, hookState: hookState };
Model.notifyObserversOf('before delete', context, function(err, ctx) {
if (err) return cb(err);
doDelete(ctx.where);
@ -1338,7 +1357,8 @@ DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyA
return cb(err, data);
}
Model.notifyObserversOf('after delete', { Model: Model, where: where }, function(err) {
var context = { Model: Model, where: where, hookState: hookState };
Model.notifyObserversOf('after delete', context, function(err) {
cb(err, data);
if (!err)
Model.emit('deletedAll', whereIsEmpty(where) ? undefined : where);
@ -1459,7 +1479,10 @@ DataAccessObject.count = function (where, options, cb) {
}
var Model = this;
this.notifyObserversOf('access', { Model: Model, query: { where: where } }, function(err, ctx) {
var hookState = {};
var context = { Model: Model, query: { where: where }, hookState: hookState };
this.notifyObserversOf('access', context, function(err, ctx) {
if (err) return cb(err);
where = ctx.query.where;
Model.getDataSource().connector.count(Model.modelName, cb, where);
@ -1506,8 +1529,10 @@ DataAccessObject.prototype.save = function (options, cb) {
var inst = this;
var modelName = Model.modelName;
var hookState = {};
Model.notifyObserversOf('before save', { Model: Model, instance: inst }, function(err) {
var context = { Model: Model, instance: inst, hookState: hookState };
Model.notifyObserversOf('before save', context, function(err) {
if (err) return cb(err);
var data = inst.toObject(true);
@ -1542,7 +1567,8 @@ DataAccessObject.prototype.save = function (options, cb) {
return cb(err, inst);
}
inst._initProperties(data, { persisted: true });
Model.notifyObserversOf('after save', { Model: Model, instance: inst }, function(err) {
var context = { Model: Model, instance: inst, hookState: hookState };
Model.notifyObserversOf('after save', context, function(err) {
if (err) return cb(err, inst);
updateDone.call(inst, function () {
saveDone.call(inst, function () {
@ -1625,16 +1651,15 @@ DataAccessObject.updateAll = function (where, data, options, cb) {
where = query.where;
var Model = this;
var hookState = {};
Model.notifyObserversOf('access', { Model: Model, query: { where: where } }, function(err, ctx) {
var context = { Model: Model, query: { where: where }, hookState: hookState };
Model.notifyObserversOf('access', context, function(err, ctx) {
if (err) return cb(err);
Model.notifyObserversOf(
'before save',
{
Model: Model,
where: ctx.query.where,
data: data
},
var context = {
Model: Model, where: ctx.query.where, data: data, hookState: hookState
};
Model.notifyObserversOf('before save', context,
function(err, ctx) {
if (err) return cb(err);
doUpdate(ctx.where, ctx.data);
@ -1657,14 +1682,10 @@ DataAccessObject.updateAll = function (where, data, options, cb) {
var connector = Model.getDataSource().connector;
connector.update(Model.modelName, where, data, function(err, count) {
if (err) return cb (err);
Model.notifyObserversOf(
'after save',
{
Model: Model,
where: where,
data: data
},
function(err, ctx) {
var context = {
Model: Model, where: where, data: data, hookState: hookState
};
Model.notifyObserversOf('after save', context, function(err, ctx) {
return cb(err, count);
});
});
@ -1714,16 +1735,16 @@ DataAccessObject.prototype.remove =
var self = this;
var Model = this.constructor;
var id = getIdValue(this.constructor, this);
var hookState = {};
Model.notifyObserversOf(
'access',
{ Model: Model, query: byIdQuery(Model, id) },
function(err, ctx) {
var context = {
Model: Model, query: byIdQuery(Model, id), hookState: hookState
};
Model.notifyObserversOf('access', context, function(err, ctx) {
if (err) return cb(err);
Model.notifyObserversOf(
'before delete',
{ Model: Model, where: ctx.query.where },
function(err, ctx) {
var context = { Model: Model, where: ctx.query.where, hookState: hookState };
Model.notifyObserversOf('before delete', context, function(err, ctx) {
if (err) return cb(err);
doDeleteInstance(ctx.where);
});
@ -1736,7 +1757,8 @@ DataAccessObject.prototype.remove =
// We must switch to full query-based delete.
Model.deleteAll(where, { notify: false }, function(err) {
if (err) return cb(err);
Model.notifyObserversOf('after delete', { Model: Model, where: where }, function(err) {
var context = { Model: Model, where: where, hookState: hookState };
Model.notifyObserversOf('after delete', context, function(err) {
cb(err);
if (!err) Model.emit('deleted', id);
});
@ -1751,7 +1773,8 @@ DataAccessObject.prototype.remove =
}
destroyed(function () {
Model.notifyObserversOf('after delete', { Model: Model, where: where }, function(err) {
var context = { Model: Model, where: where, hookState: hookState };
Model.notifyObserversOf('after delete', context, function(err) {
cb(err);
if (!err) Model.emit('deleted', id);
});
@ -1858,6 +1881,7 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, op
var inst = this;
var Model = this.constructor;
var model = Model.modelName;
var hookState = {};
// Convert the data to be plain object so that update won't be confused
if (data instanceof Model) {
@ -1868,7 +1892,8 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, op
var context = {
Model: Model,
where: byIdQuery(Model, getIdValue(Model, inst)).where,
data: data
data: data,
hookState: hookState
};
Model.notifyObserversOf('before save', context, function(err, ctx) {
@ -1905,7 +1930,8 @@ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, op
done.call(inst, function () {
saveDone.call(inst, function () {
if (err) return cb(err, inst);
Model.notifyObserversOf('after save', { Model: Model, instance: inst }, function(err) {
var context = { Model: Model, instance: inst, hookState: hookState };
Model.notifyObserversOf('after save', context, function(err) {
if(!err) Model.emit('changed', inst);
cb(err, inst);
});

View File

@ -1095,6 +1095,31 @@ module.exports = function(dataSource, should) {
});
});
it('propagates hookState from `before delete` to `after delete`', function(done) {
TestModel.observe('before delete', pushContextAndNext(function(ctx) {
ctx.hookState.foo = 'bar';
}));
TestModel.observe('after delete', pushContextAndNext(function(ctx) {
ctx.hookState.foo = ctx.hookState.foo.toUpperCase();
}));
existingInstance.delete(function(err) {
if (err) return done(err);
observedContexts.should.eql([
aTestModelCtx({
hookState: { foo: 'bar', test: true },
where: { id: '1' }
}),
aTestModelCtx({
hookState: { foo: 'BAR', test: true },
where: { id: '1' }
})
]);
done();
});
});
it('triggers hooks only once', function(done) {
TestModel.observe('access', pushNameAndNext('access'));
TestModel.observe('after delete', pushNameAndNext('after delete'));
@ -1202,9 +1227,14 @@ module.exports = function(dataSource, should) {
});
});
function pushContextAndNext() {
function pushContextAndNext(fn) {
return function(context, next) {
if (typeof fn === 'function') {
fn(context);
}
context = deepCloneToObject(context);
context.hookState.test = true;
if (typeof observedContexts === 'string') {
observedContexts = context;
@ -1246,6 +1276,9 @@ module.exports = function(dataSource, should) {
function aTestModelCtx(ctx) {
ctx.Model = TestModel;
if (!ctx.hookState) {
ctx.hookState = { test: true };
}
return deepCloneToObject(ctx);
}